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