FountainWireProt.h

Go to the documentation of this file.
00001 /* ex: set tabstop=4 expandtab: */
00002 /*                                                                                                                      */
00003 /*  (c) 2004-2006 Iowa State University                                         */
00004 /*      see the LICENSE file in the top level directory         */
00005 /*                                                                                                                      */
00006 /* $Header: /afs/scl/project/cvsroot/fountain/source/common/FountainWireProt.h,v 1.32 2006/03/05 22:05:16 samm Exp $ */
00038 #ifndef FOUNTAIN_WIRE_PROT_H__
00039 #define FOUNTAIN_WIRE_PROT_H__
00040 
00041 class NodeID;
00042 
00043 #include "define.h"
00044 #include "FountainMessage.h"
00045 #include "Enforce.h"
00046 #include "LokiInclude.h"
00047 #include <bamboo/WireProt.h>
00048 #include <bamboo/SSSXML.h>
00049 #include <bamboo/qLog.h>
00050 #include <cstddef> //for NULL
00051 #include <vector>
00052 #include <sstream> //for stringstream
00053 
00059 struct SSSWireProtConnectionPolicy {
00060     typedef Loki::SmartPtr<BambooLib::SSSWireProt, Loki::NoCopy, Loki::DisallowConversion, Loki::NoCheck> type;
00061         typedef BambooLib::BuildMsg* SendType;
00062         typedef const BambooLib::ParseMsg* ReceiveType;
00063 };
00064 
00070 struct FountainMessagePtrSendPolicy {
00071     typedef FountainMessagePtr type;
00072 };
00073 
00079 struct ConstParseMsgPtrReceivePolicy {
00080     typedef ConstParseMsgPtr type;
00081 };
00082 
00088 template <class ConnectionPolicy>
00089 class LookupHostname {
00090     public:
00091         LookupHostname() : hostname_() {}
00092         void lookup(typename ConnectionPolicy::type &connection);
00093         std::string getHostname() const { return hostname_; }
00094     protected:
00095         ~LookupHostname() {}
00096         
00097     private:
00098         std::string hostname_;
00099 
00100 };
00101 
00107 template <class ConnectionPolicy>
00108 class NoLookupHostname {
00109     public:
00110         void lookup(typename ConnectionPolicy::type&) { }
00111 
00112     protected:
00113         ~NoLookupHostname() {}
00114 };
00115 
00121 template <
00122     class ConnectionPolicy = SSSWireProtConnectionPolicy,
00123     class SendPolicy = FountainMessagePtrSendPolicy,
00124     class ReceivePolicy = ConstParseMsgPtrReceivePolicy,
00125     template <class> class HostnamePolicy = LookupHostname
00126 >
00127 class FountainWireProtImpl : public HostnamePolicy<ConnectionPolicy> {
00128     private:
00129         //these typedefs are somewhat of a juxtaposition, a typedef typename... =D
00134         typedef typename ConnectionPolicy::type CT;
00135         
00140         typedef typename SendPolicy::type ST;
00141         
00146         typedef typename ReceivePolicy::type RT;
00147         
00152         typedef HostnamePolicy<ConnectionPolicy> HP;
00153     
00154     public:
00160         bool CheckForData(int sleepTime=0) {
00161             return connection_->CheckForData(sleepTime);
00162         }
00163         
00169         int sendMessage(ST msg) {
00170             return connection_->sendMessage(GetImpl(msg));
00171         }
00172         
00177         RT recvMessage() {
00178                         typename ConnectionPolicy::ReceiveType msg = connection_->recvMessage();
00179             if (!msg) {
00180                 return NULL;
00181             } else {
00182                                 return new FountainParseMessage(msg);
00183             }
00184         }
00185     
00191         int forwardMessage(RT msg) {
00193                         return connection_->forwardMessage(msg->getParseMsg());
00194         }
00195         
00196     protected:
00200                 FountainWireProtImpl(typename CT::PointerType connection) : connection_(connection) {}
00201         
00205         ~FountainWireProtImpl() {}
00206                 
00210         void lookupHostname() { HP::lookup(connection_); }
00211         
00215         CT connection_;
00216 };
00217 
00218 //forward declaration
00219 class FountainWireProt;
00220 
00230 typedef Loki::SmartPtr<FountainWireProt, Loki::RefCounted, Loki::DisallowConversion, Loki::RejectNull> FountainWireProtPtr;
00231 
00237 class FountainWireProt : public FountainWireProtImpl<SSSWireProtConnectionPolicy> {
00238     public:
00245         static FountainWireProtPtr Connect(BambooLib::SSS_Protocols protocol, const NodeID& remoteNode);
00246         
00251                 FountainWireProt(SSSWireProtConnectionPolicy::type::PointerType connection) : FountainWireProtImpl<SSSWireProtConnectionPolicy>(connection) {
00252             lookupHostname();
00253         }
00254                 
00258         ~FountainWireProt();
00259         
00260     private:
00265                 FountainWireProt(BambooLib::SSS_Protocols protocol) : FountainWireProtImpl<SSSWireProtConnectionPolicy>(BambooLib::SSSWireProt::CreateNewConnection(protocol)) {}
00266                 
00270         FountainWireProt(const FountainWireProt& rhs);
00271                 
00275         FountainWireProt& operator=(const FountainWireProt& rhs);
00276 };
00277 
00278 #endif

Generated on Wed Mar 8 14:43:31 2006 for Fountain by  doxygen 1.4.6