📄 peer_agent.h
字号:
int sec_; int secRcv_; //average number of messages received per second void statistics(); //print out statistics //APIs to GnutellaApp. GnutellaApps use them to send Gnutella messages. virtual int Connect(NodeAddr_t peer, double timeout); virtual void Ping(NodeAddr_t peer, int ttl); virtual void Pong(NodeAddr_t peer, int ttl, int cnt, NodeAddr_t *iplist, int *size, int *fnum, char *id); virtual void Query(NodeAddr_t peer, Word_t minSpeed, char *search); virtual void QueryHit(NodeAddr_t peer, Byte_t number, NodeAddr_t responder, int speed, char *results, char *id,int ttl); virtual void Bootstrap(NodeAddr_t peer); virtual void Bootstrap_Reply(NodeAddr_t peer, int cnt, NodeAddr_t *addrs); virtual void Disconnect(NodeAddr_t peer); virtual void UpdateBootcache(); //upcalls from Socket, triggered by socket status changes virtual int upcall_recv(Socket *, PacketData *, Handler *); virtual void upcall_connected(Socket *); virtual void upcall_passconn(Socket *); virtual void upcall_closing(Socket *); virtual void upcall_send(Socket *); //internal functions, used to process certain messages //broadcast routing protocol messages virtual int forward(Socket *incoming, PacketData *data, desc_hdr *hdr); //unicast routing for QueryHit and Pong messages virtual int backroute(PacketData *data, desc_hdr *hdr); void conn_timeout(); //connection request timeout void gc(); //garbage collection of: descriptors, pending connection requests and Queries Socket *find_desc(char *); int find_query(char *, int); int find_ping(char *, int); virtual void gnutella_req(Socket *); virtual void gnutella_ok(Socket *); virtual void gnutella_reject(Socket *); void lime_bootstrap(Socket *); void init_id(); protected: int command(int, const char*const*);};/* Gnutella Message Handler */class GnutellaMsg { public: GnutellaMsg(NodeAddr_t); desc_hdr header_; union tPayload { tPong *pong_; tQuery *query_; tQueryHit *queryhit_; tPush *push_; }; tPayload payload_; //the DescriptIDs for messages from a servent are generated with three //integers and the node address of the peer (last 4 bytes) int seq1_; int seq2_; int seq3_; NodeAddr_t addr_; virtual int parse(int len, unsigned char *); int clear(); char *new_descid(); PacketData *newpacket(char *id, Byte_t type, int ttl, int hops, int length, char *);};/**********************************************************************//**** Part II: GnutellaApp related ****//**** Part II-1: GnutellaApp basic ****//* List of connected peers maintained by GnutellaApp */class PeerEntry { public: PeerEntry(NodeAddr_t peer, double tstamp, Byte_t leaf) { peer_ = peer; lstamp_ = tstamp; isLeaf_ = leaf; } NodeAddr_t peer_; double lstamp_; Byte_t isLeaf_;};/* Periodic triggered timer to ping neighbours, maintained by GnutellaApp*/class PingTimer: public TimerHandler { friend class GnutellaApp; public: PingTimer(GnutellaApp *); void expire(Event *); GnutellaApp *app_;}; class WatchDog: public TimerHandler { friend class GnutellaApp; public: WatchDog(GnutellaApp *); void expire(Event *); GnutellaApp *app_;};/* pending requests */class PendingReqEntry { public: PendingReqEntry(char *id, double tstamp, int type) { memcpy((char *)&id_, id, 16); tstamp_ = tstamp; replycnt_ = 0; type_ = type; } DescriptorID_t id_; double tstamp_; int replycnt_; int type_;};/* base class for a peer-to-peer application *//* //pcj comment the GTNETS macro#ifdef GTNETSclass PeerApp: public Application {#else*/class PeerApp: public TclObject {//#endif public: PeerApp(NodeAddr_t addr); PeerApp() {}; NodeAddr_t addr_; int shared_Mb_; int shared_files_; int speed_; int isFreeloader_; PeerAgent *agent_; ActivityController *ac_; //activity controller PeerState_t state_; //OFFLINE or ACTIVE or IDLE PeerMap_t lpeers_; //list of peers /*user command interface */ virtual void join() {}; virtual void leave() {}; virtual void search(char* criteria) {}; virtual void share() {}; virtual void setState(PeerState_t newstate) {state_ = newstate; }; void setFreeloader(int freeloader) {isFreeloader_ = freeloader ;} ; /* upcalls provided to GnutellaAgent*/ Upcall_Proto *upcalls[10]; /* internal operations related to peer connections*/ virtual void bootstrap() {}; virtual void maintenance() {}; virtual void connect() {};}; /* GnutellaApp: Gnutella application, such as LimeWire, Gnucleus */class GnutellaApp: public PeerApp { public: GnutellaApp(NodeAddr_t addr); GnutellaApp(){} ; int ping_interval_; int watch_interval_; int isBootserver_; int max_deg_; PingTimer *ping_timer_; WatchDog *watchDog_; GnutellaAgent *gagent_; ServentMap_t servent_cache_; BasicStat *degree_; BServerList_t bserver_list_; int smpBoot_; SmpBootServer *bootSrv_; // add by zdh 04-03 desc_segment_ *segment_lable_; int nwindow; //jackie add lines Word_t segmentID_;//the id of the segment which app is query desc_cache *buffered_segment_; int buffer_size_; int file_size_; /*user command interface */ virtual void join(); virtual void leave(); virtual void stop(); //"stop" is forced "leave" but its current //implementation is identical to "leave" virtual void search(char* criteria); virtual void share(); virtual void disconnect(NodeAddr_t node); virtual void update_bootcache(); void setState(PeerState_t newstate); /* upcalls provided to GnutellaAgent*/ virtual void ConnectSucceeded(NodeAddr_t peer); void ConnectionRejected(NodeAddr_t peer); void ConnectionFailed(NodeAddr_t peer); void ConnectionTimeout(NodeAddr_t peer); void ConnectionClosed(NodeAddr_t peer); virtual int ConnectionRequest(NodeAddr_t peer, Socket *socket); virtual int UltraConnRequest(NodeAddr_t peer, Socket *socket) {return FALSE;}; virtual int LeafConnRequest(NodeAddr_t peer, Socket *socket) {return FALSE;}; virtual void PingRequest(NodeAddr_t peer, int ttl, char *id); virtual void PongReply(NodeAddr_t peer, int ttl, char* payload); //virtual void QueryRequest(NodeAddr_t peer, int ttl, char* payload, char *id); //modify by zdh 04-04 virtual int QueryRequest(NodeAddr_t peer, int ttl, char* payload, char *id); virtual void QueryHitReply(NodeAddr_t peer, int ttl, char* payload); virtual void BootstrapRequest(NodeAddr_t peer); virtual void BootstrapResult(int, NodeAddr_t *); virtual void BootcacheUpdate(NodeAddr_t peer); /* internal operations related to peer connections*/ virtual void bootstrap(); virtual void maintenance(); virtual void ping(); virtual void connect(int); virtual int avail(int); virtual void smp_bootstrap(); void setBootServer(FILE *); int find_servent(NodeAddr_t); void remove_servent(NodeAddr_t); //06-04-03 add by zdh void stat(); //20060321 Jackie modified int segmentIsBuffer(int ); void GnutellaApp::update_segmentcache(Word_t,Byte_t);protected: int command(int argc, const char*const*);};/**********************************************************************//**** Part II-2: behavioral model and activity controller ****//* UMASS model related definitions: system characteristics and peer behavior *//* behavioral model of a class of peers */typedef struct { int count_; double loff_; double lidle_; double poff_; int isFreeloader_;} ClassSpec_t;typedef std::map<int, ClassSpec_t> ClassMap_t;/*overall system characteristics */class PeerSys: public TclObject { public: PeerSys(); int nFiles_; //number of popular files studied int nClasses_; //number of classes (of peers) double *cdffile_; //CDF of a file being queried for double alpha_; //alpha in the Pareto distribution of file popularity ClassMap_t classes_; virtual void init_file(); virtual void init_class(FILE *fh); protected: int command(int, const char*const*);};/* dynamically controls peer behavior according to a system and class model */class ActivityController: public TimerHandler{ friend class GnutellaApp; public: ActivityController(PeerSys *, GnutellaApp *, int); PeerSys *sys_; GnutellaApp *app_; PeerState_t *state_; //pointer to the app_->state_ virtual void expire(Event *); //next change of state_ virtual void gen_query(); //generate a query based on cdffile_ ClassSpec_t *clSpec_; //class spec of the peer};/**********************************************************************//**** Part II-3: random bootstrap ****//* simple bootstrap process returns active servents randomly *//* format of a bootstrap result*/typedef struct { int cnt_; NodeAddr_t *servents_;}BootstrapRes_t ;/* Gnutella servent record maintained by a bootstrap server*/class GServentRec { public: GServentRec(Byte_t, NodeAddr_t, int, int, GnutellaApp *); Word_t port_; NodeAddr_t addr_; GnutellaApp *app_; int file_shared_; int byte_shared_;};/* bootstrapping server that uses random bootstrapping */class SmpBootServer: public SocketApp { public: SmpBootServer(); GServentMap_t servent_cache_; virtual BootstrapRes_t *BootstrapRequest(NodeAddr_t, GnutellaApp *, int); void RemovePeer(NodeAddr_t, GnutellaApp *);};/****** Part IV: random bootstrap for PDNS *********************************//* In PDNS, each physical node runs a simple bootstrap server similar to *//* SmpBootServer. Simple bootstrap servers on different physical nodes are *//* connected to and update each other on changes of available peers */#define BOOTSERVER_PORT 6734#define ADD_PEER 0#define REM_PEER 1//#define ADD_AVAIL 2//#define REM_AVAIL 3/* bootstrap servers on other nodes that are known to this bootserver */class BootServerEntry { public: BootServerEntry(NodeAddr_t addr, Socket *s) { addr_ = addr; sock_ = s; } NodeAddr_t addr_; Socket *sock_;};/* PDNS servent entry */class PDNSServentEntry { public: PDNSServentEntry(Port_t port, NodeAddr_t addr) { addr_ = addr; port_ = port; } NodeAddr_t addr_; Port_t port_;// int avail() { return (avail_>0);}};/* messages used to communicate among bootstrap servers */typedef struct { int type_; NodeAddr_t peer_;}PDNSBootMsg;typedef std::map<NodeAddr_t, BootServerEntry> BootServerMap_t;typedef std::map<NodeAddr_t, PDNSServentEntry> PDNSServentMap_t;/* PDNS bootstrap servers */class PDNSBootServer: public SmpBootServer { public: PDNSBootServer(NodeAddr_t); NodeAddr_t addr_; Port_t port_; BootServerMap_t bservers_; PDNSServentMap_t pservent_cache_; // PDNSBootstrapRes_t *BootstrapRequest(NodeAddr_t node); BootstrapRes_t *BootstrapRequest(NodeAddr_t node, GnutellaApp *, int peertype); void RemovePeer(NodeAddr_t node); void broadcast(int, NodeAddr_t peer); PDNSBootMsg *parse(int, unsigned char *); // upcalls from Socket int upcall_recv(Socket *, PacketData *, Handler *); void upcall_connected(Socket *) {}; void upcall_passconn(Socket *) {}; void upcall_closing(Socket *) {}; void upcall_send(Socket *) {}; protected: int command(int, const char*const*);};/***** Part V: Ultrapeer-Leaf Architecture *********//* GnutellaAgent for a Leaf peer*/class LeafAgent: public GnutellaAgent { public: LeafAgent(GnutellaApp *); LeafAgent(NodeAddr_t); virtual int upcall_recv(Socket *, PacketData *, Handler *); virtual void gnutella_req(Socket *sock);};/* Leaf Gnutella Application */class Leaf: public GnutellaApp { public: Leaf(NodeAddr_t addr): GnutellaApp(addr) {}; virtual void bootstrap(); virtual int avail(int); virtual void disconnect(NodeAddr_t);};/* GnutellaAgent for an Ultrapeer */class UltraAgent: public GnutellaAgent { public: UltraAgent(GnutellaApp *); UltraAgent(NodeAddr_t); virtual void Ping(NodeAddr_t peer, int ttl); virtual void Query(NodeAddr_t peer, Word_t minSpeed, char *search); virtual int forward(Socket *incoming, PacketData *data, desc_hdr *hdr); virtual void gnutella_req(Socket *sock); virtual void gnutella_ok(Socket *sock) ;};/* Ultrapper Gnutella Application */class Ultrapeer: public GnutellaApp { public: Ultrapeer(NodeAddr_t addr); int npeers_; //number of ultrapeer connections int nleaves_; //number of leaf connections int nlegacy_; //number of legacy peer connections virtual void bootstrap(); virtual int avail(int); //available for a particular type of connection virtual void connect(int); virtual void ping(); virtual void ConnectSucceeded(NodeAddr_t); virtual int ConnectionRequest(NodeAddr_t, Socket *); virtual int UltraConnRequest(NodeAddr_t, Socket *); virtual int LeafConnRequest(NodeAddr_t, Socket *); virtual void PongReply(NodeAddr_t peer, int ttl, char* payload); int max_peer_; //maximum number of ultrapeer connections int max_leaves_; //maximum number of leaf connections int max_legacy_; //maximum number of legacy peer connetions};#endif // __PEER_AGENT_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -