Advertisement
Guest User

Router.h/Router.cpp

a guest
Nov 10th, 2012
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.22 KB | None | 0 0
  1. //Router Head file
  2. #ifndef ROUTER_H
  3. #define ROUTER_H
  4.  
  5. #pragma comment( linker, "/defaultlib:ws2_32.lib" )
  6. #include <winsock2.h>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <time.h>
  10. #include <list>
  11. #include <stdio.h>
  12.  
  13. using namespace std ;
  14.  
  15. #define MAXBUFSIZE 2048             //maximum packet size
  16. #define MAXHOSTNAMELEN 256          //maximum length of host name
  17. #define ROUTER_PORT1 7000           //router port number 1
  18. #define ROUTER_PORT2 7001           //router port number 2
  19. #define PEER_PORT1 5000             //port number of peer host 1
  20. #define PEER_PORT2 5001             //port number of peer host 2
  21. #define TIMEOUT_USEC 300000         //time-out value
  22.  
  23. #define TRACE 1
  24.  
  25. struct EVENT_LIST
  26. {
  27.     bool empty;
  28.     DWORD count;                    //count is the packet number
  29.     short destination;              //destination of this packet
  30.     int len;                        //length of this packet
  31.     char Buffer[MAXBUFSIZE];        //buffer for packet
  32. };
  33.  
  34. class Router
  35. {
  36. public:
  37.     char localhost[MAXHOSTNAMELEN];     //local host name
  38.     //Constructor
  39.     Router(char *fn="log.txt");
  40.  
  41.     //Destructor
  42.     ~Router();
  43.  
  44.     void Run();
  45.  
  46. private:
  47.     ofstream fout;          //log file
  48.     float damage_rate, delay_rate;              //damage rate: dropped and delayed
  49.     SOCKET Sock1, Sock2;            //sockets used for communcation with peer host 1 and 2
  50.     EVENT_LIST FileBuf;     //buffer for delayed packets
  51.  
  52. protected:
  53.     SOCKADDR_IN sa_in_peer1;        // address structure for peer host 1 address
  54.     SOCKADDR_IN sa_in_peer2;        // address structure for peer host 2 address
  55.    
  56.     bool IsDamage() const;
  57.     bool IsDelayed() const;
  58.     void SendProc();
  59. };
  60.  
  61. #endif
  62.  
  63. //Router.cpp
  64. #include "Router.h"
  65. //////////////////////////////////////////////////////////
  66. //
  67. //  Router Constructor
  68. //  arguements:
  69. //      fn: A string of log file name
  70. //
  71. //////////////////////////////////////////////////////////
  72.  
  73. Router::Router(char *fn)        //Constructor
  74. {
  75.     WSADATA wsadata;
  76.     HOSTENT* hp;               
  77.     char peer_name1[MAXHOSTNAMELEN], peer_name2[MAXHOSTNAMELEN];
  78.     SOCKADDR_IN sa_in;
  79.  
  80.     FileBuf.empty=true;
  81.  
  82.     try
  83.     {                  
  84.         if (WSAStartup(0x0202,&wsadata)!=0)
  85.             throw "Error in starting WSAStartup()\n";
  86.     }
  87.  
  88.     //Display any needed error response.
  89.     catch (char *str) { cerr<<str<<":"<<dec<<WSAGetLastError()<<endl; return;}
  90.  
  91.     //Get Host name
  92.     gethostname(localhost,MAXHOSTNAMELEN);
  93.     cout<<"Router starting on host:"<<localhost<<endl<<flush;
  94.  
  95.     //Open the log file
  96.     fout.open(fn);
  97.  
  98.     try
  99.     {
  100.         //Create the Udp Sock1
  101.         if((Sock1 = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET)
  102.             throw "Create UDP Socket1 failed\n";
  103.  
  104.         //Fill-in UDP Port and Address info.
  105.         sa_in.sin_family = AF_INET;
  106.         sa_in.sin_port = htons(ROUTER_PORT1);
  107.         sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
  108.  
  109.         //Bind the UDP port1
  110.         if (bind(Sock1,(LPSOCKADDR)&sa_in,sizeof(sa_in)) == SOCKET_ERROR)
  111.             throw "can't bind the socket1";
  112.  
  113.         //Create the Udp Sock2
  114.         if((Sock2 = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET)
  115.             throw "Create UDP Socket2 failed\n";
  116.  
  117.         //Fill-in UDP Port and Address info.
  118.         sa_in.sin_family = AF_INET;
  119.         sa_in.sin_port = htons(ROUTER_PORT2);
  120.         sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
  121.  
  122.         //Bind the UDP port2
  123.         if (bind(Sock2,(LPSOCKADDR)&sa_in,sizeof(sa_in)) == SOCKET_ERROR)
  124.             throw "can't bind the socket2";
  125.  
  126.         cout<<"\nPlease enter the first peer host name:"<<flush;        //enter the dropping rate.
  127.         cin>>peer_name1;
  128.         cout<<"\nPlease enter the second peer host name:"<<flush;       //enter the dropping rate.
  129.         cin>>peer_name2;
  130.         cout<<"\nPlease enter the drop rate:"<<flush;       //enter the dropping rate.
  131.         cin>>damage_rate;
  132.         cout<<"\nPlease enter the delay rate:"<<flush;      //enter the dropping rate.
  133.         cin>>delay_rate;
  134.  
  135.         //creat peer host1
  136.         if((hp=gethostbyname(peer_name1)) == NULL)
  137.             throw "get server name failed\n";
  138.         memset(&sa_in_peer1,0,sizeof(sa_in_peer1));
  139.         memcpy(&sa_in_peer1.sin_addr,hp->h_addr,hp->h_length);
  140.         sa_in_peer1.sin_family = hp->h_addrtype;  
  141.         sa_in_peer1.sin_port = htons(PEER_PORT1);
  142.  
  143.         //creat peer host2
  144.         if((hp=gethostbyname(peer_name2)) == NULL)
  145.             throw "get client name failed\n";
  146.         memset(&sa_in_peer2,0,sizeof(sa_in_peer2));
  147.         memcpy(&sa_in_peer2.sin_addr,hp->h_addr,hp->h_length);
  148.         sa_in_peer2.sin_family = hp->h_addrtype;  
  149.         sa_in_peer2.sin_port = htons(PEER_PORT2);
  150.  
  151.         if (TRACE)
  152.         {
  153.             fout<<"Peer host 1: "<<peer_name1<<endl;
  154.             fout<<"Peer host 2: "<<peer_name2<<endl;
  155.             fout<<"Damage Rate: "<<damage_rate<<endl;
  156.         }
  157.     }      
  158.     catch (char *str) {cerr<<str<<":"<<dec<<WSAGetLastError()<<endl; exit(1);}
  159.  
  160.     srand( (unsigned)time( NULL ) );
  161. }
  162.  
  163. //////////////////////////////////////////////////////////
  164. //
  165. //  Router::IsDamage
  166. //      The function that generates random damages according to damage rate.
  167. //
  168. //////////////////////////////////////////////////////////
  169.  
  170. bool Router::IsDamage() const
  171. {  
  172.     return ( (((float)rand())/RAND_MAX) < ((float)damage_rate/100));
  173. }
  174.  
  175. //////////////////////////////////////////////////////////
  176. //
  177. //  Router::IsDelayed
  178. //      The function that generates random delayed according to delay rate.
  179. //
  180. //////////////////////////////////////////////////////////
  181.  
  182. bool Router::IsDelayed() const
  183. {  
  184.     return ( (((float)rand())/RAND_MAX) < ((float)delay_rate/100));
  185. }
  186.  
  187. //////////////////////////////////////////////////////////
  188. //
  189. //  Router::Run
  190. //      The function receives packets from peer hosts and forwards to destinations.  
  191. //      It also drops packets and stores delayed packets for future sending.
  192. //      It calls SendProc to send delayed packets.
  193. //
  194. //////////////////////////////////////////////////////////
  195.  
  196. void Router::Run()
  197. {
  198.     fd_set readfds;
  199.     struct timeval *tp=new timeval;
  200.     SOCKADDR from;
  201.     int RetVal, fromlen, recvlen, wait_count;
  202.     EVENT_LIST temp;
  203.     DWORD CurrentTime, count1, count2;
  204.  
  205.     count1=0;
  206.     count2=0;
  207.     wait_count=0;
  208.     tp->tv_sec=0;
  209.     tp->tv_usec=TIMEOUT_USEC;
  210.  
  211.     while (1)
  212.     {
  213.         try
  214.         {
  215.             FD_ZERO(&readfds);
  216.             FD_SET(Sock1,&readfds);
  217.             FD_SET(Sock2,&readfds);
  218.             fromlen=sizeof(from);
  219.             if((RetVal=select(1,&readfds,NULL,NULL,tp))==SOCKET_ERROR)  //check for incoming packets.
  220.                 throw "Timer error!";
  221.             else if(RetVal>0)   //There are incoming packets.
  222.             {
  223.                 if(!FileBuf.empty) wait_count++;
  224.                 if(FD_ISSET(Sock1, &readfds))   //incoming packet from peer host 1
  225.                 {
  226.                     if((recvlen=recvfrom(Sock1, temp.Buffer, sizeof(temp.Buffer), 0, &from, &fromlen))==SOCKET_ERROR)
  227.                         throw " Get buffer error!";
  228.                     if (TRACE)
  229.                     {
  230.                         fout<<"Router: Receive packet "<<count1<<" from peer host 1"<<endl;
  231.                         cout<<"Router: Receive packet "<<count1<<" from peer host 1"<<endl;
  232.                     }
  233.                     temp.count=count1;
  234.                     count1++;
  235.                     temp.destination=2;
  236.                 }
  237.                 else if(FD_ISSET(Sock2, &readfds))  //incoming packet from peer host 2
  238.                 {
  239.                     if((recvlen=recvfrom(Sock2, temp.Buffer, sizeof(temp.Buffer), 0, &from, &fromlen))==SOCKET_ERROR)
  240.                         throw " Get buffer error!";
  241.                     if (TRACE)
  242.                     {
  243.                         fout<<"Router: Receive packet "<<count2<<" from peer host 2"<<endl;
  244.                         cout<<"Router: Receive packet "<<count2<<" from peer host 2"<<endl;
  245.                     }
  246.                     temp.count=count2;
  247.                     count2++;
  248.                     temp.destination=1;
  249.                 }
  250.                 else continue;
  251.                 temp.len=recvlen;
  252.                 CurrentTime=GetTickCount();
  253.                 if(FileBuf.empty&&IsDelayed())      //if the packet is delayed.
  254.                 {
  255.                     FileBuf=temp;
  256.                     FileBuf.empty=false;
  257.                     if (TRACE)
  258.                     {
  259.                         fout<<"Router: Packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1)<<" has been delayed!"<<endl;
  260.                         cout<<"Router: Packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1)<<" has been delayed!"<<endl;
  261.                     }
  262.                 }
  263.                 else if(IsDamage()) //if the packet is dropped: dropping packet by no forwarding the packet.
  264.                 {
  265.                     if (TRACE)
  266.                     {
  267.                         fout<<"Router: Packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1)<<" has been dropped by router!"<<endl;
  268.                         cout<<"Router: Packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1)<<" has been dropped by router!"<<endl;
  269.                     }
  270.                 }
  271.                 else        //otherwise, packet is forwarded to destination
  272.                 {
  273.                     if(temp.destination==1) //forward packets received from 2 to 1.
  274.                     {
  275.                         if(sendto(Sock1, temp.Buffer, temp.len,0,(SOCKADDR*)&sa_in_peer1,sizeof(sa_in_peer1))==SOCKET_ERROR)
  276.                             throw "Send packet error!";
  277.                         if (TRACE)
  278.                         {
  279.                             fout<<"Router: Send packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1) <<" to host "<<temp.destination<<endl;
  280.                             cout<<"Router: Send packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1) <<" to host "<<temp.destination<<endl;
  281.                         }
  282.                         if(!FileBuf.empty&&FileBuf.destination==1)
  283.                         {
  284.                             wait_count=0;
  285.                             SendProc();
  286.                         }
  287.                     }
  288.                     else
  289.                     {   //forward packets received from 1 to 2.
  290.                         if(sendto(Sock2, temp.Buffer, temp.len,0,(SOCKADDR*)&sa_in_peer2,sizeof(sa_in_peer2))==SOCKET_ERROR)
  291.                             throw "Send packet error1";
  292.                         if (TRACE)
  293.                         {
  294.                             fout<<"Router: Send packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1) <<" to host "<<temp.destination<<endl;
  295.                             cout<<"Router: Send packet "<<temp.count<<" received from peer host "<<(temp.destination==1?2:1) <<" to host "<<temp.destination<<endl;
  296.                         }
  297.                         if(!FileBuf.empty&&FileBuf.destination==2)
  298.                         {
  299.                             wait_count=0;
  300.                             SendProc();
  301.                         }
  302.                     }  
  303.                 }
  304.             }
  305.             else //If there is no incoming packet and there is a delayed packets storing in buffer for 3 cycle times (about 0.9 second), call SendProc to send delayed packet.
  306.             {      
  307.                 if(!FileBuf.empty)
  308.                 {
  309.                     wait_count++;
  310.                     if(wait_count>=3)
  311.                     {
  312.                         SendProc();
  313.                         wait_count=0;
  314.                     }
  315.                 }
  316.             }
  317.         } //end of try
  318.         catch(char *str) {cerr<<str<<":"<<dec<<WSAGetLastError()<<endl;}
  319.     }//end of while
  320. }
  321.  
  322. //////////////////////////////////////////////////////////
  323. //
  324. //  Router::SendProc
  325. //      Send delayed packets to the destinations.
  326. //
  327. //////////////////////////////////////////////////////////
  328.  
  329. void Router::SendProc()
  330. {
  331.     try
  332.     {
  333.         if(FileBuf.destination==1)
  334.         {
  335.             if(sendto(Sock1, FileBuf.Buffer, FileBuf.len,0,(SOCKADDR*)&sa_in_peer1,sizeof(sa_in_peer1))==SOCKET_ERROR)
  336.                 throw "Send packet error!";
  337.         }
  338.         else
  339.         {
  340.             if(sendto(Sock2, FileBuf.Buffer, FileBuf.len,0,(SOCKADDR*)&sa_in_peer2,sizeof(sa_in_peer2))==SOCKET_ERROR)
  341.                 throw "Send packet error!";
  342.         }
  343.         if (TRACE)
  344.         {
  345.             fout<<"Router: Send delayed packet "<<FileBuf.count<<" received from peer host "<<(FileBuf.destination==1?2:1)<<" to host "<<FileBuf.destination<<endl;
  346.             cout<<"Router: Send delayed packet "<<FileBuf.count<<" received from peer host "<<(FileBuf.destination==1?2:1)<<" to host "<<FileBuf.destination<<endl;
  347.         }
  348.     }
  349.     catch(char *str){cerr<<str<<":"<<dec<<WSAGetLastError()<<endl;}
  350.     FileBuf.empty=true;
  351. }
  352.  
  353. //////////////////////////////////////////////////////////
  354. //
  355. //  Router Destructor
  356. //  arguements:
  357. //      fn: A string of log file name
  358. //
  359. //////////////////////////////////////////////////////////
  360.  
  361. Router :: ~Router()
  362. {
  363.     closesocket(Sock1);
  364.     closesocket(Sock2);
  365.  
  366.     /* When done, uninstall winsock.dll (WSACleanup()) and exit */
  367.     WSACleanup();  
  368.  
  369.     //close log file
  370.     fout.close();
  371. }
  372.  
  373. //////////////////////////////////////////////////////////
  374. //
  375. //  Main function
  376. //
  377. //////////////////////////////////////////////////////////
  378.  
  379. void main()
  380. {
  381.     Router router;
  382.     router.Run();
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement