Advertisement
Guest User

Code

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package protocol;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import client.*;
  8.  
  9. public class BetterProtocol extends IRDTProtocol {
  10.  
  11.     // change the following as you wish:
  12.     static final int HEADERSIZE=1;   // number of header bytes in each packet
  13.     static final int DATASIZE=128;   // max. number of user data bytes in each packet
  14.     Map<Integer, Integer[]> packets = new HashMap<Integer, Integer[]>();
  15.  
  16.     @Override
  17.     public void sender() {
  18.         System.out.println("Sending...");
  19.  
  20.         // read from the input file
  21.         Integer[] fileContents = Utils.getFileContents(getFileID());
  22.  
  23.         // keep track of where we are in the data
  24.         int filePointer = 0;
  25.  
  26.         // create a new packet of appropriate size
  27.         int i = 0;
  28.         while (filePointer <= fileContents.length)  {
  29.             int datalen = Math.min(DATASIZE, fileContents.length - filePointer);
  30.             Integer[] pkt = new Integer[HEADERSIZE + datalen];
  31.             // write something random into the header byte
  32.             pkt[0] = i;
  33.             // copy databytes from the input file into data part of the packet, i.e., after the header
  34.             System.arraycopy(fileContents, filePointer, pkt, HEADERSIZE, datalen);
  35.             packets.put(i, pkt);
  36.             client.Utils.Timeout.SetTimeout(10000, this, i);
  37.             // send the packet to the network layer
  38.             getNetworkLayer().sendPacket(pkt);
  39.             System.out.println("Sent one packet with header="+pkt[0]);
  40.             boolean stop = false;
  41.             while (!stop) {
  42.                 try {
  43.                     Integer[] acknowledgement = getNetworkLayer().receivePacket();
  44.                     if (acknowledgement != null && acknowledgement[0] == i) {
  45.                         packets.remove(i);
  46.                         stop = true;
  47.                         System.out.println("Packet with header="+pkt[0]+" has been received!");
  48.                     } else  {
  49.                         Thread.sleep(10);
  50.                     }
  51.                 } catch (InterruptedException e) {
  52.                     stop = true;
  53.                 }
  54.             }
  55.             filePointer += DATASIZE;
  56.             i++;
  57.         }
  58.  
  59.     }
  60.  
  61.     @Override
  62.     public void TimeoutElapsed(Object tag) {
  63.         int z=(Integer)tag;
  64.         System.out.println("Timer expired with tag="+z);
  65.         if (packets.containsKey(z)) {
  66.             client.Utils.Timeout.SetTimeout(10000, this, z);
  67.             getNetworkLayer().sendPacket(packets.get(z));
  68.             System.out.println("Resent packet " + z);
  69.         }
  70.     }
  71.  
  72.     @Override
  73.     public void receiver() {
  74.         System.out.println("Receiving...");
  75.  
  76.         // create the array that will contain the file contents
  77.         // note: we don't know yet how large the file will be, so the easiest (but not most efficient)
  78.         //   is to reallocate the array every time we find out there's more data
  79.         Integer[] fileContents = new Integer[0];
  80.  
  81.         // loop until we are done receiving the file
  82.         boolean stop = false;
  83.         while (!stop) {
  84.  
  85.             // try to receive a packet from the network layer
  86.             Integer[] packet = getNetworkLayer().receivePacket();
  87.  
  88.             // if we indeed received a packet
  89.             if (packet != null) {
  90.  
  91.                 // tell the user
  92.                 System.out.println("Received packet, length="+packet.length+"  first byte="+packet[0] );
  93.  
  94.                 // append the packet's data part (excluding the header) to the fileContents array, first making it larger
  95.                 int oldlength=fileContents.length;
  96.                 int datalen= packet.length - HEADERSIZE;
  97.                 fileContents = Arrays.copyOf(fileContents, oldlength+datalen);
  98.                 System.arraycopy(packet, HEADERSIZE, fileContents, oldlength, datalen);
  99.  
  100.                 // and let's just hope the file is now complete
  101.                 stop=true;
  102.  
  103.             }else{
  104.                 // wait ~10ms (or however long the OS makes us wait) before trying again
  105.                 try {
  106.                     Thread.sleep(10);
  107.                 } catch (InterruptedException e) {
  108.                     stop = true;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         // write to the output file
  114.         Utils.setFileContents(fileContents, getFileID());
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement