DatStorm

Untitled

May 8th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. package sendBackAndForth;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.*;
  8. import java.nio.ByteBuffer;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12.  
  13. public class OpSysClientSendRecieve {
  14.     public static int B = 1000;
  15.     private BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
  16.     private int serverport = 5432;
  17.     private int bufsize = 512;
  18.     private int MAX = (int) Math.floor(Math.pow(2, 4 * 8) - 1);
  19.     private int R = (int) Math.floor(Math.random() * MAX);
  20.     private boolean[] ackArray;
  21.     private boolean init = true;
  22.  
  23.  
  24.     /**
  25.      * VÆR OPMÆRKSOM PÅ
  26.      * timeout
  27.      * package lost
  28.      */
  29.     static public void main(String args[]) {
  30.         new OpSysClientSendRecieve().run(args);
  31.     }
  32.  
  33.     private void run(String[] args) {
  34.         String desthost = "localhost";
  35.  
  36.         if (args.length >= 1) {
  37.             desthost = args[0];
  38.         }
  39.  
  40.         InetAddress dest; // DNS query
  41.         DatagramSocket s = null;
  42.         try {
  43.             dest = InetAddress.getByName(desthost);
  44.             s = new DatagramSocket();
  45.             System.err.println("Our own port is " + s.getLocalPort());
  46.             System.out.println("R IS: " + R);
  47.  
  48.             DatagramPacket receiveMsg = new DatagramPacket(new byte[bufsize], bufsize);
  49.             /*************************************************
  50.              *                     LOOP
  51.              *************************************************/
  52.             while (true) {
  53.                 handleSend(s, dest);
  54.                 handleReceive(s, receiveMsg);
  55.             } // while
  56.         } catch (IOException e) {
  57.             e.printStackTrace();
  58.         } finally {
  59.             if (s != null) {
  60.                 s.close();
  61.             }
  62.         }
  63.     }
  64.  
  65.  
  66.     private void handleSend(DatagramSocket s, InetAddress dest) throws IOException {
  67.         Path path = Paths.get("/home/datstorm/IdeaProjects/Opsys-BFTP/src/testerFiles/test.txt");
  68.         File file =new File("/home/datstorm/IdeaProjects/Opsys-BFTP/src/testerFiles/test.txt");
  69.  
  70.         if(file.exists()) {
  71.             long S = file.length();
  72.             byte[] fileInBytes = Files.readAllBytes(path);
  73.  
  74.  
  75. //            long S = fileInBytes.length;
  76.             if (init) {
  77.                 int blocksOfSizeB = (int) (Math.ceil((double) S / B)); // FIXME Removed -1 HERE
  78.                 ackArray = new boolean[blocksOfSizeB];
  79.                 init = false;
  80.             }
  81.  
  82.  
  83.             // TODO: SEND ONLY NOT SENT PACKAGES
  84.             int T = (int) (Math.ceil((double) S / B) ); // FIXME ADD -1
  85. //            int blockNum = 0;
  86.             ByteBuffer bb = ByteBuffer.wrap(fileInBytes);
  87.             for (int blockNum = 0; blockNum < T; blockNum++) {
  88.                 byte b = bb.get(blockNum);
  89.  
  90.                 // We have recieved ACK here
  91.                 if (ackArray[blockNum]) {
  92.                     continue;
  93.                 }
  94.                 //Creating packet
  95.                 ByteBuffer bufAllocate = ByteBuffer.allocate(16 + B);
  96.                 bufAllocate.putInt(R);
  97.                 bufAllocate.putLong(S);
  98.                 bufAllocate.putInt(blockNum);
  99.                 bufAllocate.put(b);
  100.                 DatagramPacket sendMsg = new DatagramPacket(bufAllocate.array(), bufAllocate.capacity(), dest, serverport);
  101.                 s.send(sendMsg);
  102.             }
  103.         }
  104.  
  105.     }
  106.  
  107.     private void handleReceive(DatagramSocket s, DatagramPacket receive) throws IOException {
  108.         receive.setLength(bufsize); // max received packet size
  109.         s.receive(receive); // the actual receive operation
  110.  
  111.         ByteBuffer buffer = ByteBuffer.wrap(receive.getData());
  112.         int recieveR = buffer.getInt();
  113.         int receiveI = buffer.getInt();
  114.         // we got our ACK
  115.         if (recieveR == R) {
  116.             ackArray[receiveI] = true;
  117.             System.out.println("R: " + recieveR + ". i: " + receiveI);
  118.         }
  119.     }
  120.  
  121. }
  122.  
  123.  
  124.  
  125. /* FRAKLIP
  126.  
  127.     //        String buf;
  128. //        buf = bin.readLine();
  129. //        if (buf == null) {
  130. //            buf = "null"; // user typed EOF character
  131. //        }
  132. //        buf = buf + "\n"; // append newline character
  133. //        int slen = buf.length();
  134. //        byte[] bbuf = buf.getBytes();
  135. //        msg.setData(bbuf);
  136. //        msg.setLength(slen);
  137.  
  138.  
  139.  */
Advertisement
Add Comment
Please, Sign In to add comment