Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package protocol;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import client.*;
- public class BetterProtocol extends IRDTProtocol {
- // change the following as you wish:
- static final int HEADERSIZE=1; // number of header bytes in each packet
- static final int DATASIZE=128; // max. number of user data bytes in each packet
- Map<Integer, Integer[]> packets = new HashMap<Integer, Integer[]>();
- @Override
- public void sender() {
- System.out.println("Sending...");
- // read from the input file
- Integer[] fileContents = Utils.getFileContents(getFileID());
- // keep track of where we are in the data
- int filePointer = 0;
- // create a new packet of appropriate size
- int i = 0;
- while (filePointer <= fileContents.length) {
- int datalen = Math.min(DATASIZE, fileContents.length - filePointer);
- Integer[] pkt = new Integer[HEADERSIZE + datalen];
- // write something random into the header byte
- pkt[0] = i;
- // copy databytes from the input file into data part of the packet, i.e., after the header
- System.arraycopy(fileContents, filePointer, pkt, HEADERSIZE, datalen);
- packets.put(i, pkt);
- client.Utils.Timeout.SetTimeout(10000, this, i);
- // send the packet to the network layer
- getNetworkLayer().sendPacket(pkt);
- System.out.println("Sent one packet with header="+pkt[0]);
- boolean stop = false;
- while (!stop) {
- try {
- Integer[] acknowledgement = getNetworkLayer().receivePacket();
- if (acknowledgement != null && acknowledgement[0] == i) {
- packets.remove(i);
- stop = true;
- System.out.println("Packet with header="+pkt[0]+" has been received!");
- } else {
- Thread.sleep(10);
- }
- } catch (InterruptedException e) {
- stop = true;
- }
- }
- filePointer += DATASIZE;
- i++;
- }
- }
- @Override
- public void TimeoutElapsed(Object tag) {
- int z=(Integer)tag;
- System.out.println("Timer expired with tag="+z);
- if (packets.containsKey(z)) {
- client.Utils.Timeout.SetTimeout(10000, this, z);
- getNetworkLayer().sendPacket(packets.get(z));
- System.out.println("Resent packet " + z);
- }
- }
- @Override
- public void receiver() {
- System.out.println("Receiving...");
- // create the array that will contain the file contents
- // note: we don't know yet how large the file will be, so the easiest (but not most efficient)
- // is to reallocate the array every time we find out there's more data
- Integer[] fileContents = new Integer[0];
- // loop until we are done receiving the file
- boolean stop = false;
- while (!stop) {
- // try to receive a packet from the network layer
- Integer[] packet = getNetworkLayer().receivePacket();
- // if we indeed received a packet
- if (packet != null) {
- // tell the user
- System.out.println("Received packet, length="+packet.length+" first byte="+packet[0] );
- // append the packet's data part (excluding the header) to the fileContents array, first making it larger
- int oldlength=fileContents.length;
- int datalen= packet.length - HEADERSIZE;
- fileContents = Arrays.copyOf(fileContents, oldlength+datalen);
- System.arraycopy(packet, HEADERSIZE, fileContents, oldlength, datalen);
- // and let's just hope the file is now complete
- stop=true;
- }else{
- // wait ~10ms (or however long the OS makes us wait) before trying again
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- stop = true;
- }
- }
- }
- // write to the output file
- Utils.setFileContents(fileContents, getFileID());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement