Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound 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 void ChannelSwitch::Put(byte inByte)
00010 {
00011         ChannelPut(NULL_CHANNEL, inByte);
00012 }
00013 
00014 void ChannelSwitch::Put(const byte *inString, unsigned int length)
00015 {
00016         ChannelPut(NULL_CHANNEL, inString, length);
00017 }
00018 
00019 void ChannelSwitch::Flush(bool completeFlush, int propagation)
00020 {
00021         ChannelFlush(NULL_CHANNEL, completeFlush, propagation);
00022 }
00023 
00024 void ChannelSwitch::MessageEnd(int propagation)
00025 {
00026         ChannelMessageEnd(NULL_CHANNEL, propagation);
00027 }
00028 
00029 void ChannelSwitch::MessageSeriesEnd(int propagation)
00030 {
00031         ChannelMessageSeriesEnd(NULL_CHANNEL, propagation);
00032 }
00033 
00034 void ChannelSwitch::PutMessageEnd(const byte *inString, unsigned int length, int propagation)
00035 {
00036         ChannelPutMessageEnd(NULL_CHANNEL, inString, length, propagation);
00037 }
00038 
00039 class RouteIterator
00040 {
00041 public:
00042         typedef ChannelSwitch::RouteMap::const_iterator MapIterator;
00043         typedef ChannelSwitch::DefaultRouteList::const_iterator ListIterator;
00044 
00045         const std::string m_channel;
00046         bool m_useDefault;
00047         MapIterator m_itMapCurrent, m_itMapEnd;
00048         ListIterator m_itListCurrent, m_itListEnd;
00049 
00050         RouteIterator(ChannelSwitch &cs, const std::string &channel)
00051                 : m_channel(channel)
00052         {
00053                 pair<MapIterator, MapIterator> range = cs.m_routeMap.equal_range(channel);
00054                 if (range.first == range.second)
00055                 {
00056                         m_useDefault = true;
00057                         m_itListCurrent = cs.m_defaultRoutes.begin();
00058                         m_itListEnd = cs.m_defaultRoutes.end();
00059                 }
00060                 else
00061                 {
00062                         m_useDefault = false;
00063                         m_itMapCurrent = range.first;
00064                         m_itMapEnd = range.second;
00065                 }
00066         }
00067 
00068         bool End() const
00069         {
00070                 return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;
00071         }
00072 
00073         void Next()
00074         {
00075                 if (m_useDefault)
00076                         ++m_itListCurrent;
00077                 else
00078                         ++m_itMapCurrent;
00079         }
00080 
00081         BufferedTransformation & Destination()
00082         {
00083                 return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;
00084         }
00085 
00086         const std::string & Channel()
00087         {
00088                 if (m_useDefault)
00089                         return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;
00090                 else
00091                         return m_itMapCurrent->second.second;
00092         }
00093 };
00094 
00095 void ChannelSwitch::ChannelPut(const std::string &channel, byte inByte)
00096 {
00097         RouteIterator it(*this, channel);
00098         while (!it.End())
00099         {
00100                 it.Destination().ChannelPut(it.Channel(), inByte);
00101                 it.Next();
00102         }
00103 }
00104 
00105 void ChannelSwitch::ChannelPut(const std::string &channel, const byte *inString, unsigned int length)
00106 {
00107         RouteIterator it(*this, channel);
00108         while (!it.End())
00109         {
00110                 it.Destination().ChannelPut(it.Channel(), inString, length);
00111                 it.Next();
00112         }
00113 }
00114 
00115 void ChannelSwitch::ChannelFlush(const std::string &channel, bool completeFlush, int propagation)
00116 {
00117         RouteIterator it(*this, channel);
00118         while (!it.End())
00119         {
00120                 it.Destination().ChannelFlush(it.Channel(), completeFlush, propagation);
00121                 it.Next();
00122         }
00123 }
00124 
00125 void ChannelSwitch::ChannelMessageEnd(const std::string &channel, int propagation)
00126 {
00127         RouteIterator it(*this, channel);
00128         while (!it.End())
00129         {
00130                 it.Destination().ChannelMessageEnd(it.Channel(), propagation);
00131                 it.Next();
00132         }
00133 }
00134 
00135 void ChannelSwitch::ChannelMessageSeriesEnd(const std::string &channel, int propagation)
00136 {
00137         RouteIterator it(*this, channel);
00138         while (!it.End())
00139         {
00140                 it.Destination().ChannelMessageSeriesEnd(it.Channel(), propagation);
00141                 it.Next();
00142         }
00143 }
00144 
00145 void ChannelSwitch::ChannelPutMessageEnd(const std::string &channel, const byte *inString, unsigned int length, int propagation)
00146 {
00147         RouteIterator it(*this, channel);
00148         while (!it.End())
00149         {
00150                 it.Destination().ChannelPutMessageEnd(it.Channel(), inString, length, propagation);
00151                 it.Next();
00152         }
00153 }
00154 
00155 void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination)
00156 {
00157         m_defaultRoutes.push_back(DefaultRoute(&destination, value_ptr<std::string>(NULL)));
00158 }
00159 
00160 void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination)
00161 {
00162         for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)
00163                 if (it->first == &destination && !it->second.get())
00164                 {
00165                         m_defaultRoutes.erase(it);
00166                         break;
00167                 }
00168 }
00169 
00170 void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)
00171 {
00172         m_defaultRoutes.push_back(DefaultRoute(&destination, outChannel));
00173 }
00174 
00175 void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)
00176 {
00177         for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)
00178                 if (it->first == &destination && (it->second.get() && *it->second == outChannel))
00179                 {
00180                         m_defaultRoutes.erase(it);
00181                         break;
00182                 }
00183 }
00184 
00185 void ChannelSwitch::AddRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)
00186 {
00187         m_routeMap.insert(RouteMap::value_type(inChannel, Route(&destination, outChannel)));
00188 }
00189 
00190 void ChannelSwitch::RemoveRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)
00191 {
00192         typedef ChannelSwitch::RouteMap::iterator MapIterator;
00193         pair<MapIterator, MapIterator> range = m_routeMap.equal_range(inChannel);
00194         
00195         for (MapIterator it = range.first; it != range.second; ++it)
00196                 if (it->second.first == &destination && it->second.second == outChannel)
00197                 {
00198                         m_routeMap.erase(it);
00199                         break;
00200                 }
00201 }
00202 
00203 NAMESPACE_END

Generated at Mon Jan 15 01:16:30 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000