📄 or.cc
字号:
#include "or.h"#include <stdlib.h>//Nexthop is the end ,use -1 instead! in fact,-1 means BROADCAST#define END -1int hdr_or::offset_;static class OrHeaderClass : public PacketHeaderClass {public: OrHeaderClass() : PacketHeaderClass("PacketHeader/Or", sizeof(hdr_or)) { bind_offset(&hdr_or::offset_); }} class_rohdr;static class OrClass : public TclClass {public: OrClass() : TclClass("Agent/Or") {} TclObject* create(int, const char*const*) { return (new OrAgent()); }} class_or;OrAgent::OrAgent() : Agent(PT_OR), seq(0){ bind("packetSize_", &size_);}int OrAgent::command(int argc, const char*const* argv){ if (argc == 2) { if (strcmp(argv[1], "send") == 0) { // Create a new packet Packet* pkt = allocpkt(); // Access the Or header for the new packet: hdr_or* hdr = hdr_or::access(pkt); hdr_ip* hdrip = hdr_ip::access(pkt); hdr->seq = seq++; hdr->nexthop = END; hdr->next = NULL; printf("Node %d :\n",(int)hdrip->saddr()); printf("seq-%d has been sent!\n\n",hdr->seq); // Store the current time in the 'send_time' field hdr->send_time = Scheduler::instance().clock(); // Send the packet send(pkt, 0); // return TCL_OK, so the calling function knows that // the command has been processed return (TCL_OK); } else if (strcmp(argv[1], "start-WL-brdcast") == 0) { Packet* pkt = allocpkt(); hdr_ip* iph = HDR_IP(pkt); iph->daddr() = IP_BROADCAST; iph->dport() = iph->sport(); printf("Node %d :\n",(int)iph->saddr()); printf("IP_BROADCAST start!\n\n"); send(pkt, (Handler*) 0); return (TCL_OK); } } else if (argc == 3 && strcmp(argv[1], "send") == 0) { // Create a new packet Packet* pkt = allocpkt(); // Access the Or header for the new packet: hdr_or* hdr = hdr_or::access(pkt); hdr_ip* hdrip = hdr_ip::access(pkt); hdr->seq = seq++; hdr->nexthop = atoi(argv[2]); struct hdr_or* next_or_pkt = (struct hdr_or *) malloc(sizeof(struct hdr_or)); hdr->next = next_or_pkt; hdr->next->nexthop = END; hdr->next->next = NULL; printf("Node %d :\n",(int)hdrip->saddr()); printf("seq-%d has been sent!\n\n",hdr->seq); // Store the current time in the 'send_time' field hdr->send_time = Scheduler::instance().clock(); // Send the packet send(pkt, 0); // return TCL_OK, so the calling function knows that // the command has been processed return (TCL_OK); } else if (argc == 4 && strcmp(argv[1], "send") == 0) { // Create a new packet Packet* pkt = allocpkt(); // Access the Or header for the new packet: hdr_or* hdr = hdr_or::access(pkt); hdr_ip* hdrip = hdr_ip::access(pkt); hdr->seq = seq++; hdr->nexthop = atoi(argv[2]); struct hdr_or* next_or_pkt_1 = (struct hdr_or *) malloc(sizeof(struct hdr_or)); hdr->next = next_or_pkt_1; hdr->next->nexthop = atoi(argv[3]); struct hdr_or* next_or_pkt_2 = (struct hdr_or *) malloc(sizeof(struct hdr_or)); hdr->next->next = next_or_pkt_2; hdr->next->next->nexthop = END; hdr->next->next->next = NULL; printf("Node %d :\n",(int)hdrip->saddr()); printf("seq-%d has been sent!\n\n",hdr->seq); // Store the current time in the 'send_time' field hdr->send_time = Scheduler::instance().clock(); // Send the packet send(pkt, 0); // return TCL_OK, so the calling function knows that // the command has been processed return (TCL_OK); } else if (argc == 5 && strcmp(argv[1], "send") == 0) {///////////////////////////////// // Create a new packet Packet* pkt = allocpkt(); // Access the Or header for the new packet: hdr_or* hdr = hdr_or::access(pkt); hdr_ip* hdrip = hdr_ip::access(pkt); hdr->seq = seq++; hdr->nexthop = atoi(argv[2]); struct hdr_or* next_or_pkt_1 = (struct hdr_or *) malloc(sizeof(struct hdr_or)); hdr->next = next_or_pkt_1; hdr->next->nexthop = atoi(argv[3]); struct hdr_or* next_or_pkt_2 = (struct hdr_or *) malloc(sizeof(struct hdr_or)); hdr->next->next = next_or_pkt_2; hdr->next->next->nexthop = atoi(argv[4]); struct hdr_or* next_or_pkt_3 = (struct hdr_or *) malloc(sizeof(struct hdr_or)); hdr->next->next->next = next_or_pkt_3; hdr->next->next->next->nexthop = END; hdr->next->next->next->next = NULL; printf("Node %d :\n",(int)hdrip->saddr()); printf("seq-%d has been sent!\n\n",hdr->seq); // Store the current time in the 'send_time' field hdr->send_time = Scheduler::instance().clock(); // Send the packet send(pkt, 0); // return TCL_OK, so the calling function knows that // the command has been processed return (TCL_OK); } // If the command hasn't been processed by OrAgent()::command, // call the command() function for the base class return (Agent::command(argc, argv));}void OrAgent::recv(Packet* pkt, Handler*){ // Access the IP header for the received packet: hdr_ip* hdrip = hdr_ip::access(pkt); // Access the Or header for the received packet: hdr_or* hdr = hdr_or::access(pkt); // Create a new packet Packet* pktfwd = allocpkt(); // Access the Or header for the new packet: hdr_or* hdrfwd = hdr_or::access(pktfwd); hdr_ip* hdrfwdip = hdr_ip::access(pktfwd); printf("Node %d:\n",(int)hdrfwdip->saddr()); int src=hdrip->saddr(); int pktsize = hdr_cmn::access(pkt)->size()+size_of_packet(pkt);
printf("Received seq-%d from node %d! size: %d\n",hdr->seq,src,pktsize); // check if in brdcast mode if ((u_int32_t)hdrip->daddr() == IP_BROADCAST) { Packet::free(pkt); // add brdcast address hdrfwdip->daddr() = IP_BROADCAST; hdrfwdip->dport() = hdrfwdip->sport(); printf("IP_BROADCAST reply!\n\n"); send(pktfwd, 0); return; } else if (hdr->nexthop!=END) { // Set the send_time field to the correct value hdrfwd->send_time = hdr->send_time; hdrfwd->rcv_time = Scheduler::instance().clock(); hdrfwd->seq = hdr->seq; hdrfwd->nexthop = hdr->next->nexthop; hdrfwd->next = hdr->next->next; printf("Cost time: %3.1f ms!\n",(hdrfwd->rcv_time - hdrfwd->send_time) * 1000); printf("Nexthop is Node %d\n\n",hdr->nexthop); // Discard the packet Packet::free(pkt); // Send the packet send(pktfwd, 0); return; } printf("This is the Ending Node!\n\n"); hdrfwd->rcv_time = Scheduler::instance().clock(); printf("Or Routing of seq-%d is finish! Cost time: %3.1f ms!\n\n",hdr->seq,(hdrfwd->rcv_time - hdr->send_time) * 1000); printf("************************\n\n"); // Discard the packet Packet::free(pktfwd); Packet::free(pkt); return;}int OrAgent::size_of_packet(Packet* pkt){ // Access the Or header for the received packet: hdr_or* hdr = hdr_or::access(pkt); int size = sizeof(struct hdr_or); int sum = size; while(hdr->next!=NULL) { sum+=size; hdr = hdr->next; } return sum;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -