Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

channels.cpp

00001 // channels.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #include "pch.h"
00004 #include "channels.h"
00005 
00006 NAMESPACE_BEGIN(CryptoPP)
00007 USING_NAMESPACE(std)
00008 
00009 #if 0
00010 void MessageSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &channel)
00011 {
00012         m_defaultRoutes.push_back(Route(&destination, channel));
00013 }
00014 
00015 void MessageSwitch::AddRoute(unsigned int begin, unsigned int end, BufferedTransformation &destination, const std::string &channel)
00016 {
00017         RangeRoute route(begin, end, Route(&destination, channel));
00018         RouteList::iterator it = upper_bound(m_routes.begin(), m_routes.end(), route);
00019         m_routes.insert(it, route);
00020 }
00021 
00022 /*
00023 class MessageRouteIterator
00024 {
00025 public:
00026         typedef MessageSwitch::RouteList::const_iterator RouteIterator;
00027         typedef MessageSwitch::DefaultRouteList::const_iterator DefaultIterator;
00028 
00029         bool m_useDefault;
00030         RouteIterator m_itRouteCurrent, m_itRouteEnd;
00031         DefaultIterator m_itDefaultCurrent, m_itDefaultEnd;
00032 
00033         MessageRouteIterator(MessageSwitch &ms, const std::string &channel)
00034                 : m_channel(channel)
00035         {
00036                 pair<MapIterator, MapIterator> range = cs.m_routeMap.equal_range(channel);
00037                 if (range.first == range.second)
00038                 {
00039                         m_useDefault = true;
00040                         m_itListCurrent = cs.m_defaultRoutes.begin();
00041                         m_itListEnd = cs.m_defaultRoutes.end();
00042                 }
00043                 else
00044                 {
00045                         m_useDefault = false;
00046                         m_itMapCurrent = range.first;
00047                         m_itMapEnd = range.second;
00048                 }
00049         }
00050 
00051         bool End() const
00052         {
00053                 return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;
00054         }
00055 
00056         void Next()
00057         {
00058                 if (m_useDefault)
00059                         ++m_itListCurrent;
00060                 else
00061                         ++m_itMapCurrent;
00062         }
00063 
00064         BufferedTransformation & Destination()
00065         {
00066                 return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;
00067         }
00068 
00069         const std::string & Message()
00070         {
00071                 if (m_useDefault)
00072                         return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;
00073                 else
00074                         return m_itMapCurrent->second.second;
00075         }
00076 };
00077 
00078 void MessageSwitch::Put(byte inByte);
00079 void MessageSwitch::Put(const byte *inString, unsigned int length);
00080 
00081 void MessageSwitch::Flush(bool completeFlush, int propagation=-1);
00082 void MessageSwitch::MessageEnd(int propagation=-1);
00083 void MessageSwitch::PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);
00084 void MessageSwitch::MessageSeriesEnd(int propagation=-1);
00085 */
00086 #endif
00087 
00088 class ChannelRouteIterator
00089 {
00090 public:
00091         typedef ChannelSwitch::RouteMap::const_iterator MapIterator;
00092         typedef ChannelSwitch::DefaultRouteList::const_iterator ListIterator;
00093 
00094         const std::string m_channel;
00095         bool m_useDefault;
00096         MapIterator m_itMapCurrent, m_itMapEnd;
00097         ListIterator m_itListCurrent, m_itListEnd;
00098 
00099         ChannelRouteIterator(ChannelSwitch &cs, const std::string &channel)
00100                 : m_channel(channel)
00101         {
00102                 pair<MapIterator, MapIterator> range = cs.m_routeMap.equal_range(channel);
00103                 if (range.first == range.second)
00104                 {
00105                         m_useDefault = true;
00106                         m_itListCurrent = cs.m_defaultRoutes.begin();
00107                         m_itListEnd = cs.m_defaultRoutes.end();
00108                 }
00109                 else
00110                 {
00111                         m_useDefault = false;
00112                         m_itMapCurrent = range.first;
00113                         m_itMapEnd = range.second;
00114                 }
00115         }
00116 
00117         bool End() const
00118         {
00119                 return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;
00120         }
00121 
00122         void Next()
00123         {
00124                 if (m_useDefault)
00125                         ++m_itListCurrent;
00126                 else
00127                         ++m_itMapCurrent;
00128         }
00129 
00130         BufferedTransformation & Destination()
00131         {
00132                 return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;
00133         }
00134 
00135         const std::string & Channel()
00136         {
00137                 if (m_useDefault)
00138                         return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;
00139                 else
00140                         return m_itMapCurrent->second.second;
00141         }
00142 };
00143 
00144 unsigned int ChannelSwitch::ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking)
00145 {
00146         if (!blocking)
00147                 throw BlockingInputOnly("ChannelSwitch");
00148 
00149         ChannelRouteIterator it(*this, channel);
00150         while (!it.End())
00151         {
00152                 it.Destination().ChannelPut2(it.Channel(), begin, length, messageEnd, blocking);
00153                 it.Next();
00154         }
00155         return 0;
00156 }
00157 
00158 void ChannelSwitch::ChannelInitialize(const std::string &channel, const NameValuePairs &parameters/* =g_nullNameValuePairs */, int propagation/* =-1 */)
00159 {
00160         if (channel.empty())
00161         {
00162                 m_routeMap.clear();
00163                 m_defaultRoutes.clear();
00164         }
00165 
00166         ChannelRouteIterator it(*this, channel);
00167         while (!it.End())
00168         {
00169                 it.Destination().ChannelInitialize(it.Channel(), parameters, propagation);
00170                 it.Next();
00171         }
00172 }
00173 
00174 bool ChannelSwitch::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking)
00175 {
00176         if (!blocking)
00177                 throw BlockingInputOnly("ChannelSwitch");
00178 
00179         ChannelRouteIterator it(*this, channel);
00180         while (!it.End())
00181         {
00182                 it.Destination().ChannelFlush(it.Channel(), completeFlush, propagation, blocking);
00183                 it.Next();
00184         }
00185         return false;
00186 }
00187 
00188 bool ChannelSwitch::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking)
00189 {
00190         if (!blocking)
00191                 throw BlockingInputOnly("ChannelSwitch");
00192 
00193         ChannelRouteIterator it(*this, channel);
00194         while (!it.End())
00195         {
00196                 it.Destination().ChannelMessageSeriesEnd(it.Channel(), propagation);
00197                 it.Next();
00198         }
00199         return false;
00200 }
00201 
00202 byte * ChannelSwitch::ChannelCreatePutSpace(const std::string &channel, unsigned int &size)
00203 {
00204         ChannelRouteIterator it(*this, channel);
00205         if (!it.End())
00206         {
00207                 BufferedTransformation &target = it.Destination();
00208                 it.Next();
00209                 if (it.End())   // there is only one target channel
00210                         return target.ChannelCreatePutSpace(it.Channel(), size);
00211         }
00212         size = 0;
00213         return NULL;
00214 }
00215 
00216 unsigned int ChannelSwitch::ChannelPutModifiable2(const std::string &channel, byte *inString, unsigned int length, int messageEnd, bool blocking)
00217 {
00218         if (!blocking)
00219                 throw BlockingInputOnly("ChannelSwitch");
00220 
00221         ChannelRouteIterator it(*this, channel);
00222         if (!it.End())
00223         {
00224                 BufferedTransformation &target = it.Destination();
00225                 const std::string &targetChannel = it.Channel();
00226                 it.Next();
00227                 if (it.End())   // there is only one target channel
00228                         return target.ChannelPutModifiable2(targetChannel, inString, length, messageEnd, blocking);
00229         }
00230         ChannelPut2(channel, inString, length, messageEnd, blocking);
00231         return false;
00232 }
00233 
00234 void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination)
00235 {
00236         m_defaultRoutes.push_back(DefaultRoute(&destination, value_ptr<std::string>(NULL)));
00237 }
00238 
00239 void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination)
00240 {
00241         for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)
00242                 if (it->first == &destination && !it->second.get())
00243                 {
00244                         m_defaultRoutes.erase(it);
00245                         break;
00246                 }
00247 }
00248 
00249 void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)
00250 {
00251         m_defaultRoutes.push_back(DefaultRoute(&destination, outChannel));
00252 }
00253 
00254 void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)
00255 {
00256         for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)
00257                 if (it->first == &destination && (it->second.get() && *it->second == outChannel))
00258                 {
00259                         m_defaultRoutes.erase(it);
00260                         break;
00261                 }
00262 }
00263 
00264 void ChannelSwitch::AddRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)
00265 {
00266         m_routeMap.insert(RouteMap::value_type(inChannel, Route(&destination, outChannel)));
00267 }
00268 
00269 void ChannelSwitch::RemoveRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)
00270 {
00271         typedef ChannelSwitch::RouteMap::iterator MapIterator;
00272         pair<MapIterator, MapIterator> range = m_routeMap.equal_range(inChannel);
00273         
00274         for (MapIterator it = range.first; it != range.second; ++it)
00275                 if (it->second.first == &destination && it->second.second == outChannel)
00276                 {
00277                         m_routeMap.erase(it);
00278                         break;
00279                 }
00280 }
00281 
00282 NAMESPACE_END

Generated on Mon Apr 19 18:12:29 2004 for Crypto++ by doxygen 1.3.6-20040222