Abdelkader Ouda page 1 Winter 2020WESTERN UNIVERSITY - CANADAFACULTY OF ENGINEERINGDEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERINGSE 3314B – NETWORK NETWORKS APPLICATIONSAssignment 2: A Peer NodeDue Date: March 9, 2020This assignment is to be done individually. Cheating is prohibited in this assignment, that is not to showyour code to any other student, do not look at any other students code, and do not put your code in anypublic domain. However, getting help is easy and available, just ask the instructor or the TAs. At the sametime, you are encouraged to discuss your problems (if any) with other students in general terms but theactual program design and code must be your own.Please note that the consequences of cheating are much, much worse than getting a low mark for thatparticular item of work. The very best case is that you get a zero for the whole thing. Keep in mind thatturning in your own work would have at least gotten you a few marks, better than a zero.1. ObjectivesThe majority of socket programs, including GetImage/imageDB of assignment 1, follows the client-serverparadigm, where a server waits on a well-known port for clients connections. In this assignment, youll explorepeer-to-peer programming. You will be re-using most of the functions you built in assignment1 to create anew program for the peer. If you didnt manage to get these functions to work in assignment1, no problemyou still have the chance to make your new code works in this assignment. A peer is basically both a serverand a client. It accepts connections from other peers and also connects to one or more peers.2. IntroductionThe peer program takes three optional arguments on the command line:> node peer [-p : -n -v ]The -p option tells the peer program which peer to connect to initially. If this option is not provided, thepeer starts as a server listening on a random, ephemeral port. The -n option allows the user to set a peersmaximum peering relationships is equal to 2. The -v option workssimilarly to the same option for GetImage in assignment1.To bootstrap the peer-to-peer (p2p) network, we first start a peer by itself. Every time a peer runs, it printsits IP address and port number it is listening on. When a peer is run with the IPaddress:port of another peeras its command line argument, the new peer tries to join the provided peer in the p2p network by creating asocket and connecting to the peer.Abdelkader Ouda page 2 Winter 2020A peer that receives a join request will accept the peer if and only if its peer table is not full. Whether ajoin request is accepted or not, the peer sends back to the requesting peer the IPaddress:port of a peer that issaved in its peer table (if the table is not empty). This will help the newly joined peer find more peers to join.Take a step back and look at the big picture, see how code implemented in two different programs inassignment1 are now residing in the same program and how this program is serving the role of both a clientand a server. In this assignment, a custom Peer-to-Peer Transport Protocol (cPTP) for peer communicationwill be built and used. The subsequent sections describe the structure and mechanism of this protocol.The main goal of this assignment is to gain an early experience with protocol design as you are designinga simple peer-to-peer join protocol, with redirection.3. Task 1: Server SideYour first task is to implement the server side of the peer program. If peer is run without any option onthe command line, reuse your imageDB server code to announce for its IPaddress and port number and beready to accept other peers join requests.If a new peer is trying to connect to this peer and this peers peering table isnot full, handleClientJoining(sock) should accept the connection and then send back a message packet (seeFigure 1) with field set to 1, (1 means Welcome). The new peer is thenstored in the peer table. On the other hand, if the peer table is full, handleClientJoining(sock) is invoked againto receive the request but it sends back a redirect message packet -direct). All times it fills in the fields of the message: V must be set to 3314 (only in this assignment). Theholds the number of peers attached to the cPTP message packet.The figure below shows the cPTP message packet structure.00 1 2 3 4 5 6 7 8 910 1 2 3 4 5 6 7 8 920 1 2 3 4 5 6 7 8 930 1V = 3314 Message TypeSenderNumber of peersReserved Peer port numberPeer IPv4 addressFigure 1: The cPTP message packet formatThis structure stores the cPTP message type isone byte, it is Welcome Re- . The sender ID is 4 bytes, thebytes, holds the number of peers included in the message pacassignment we allow each peer a maximum of 2 partners). Followed by 2 reserved bytes, not used in this4 bytes. Although each peer may have up to 2 partners, we store the IPaddress and port number of only onejoined peer.4. Task 2: Client SideOnly ifNumber ofpeers is notzeroAbdelkader Ouda page 3 Winter 2020If a peer is run with the -p option, the user must provide a known peer IP address and port number toconnect to, with the port number separated from the peer IP address by a colon. Keep in mind that, a peerjoining a p2p network simply connects to another peer, without sending any messages, so using a differentversion number with t代做SE 3314B留学生作业、代做Java编程语言作业、代写Python,c/c++课程作业 调试Matlab程序|代he -v command line option will not affect the join process.Note that Node.js would have assigned a random, ephemeral source port to the connected socket. Findout the assigned ephemeral source port number and store it along with the IPv4 address of the current host.Note also that, the program will be managing for activities on both the socket connected to the known peerand the socket on which youre listening for connection from other peers.The peer program checks for activity on each connected peers socket. If theres an incoming packet, itreceives the message and examine it packet. It first checks the version number of the received packet. If it isnot , the message will be ignored. Assuming the version number checks out, the receipt of a packetcarrying another peer causes the third peers address and port number to be printed out. If the received packetis of type 2, it printed out that the join has been declined (redirected) and exits the program. The user canthen manually try to connect to the third peer returned in the redirect packet by running the peer programagain.Youre not required to handle peer leaving the p2p network: once a peer departs, its partner peer is notrequired to clean up its peer table and be ready to accept another peer. You can assume that a peer is only torndown when the whole p2p network is being torn down. It is required, however, that when a peer leaves, itspartner does not crash.5. Sample execution runBelow is a running scenario as an example to show the execution of the program and to summarize therequirements of this assignment. After finish building the program peer, save it in four different workingfolders, p1, p2, p3, and p4. Open four command line windows, one for each of these created folders.1. On the first window, run peer program without any command line argument:p1> node peerIt should print to screen (with a different port number, depicted in bold here), not that the folder nameinformation, the program should recognize it automatically:This peer address is 127.0.0.1: 43945 located at p12. On the second window, run peer with the following command line argument (replacing the portnumber with the one printed out on the first item above:p2> node peer -p 127.0.0.1:43945It should print to screen (with different port numbers and time stamp):Connected to peer p1:43945 at timestamp: 457This peer address is 127.0.0.1:56535 located at p2Abdelkader Ouda page 4 Winter 2020Received ack from p1:43945Meanwhile, on the first window, you should see the following additional line printed to screen:This peer address is 127.0.0.1: 43945 located at p1Connected from peer 127.0.0.1:565353. On the third window, run peer with the following command line argument (replacing the portnumber with the one from the first item above)p3> node peer -p 127.0.0.1:43945It should print to screen (with different port numbers and time stamp):Connected to peer p1:43945 at timestamp: 492This peer address is 127.0.0.1:48141 located at p3Received ack from p1:43945 which is peered with: 127.0.0.1:56535Meanwhile, on the first window, you should see the following additional line printed to screen:This peer address is 127.0.0.1: 43945 located at p1Connected from peer 127.0.0.1:56535Connected from peer 127.0.0.1:481414. On the fourth window, run peer with the following command line argument (replacing the portnumber with the one from the first item above)P4> node peer -p 127.0.0.1:43945It should print to screen (with different port numbers and time stamp):Connected to peer p1:43945 at timestamp: 712This peer address is 127.0.0.1:40231 located at p4Received ack from p1:43945 which is peered with: 127.0.0.1:56535Abdelkader Ouda page 5 Winter 2020Join redirected, try to connect to the peer above.Meanwhile, on the first window, you should see the following additional line printed to screen:This peer address is 127.0.0.1: 43945 located at p1Connected from peer 127.0.0.1:56535Connected from peer 127.0.0.1:48141Peer table full: 127.0.0.1:40231 redirected5. Staying on the fourth window, run peer again with the following command line argument (replacingthe port number with the one from the fourth step item above)P4> node peer -p 127.0.0.1:56535It should print to screen (with different port numbers):Connected to peer p2:56535 at timestamp: 1090This peer address is 127.0.0.1:50095 located at p4Received ack from p2:56535 which is peered with: 127.0.0.1:43945Meanwhile, on the second window on p2, you should see the following additional line printed to screen:Connected to peer 127.0.0.1:43945 at timestamp: 457This peer address is 127.0.0.1:56535 located at p2Received ack from 127.0.0.1:43945Connected from peer 127.0.0.1:50095That ends our sample execution/test scenario and you can quit all four peers.The above is a very simple test case to check that your peers are communicating with each other. Youshould further test your p2p network with other test cases of your own. Recall that a peer is not required toaccept another peer if its partner departs.Hand InSubmit the final version of your source code (Fully commented and formatted) through OWL by the duedate mentioned above. This should include one compressed achieve file (zip file) having all JS files for the peerprogram including the package.json file and name it yourUWOID-SE3314b-assignment2.zip.转自:http://www.6daixie.com/contents/3/4931.html