00001
00002
00003
00004
00005
00006
00036 #ifndef FOUNTAIN_WIRE_PROT_H_
00037 #define FOUNTAIN_WIRE_PROT_H_
00038
00039
00040 class NodeID;
00041
00042 #include "define.h"
00043 #include "FountainMessage.h"
00044 #include "Enforce.h"
00045 #include "Asserter.h"
00046 #include "LokiInclude.h"
00047 #include <bamboo/WireProt.h>
00048 #include <bamboo/SSSXML.h>
00049 #include <bamboo/qLog.h>
00050 #include <cstddef>
00051 #include <vector>
00052 #include <sstream>
00053
00058 struct SSSWireProtConnectionPolicy {
00059 typedef Loki::SmartPtr<BambooLib::SSSWireProt, Loki::NoCopy, Loki::DisallowConversion, Loki::NoCheck> type;
00060 typedef BambooLib::BuildMsg* SendType;
00061 typedef const BambooLib::ParseMsg* ReceiveType;
00062 };
00063
00068 struct FountainMessagePtrSendPolicy {
00069 typedef FountainMessagePtr type;
00070 };
00071
00076 struct ConstParseMsgPtrReceivePolicy {
00077 typedef ConstParseMsgPtr type;
00078 };
00079
00084 template <class ConnectionPolicy>
00085 class LookupHostname {
00086 public:
00087 LookupHostname() : hostname_() {}
00088 void lookup(typename ConnectionPolicy::type &connection);
00089 std::string getHostname() const { return hostname_; }
00090 protected:
00091 ~LookupHostname() {}
00092
00093 private:
00094 std::string hostname_;
00095
00096 };
00097
00102 template <class ConnectionPolicy>
00103 class NoLookupHostname {
00104 public:
00105 void lookup(typename ConnectionPolicy::type&) { }
00106
00107 protected:
00108 ~NoLookupHostname() {}
00109 };
00110
00115 template <
00116 class ConnectionPolicy = SSSWireProtConnectionPolicy,
00117 class SendPolicy = FountainMessagePtrSendPolicy,
00118 class ReceivePolicy = ConstParseMsgPtrReceivePolicy,
00119 template <class> class HostnamePolicy = LookupHostname
00120 >
00121 class FountainWireProtImpl : public HostnamePolicy<ConnectionPolicy> {
00122 private:
00127 typedef typename ConnectionPolicy::type CT;
00128
00133 typedef typename SendPolicy::type ST;
00134
00139 typedef typename ReceivePolicy::type RT;
00140
00145 typedef HostnamePolicy<ConnectionPolicy> HP;
00146
00147 public:
00153 bool CheckForData(int sleepTime=0) {
00154 return connection_->CheckForData(sleepTime);
00155 }
00156
00162 int sendMessage(ST msg) {
00163 ASSERT(GetImpl(msg));
00164 return connection_->sendMessage(GetImpl(msg));
00165 }
00166
00171 RT recvMessage() {
00172 typename ConnectionPolicy::ReceiveType msg = connection_->recvMessage();
00173 if (!msg) {
00174 return NULL;
00175 } else {
00176 return new FountainParseMessage(msg);
00177 }
00178 }
00179
00185 int forwardMessage(RT msg) {
00186
00187
00188
00189 return connection_->forwardMessage(*GetImpl(msg));
00190 }
00191
00192 protected:
00196 FountainWireProtImpl(typename CT::PointerType connection) : connection_(connection) {}
00197
00201 ~FountainWireProtImpl() {}
00202
00206 void lookupHostname() { HP::lookup(connection_); }
00207
00211 CT connection_;
00212 };
00213
00214
00215 class FountainWireProt;
00216
00226 typedef Loki::SmartPtr<FountainWireProt, Loki::RefCounted, Loki::DisallowConversion, Loki::RejectNull> FountainWireProtPtr;
00227
00232 class FountainWireProt : public FountainWireProtImpl<SSSWireProtConnectionPolicy> {
00233 public:
00240 static FountainWireProtPtr Connect(BambooLib::SSS_Protocols protocol, const NodeID& remoteNode);
00241
00246 FountainWireProt(SSSWireProtConnectionPolicy::type::PointerType connection) : FountainWireProtImpl<SSSWireProtConnectionPolicy>(connection) {
00247 lookupHostname();
00248 }
00249
00254 SSSWireProtConnectionPolicy::type::PointerType getConnection() { return Loki::GetImpl(connection_); }
00255
00259 ~FountainWireProt();
00260
00261 private:
00266 FountainWireProt(BambooLib::SSS_Protocols protocol) : FountainWireProtImpl<SSSWireProtConnectionPolicy>(BambooLib::SSSWireProt::CreateNewConnection(protocol)) {}
00267
00271 FountainWireProt(const FountainWireProt& rhs);
00272
00276 FountainWireProt& operator=(const FountainWireProt& rhs);
00277 };
00278
00279 #endif