⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 marshal.h

📁 件主要用于帮助计算机爱好者学习蚁群算法时做有关蚁群算法的试验。蚁群算法作为一种优秀的新兴的算法
💻 H
📖 第 1 页 / 共 3 页
字号:
		assert(false);		return NULL;	}			static char type() { return REQ_MEASURE_N_ICMP; }	virtual char getPacketType() const	{ return type(); }	virtual ~ReqMeasureICMP() {}			};#endifclass ReqConstraintGeneric : public RendvHeaderPacket {protected:	uint16_t betaNum;	uint16_t betaDen;	vector<NodeIdentConst> targets;		public:		ReqConstraintGeneric(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: 	RendvHeaderPacket(id, in_rendv_addr, in_rendv_port), 			betaNum(in_beta_num), betaDen(in_beta_den) {}			template <class T>	static ReqConstraintGeneric* parse(const char* buf, int numBytes) {		BufferWrapper rb(buf, numBytes);		char queryType = rb.retrieve_char();		if (rb.error() || queryType != T::type()) {			ERROR_LOG("Wrong type received\n");			return NULL;			}		uint32_t queryID_1 = ntohl(rb.retrieve_uint());		uint32_t queryID_2 = ntohl(rb.retrieve_uint());		uint64_t queryID = to64(queryID_1, queryID_2);		uint32_t magicNumber = ntohl(rb.retrieve_uint());				uint32_t rendvAddr = ntohl(rb.retrieve_uint());		uint16_t rendvPort = ntohs(rb.retrieve_ushort());		uint16_t in_betaNum = ntohs(rb.retrieve_ushort());		uint16_t in_betaDen = ntohs(rb.retrieve_ushort());		if (rb.error() || magicNumber != MAGIC_NUMBER) {			ERROR_LOG("Wrong magic number in packet received\n");			return NULL;					}				ReqConstraintGeneric* ret 			= new T(queryID, in_betaNum, in_betaDen, rendvAddr, rendvPort);					uint32_t numEntry = ntohl(rb.retrieve_uint());		NodeIdentConst tmpIdent;				//while (!rb.error() && numEntry-- > 0) {		for (uint32_t i = 0; (!rb.error() && i < numEntry); i++) {						tmpIdent.addr = ntohl(rb.retrieve_uint());			tmpIdent.port = ntohs(rb.retrieve_ushort());			tmpIdent.latencyConstMS = ntohl(rb.retrieve_uint());				ret->addTarget(tmpIdent);		}		if (rb.error()) {			delete ret;			return NULL;		}				return ret; 			}		virtual int createRealPacket(RealPacket& inPacket) const {		uint32_t num_targets = targets.size();		if (num_targets == 0) {			return -1;		}		inPacket.append_char(getPacketType());		write_id(inPacket);				write_rendv(inPacket);		inPacket.append_ushort(htons(betaNum));		inPacket.append_ushort(htons(betaDen));		inPacket.append_uint(htonl(num_targets));		for (uint32_t i = 0; i < num_targets; i++) {			NodeIdentConst tmp = targets[i];			inPacket.append_uint(htonl(tmp.addr));			inPacket.append_ushort(htons(tmp.port));			inPacket.append_uint(htonl(tmp.latencyConstMS));		}							if (!inPacket.completeOkay()) { 			return -1; 		}		return 0;	}		uint16_t getBetaNumerator(){		return betaNum;	}		uint16_t getBetaDenominator() {		return betaDen;	}		void addTarget(const NodeIdentConst& in_node) {		targets.push_back(in_node);	}		const vector<NodeIdentConst>* returnTargets() {		return &targets;		}		virtual char getPacketType() const = 0;	virtual ~ReqConstraintGeneric() {}			};class ReqConstraintTCP : public ReqConstraintGeneric { public:	ReqConstraintTCP(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqConstraintGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {}	template <class T>	static ReqConstraintGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CONSTRAINT_N_TCP; }	virtual char getPacketType() const	{ return type(); }			virtual ~ReqConstraintTCP() {}		};class ReqConstraintDNS : public ReqConstraintGeneric { public:	ReqConstraintDNS(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqConstraintGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {}	template <class T>	static ReqConstraintGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CONSTRAINT_N_DNS; }	virtual char getPacketType() const	{ return type(); }				virtual ~ReqConstraintDNS() {}		};class ReqConstraintPing : public ReqConstraintGeneric { public:	ReqConstraintPing(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqConstraintGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {}	template <class T>	static ReqConstraintGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CONSTRAINT_N_PING; }	virtual char getPacketType() const	{ return type(); }				virtual ~ReqConstraintPing() {}		};#ifdef PLANET_LAB_SUPPORTclass ReqConstraintICMP : public ReqConstraintGeneric { public:	ReqConstraintICMP(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqConstraintGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {}	template <class T>	static ReqConstraintGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CONSTRAINT_N_ICMP; }	virtual char getPacketType() const	{ return type(); }				virtual ~ReqConstraintICMP() {}		};#endif/*class ReqConstraintTCP : public RendvHeaderPacket {protected:	vector<NodeIdentConst> targets;public:	ReqConstraintTCP(uint64_t id, uint32_t in_rendv_addr, uint16_t in_rendv_port) 		: RendvHeaderPacket(id, in_rendv_addr, in_rendv_port) {}			virtual int createRealPacket(RealPacket& inPacket) const {		uint32_t num_targets = targets.size();		//	Must have at least one packet		if (num_targets == 0) { 			return -1; 		}		inPacket.append_char(getPacketType());		write_id(inPacket);				write_rendv(inPacket);		inPacket.append_uint(htonl(num_targets));		for (uint32_t i = 0; i < num_targets; i++) {			NodeIdentConst tmp = targets[i];			inPacket.append_uint(htonl(tmp.addr));			inPacket.append_ushort(htons(tmp.port));			inPacket.append_uint(htonl(tmp.latencyConstMS));		}		if (!inPacket.completeOkay()) { 			return -1; 		}		return 0; // Packet completed correctly	}			virtual char getPacketType() const 	{ return REQ_CONSTRAINT_N_TCP; }					//	Add TCP server targets			void addTarget(uint32_t addr, uint16_t port, uint32_t latencyMS) {		NodeIdentConst tmp = {addr, port, latencyMS};		targets.push_back(tmp);	}	virtual ~ReqConstraintTCP() {}};*/class ReqClosestGeneric : public RendvHeaderPacket {protected:	uint16_t betaNum;	uint16_t betaDen;	vector<NodeIdent> targets;		public:		ReqClosestGeneric(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: 	RendvHeaderPacket(id, in_rendv_addr, in_rendv_port), 			betaNum(in_beta_num), betaDen(in_beta_den) {}			template <class T>	static ReqClosestGeneric* parse(const char* buf, int numBytes) {		BufferWrapper rb(buf, numBytes);		char queryType = rb.retrieve_char();		if (rb.error() || queryType != T::type()) {			ERROR_LOG("Wrong type received\n");			return NULL;			}		uint32_t queryID_1 = ntohl(rb.retrieve_uint());		uint32_t queryID_2 = ntohl(rb.retrieve_uint());		uint64_t queryID = to64(queryID_1, queryID_2);		uint32_t magicNumber = ntohl(rb.retrieve_uint());				uint32_t rendvAddr = ntohl(rb.retrieve_uint());		uint16_t rendvPort = ntohs(rb.retrieve_ushort());		uint16_t in_betaNum = ntohs(rb.retrieve_ushort());		uint16_t in_betaDen = ntohs(rb.retrieve_ushort());		if (rb.error() || magicNumber != MAGIC_NUMBER) {			ERROR_LOG("Wrong magic number in packet received\n");			return NULL;					}				ReqClosestGeneric* ret 			= new T(queryID, in_betaNum, in_betaDen, rendvAddr, rendvPort);					uint32_t numEntry = ntohl(rb.retrieve_uint());		NodeIdent tmpIdent;				//while (!rb.error() && numEntry-- > 0) {		for (uint32_t i = 0; (!rb.error() && i < numEntry); i++) {						tmpIdent.addr = ntohl(rb.retrieve_uint());			tmpIdent.port = ntohs(rb.retrieve_ushort());			ret->addTarget(tmpIdent);		}		if (rb.error()) {			delete ret;			return NULL;		}				return ret; 			}		virtual int createRealPacket(RealPacket& inPacket) const {		uint32_t num_targets = targets.size();		if (num_targets == 0) {			return -1;		}		inPacket.append_char(getPacketType());		write_id(inPacket);				write_rendv(inPacket);		inPacket.append_ushort(htons(betaNum));		inPacket.append_ushort(htons(betaDen));		inPacket.append_uint(htonl(num_targets));		for (uint32_t i = 0; i < num_targets; i++) {			NodeIdent tmp = targets[i];			inPacket.append_uint(htonl(tmp.addr));			inPacket.append_ushort(htons(tmp.port));					}							if (!inPacket.completeOkay()) { 			return -1; 		}		return 0;	}		uint16_t getBetaNumerator(){		return betaNum;	}		uint16_t getBetaDenominator() {		return betaDen;	}		void addTarget(const NodeIdent& in_node) {		targets.push_back(in_node);	}		const vector<NodeIdent>* returnTargets() {		return &targets;		}		virtual char getPacketType() const = 0;	virtual ~ReqClosestGeneric() {}			};class ReqClosestTCP : public ReqClosestGeneric { public:	ReqClosestTCP(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqClosestGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {}	template <class T>	static ReqClosestGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CLOSEST_N_TCP; }	virtual char getPacketType() const	{ return type(); }				virtual ~ReqClosestTCP() {}		};class ReqClosestMeridPing : public ReqClosestGeneric { public:	ReqClosestMeridPing(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqClosestGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {} 	template <class T>	static ReqClosestGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CLOSEST_N_MERID_PING; }	virtual char getPacketType() const	{ return type(); }							virtual ~ReqClosestMeridPing() {}		};class ReqClosestDNS : public ReqClosestGeneric {public:	ReqClosestDNS(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqClosestGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {}			template <class T>	static ReqClosestGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CLOSEST_N_DNS; }	virtual char getPacketType() const	{ return type(); }						virtual ~ReqClosestDNS() {}				};#ifdef PLANET_LAB_SUPPORTclass ReqClosestICMP : public ReqClosestGeneric { public:	ReqClosestICMP(uint64_t id, uint16_t in_beta_num, uint16_t in_beta_den, 			uint32_t in_rendv_addr, uint16_t in_rendv_port)  		: ReqClosestGeneric(id, in_beta_num, in_beta_den, 			in_rendv_addr, in_rendv_port) {} 	template <class T>	static ReqClosestGeneric* parse(const char* buf, int numBytes) {		assert(false);		return NULL;	}			static char type() { return REQ_CLOSEST_N_ICMP; }	virtual char getPacketType() const	{ return type(); }						virtual ~ReqClosestICMP() {}		};#endifclass RetError : public Packet {public:	RetError(uint64_t id) : Packet(id) {}		static RetError* parse(const char* buf, int numBytes) {		BufferWrapper rb(buf, numBytes);		char queryType = rb.retrieve_char();		if (rb.error() || queryType != RET_ERROR) {			ERROR_LOG("Wrong type received\n");			return NULL;			}		uint32_t queryID_1 = ntohl(rb.retrieve_uint());		uint32_t queryID_2 = ntohl(rb.retrieve_uint());		uint64_t queryID = to64(queryID_1, queryID_2);		uint32_t magicNumber = ntohl(rb.retrieve_uint());		if (rb.error() || magicNumber != MAGIC_NUMBER) {			ERROR_LOG("Wrong magic number in packet received\n");			return NULL;					}		return new RetError(queryID); 			}		virtual int createRealPacket(RealPacket& inPacket) const {		inPacket.append_char(getPacketType());		write_id(inPacket);						if (!inPacket.completeOkay()) { 			return -1; 		}		return 0; // Packet completed correctly			}			virtual char getPacketType() const	{ return RET_ERROR; }	virtual ~RetError() {}		};class RetInfo : public Packet {private:	uint32_t 	addr;	uint16_t port;public:	RetInfo(uint64_t id, uint32_t in_addr, uint16_t in_port) 		: Packet(id), addr(in_addr), port(in_port) {}		static RetInfo* parse(			const NodeIdent& in_remoteNode, const char* buf, int numBytes) {		BufferWrapper rb(buf, numBytes);		char queryType = rb.retrieve_char();		if (rb.error() || queryType != RET_INFO) {			ERROR_LOG("Wrong type received\n");			return NULL;			}		uint32_t queryID_1 = ntohl(rb.retrieve_uint());		uint32_t queryID_2 = ntohl(rb.retrieve_uint());		uint64_t queryID = to64(queryID_1, queryID_2);		uint32_t magicNumber = ntohl(rb.retrieve_uint());		uint32_t in_addr = ntohl(rb.retrieve_uint());		uint16_t in_port = ntohs(rb.retrieve_ushort());				if (rb.error() || magicNumber != MAGIC_NUMBER) {			ERROR_LOG("Wrong magic number in packet received\n");			return NULL;					}		if ((in_addr == 0) && (in_port == 0)) {			in_addr = in_remoteNode.addr;			in_port = in_remoteNode.port;		}		return (new RetInfo(queryID, in_addr, in_port)); 			}		NodeIdent getInfoNode() {		NodeIdent tmp = {addr, port};		return tmp;	}		virtual int createRealPacket(RealPacket& inPacket) const {		inPacket.append_char(getPacketType());		write_id(inPacket);		inPacket.append_uint(htonl(addr));		inPacket.append_ushort(htons(port));				if (!inPacket.completeOkay()) { 			return -1; 		}		return 0; // Packet completed correctly			}			virtual char getPacketType() const	{ return RET_INFO; }	virtual ~RetInfo() {}		};class RetResponse : public Packet {private:	uint32_t 					addr;	// Address of solution node	uint16_t 				port;	// Port of solution node	vector<NodeIdentLat>	targets;public:	RetResponse(uint64_t id, uint32_t in_addr, uint16_t in_port,		const map<NodeIdent, uint32_t, ltNodeIdent>& in_targets) 			: Packet(id), addr(in_addr), port(in_port) {		map<NodeIdent, uint32_t, ltNodeIdent>::const_iterator it 			= in_targets.begin();		for (; it != in_targets.end(); it++) {						NodeIdentLat tmpIdent 				= {(it->first).addr, (it->first).port, it->second};			targets.push_back(tmpIdent);		}	}		NodeIdent getResponse() {		NodeIdent tmp = {addr, port};		return tmp;	}		const vector<NodeIdentLat>* getTargets() {		return &targets;		}		static RetResponse* parse(			const NodeIdent& in_remote, const char* buf, int numBytes) {		BufferWrapper rb(buf, numBytes);		char queryType = rb.retrieve_char();		if (rb.error() || queryType != RET_RESPONSE) {			ERROR_LOG("Wrong type received\n");			return NULL;			}		uint32_t queryID_1 = ntohl(rb.retrieve_uint());		uint32_t queryID_2 = ntohl(rb.retrieve_uint());		uint64_t queryID = to64(queryID_1, queryID_2);		uint32_t magicNumber = ntohl(rb.retrieve_uint());		if (rb.error() || magicNumber != MAGIC_NUMBER) {			ERROR_LOG("Wrong magic number in packet received\n");			return NULL;					}				uint32_t closestAddr = ntohl(rb.retrieve_uint());		uint16_t closestPort = ntohs(rb.retrieve_ushort());		if ((closestAddr == 0) && (closestPort == 0)) {			closestAddr = in_remote.addr;			closestPort = in_remote.port;		}		uint32_t numEntry = ntohl(rb.retrieve_uint());		map<NodeIdent, uint32_t, ltNodeIdent> tmpMap;						NodeIdent tmpIdent;				//while (!rb.error() && numEntry-- > 0) {		for (uint32_t i = 0; (!rb.error() && i < numEntry); i++) {						tmpIdent.addr = ntohl(rb.retrieve_uint());			tmpIdent.port = ntohs(rb.retrieve_ushort());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -