1 // channels.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file channels.h
4 /// \brief Classes for multiple named channels
5
6 #ifndef CRYPTOPP_CHANNELS_H
7 #define CRYPTOPP_CHANNELS_H
8
9 #include "cryptlib.h"
10 #include "simple.h"
11 #include "smartptr.h"
12 #include "stdcpp.h"
13
14 #if CRYPTOPP_MSC_VERSION
15 # pragma warning(push)
16 # pragma warning(disable: 4355)
17 #endif
18
NAMESPACE_BEGIN(CryptoPP)19 NAMESPACE_BEGIN(CryptoPP)
20
21 #if 0
22 /// Route input on default channel to different and/or multiple channels based on message sequence number
23 class MessageSwitch : public Sink
24 {
25 public:
26 void AddDefaultRoute(BufferedTransformation &destination, const std::string &channel);
27 void AddRoute(unsigned int begin, unsigned int end, BufferedTransformation &destination, const std::string &channel);
28
29 void Put(byte inByte);
30 void Put(const byte *inString, unsigned int length);
31
32 void Flush(bool completeFlush, int propagation=-1);
33 void MessageEnd(int propagation=-1);
34 void PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);
35 void MessageSeriesEnd(int propagation=-1);
36
37 private:
38 typedef std::pair<BufferedTransformation *, std::string> Route;
39 struct RangeRoute
40 {
41 RangeRoute(unsigned int begin, unsigned int end, const Route &route)
42 : begin(begin), end(end), route(route) {}
43 bool operator<(const RangeRoute &rhs) const {return begin < rhs.begin;}
44 unsigned int begin, end;
45 Route route;
46 };
47
48 typedef std::list<RangeRoute> RouteList;
49 typedef std::list<Route> DefaultRouteList;
50
51 RouteList m_routes;
52 DefaultRouteList m_defaultRoutes;
53 unsigned int m_nCurrentMessage;
54 };
55 #endif
56
57 class ChannelSwitchTypedefs
58 {
59 public:
60 typedef std::pair<BufferedTransformation *, std::string> Route;
61 typedef std::multimap<std::string, Route> RouteMap;
62
63 typedef std::pair<BufferedTransformation *, value_ptr<std::string> > DefaultRoute;
64 typedef std::list<DefaultRoute> DefaultRouteList;
65
66 // SunCC workaround: can't use const_iterator here
67 typedef RouteMap::iterator MapIterator;
68 typedef DefaultRouteList::iterator ListIterator;
69 };
70
71 class ChannelSwitch;
72
73 class ChannelRouteIterator : public ChannelSwitchTypedefs
74 {
75 public:
ChannelRouteIterator(ChannelSwitch & cs)76 ChannelRouteIterator(ChannelSwitch &cs) : m_cs(cs), m_useDefault(false) {}
77
78 void Reset(const std::string &channel);
79 bool End() const;
80 void Next();
81 BufferedTransformation & Destination();
82 const std::string & Channel();
83
84 ChannelSwitch& m_cs;
85 std::string m_channel;
86 bool m_useDefault;
87 MapIterator m_itMapCurrent, m_itMapEnd;
88 ListIterator m_itListCurrent, m_itListEnd;
89
90 protected:
91 // Hide this to see if we break something...
92 ChannelRouteIterator();
93 };
94
95 /// Route input to different and/or multiple channels based on channel ID
96 class CRYPTOPP_DLL ChannelSwitch : public Multichannel<Sink>, public ChannelSwitchTypedefs
97 {
98 public:
ChannelSwitch()99 ChannelSwitch() : m_it(*this), m_blocked(false) {}
ChannelSwitch(BufferedTransformation & destination)100 ChannelSwitch(BufferedTransformation &destination) : m_it(*this), m_blocked(false)
101 {
102 AddDefaultRoute(destination);
103 }
ChannelSwitch(BufferedTransformation & destination,const std::string & outChannel)104 ChannelSwitch(BufferedTransformation &destination, const std::string &outChannel) : m_it(*this), m_blocked(false)
105 {
106 AddDefaultRoute(destination, outChannel);
107 }
108
109 void IsolatedInitialize(const NameValuePairs ¶meters=g_nullNameValuePairs);
110
111 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
112 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking);
113
114 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true);
115 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
116
117 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
118
119 void AddDefaultRoute(BufferedTransformation &destination);
120 void RemoveDefaultRoute(BufferedTransformation &destination);
121 void AddDefaultRoute(BufferedTransformation &destination, const std::string &outChannel);
122 void RemoveDefaultRoute(BufferedTransformation &destination, const std::string &outChannel);
123 void AddRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel);
124 void RemoveRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel);
125
126 private:
127 RouteMap m_routeMap;
128 DefaultRouteList m_defaultRoutes;
129
130 ChannelRouteIterator m_it;
131 bool m_blocked;
132
133 friend class ChannelRouteIterator;
134 };
135
136 NAMESPACE_END
137
138 #if CRYPTOPP_MSC_VERSION
139 # pragma warning(pop)
140 #endif
141
142 #endif
143