Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sendBackAndForth;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.*;
- import java.nio.ByteBuffer;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- public class OpSysClientSendRecieve {
- public static int B = 1000;
- private BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
- private int serverport = 5432;
- private int bufsize = 512;
- private int MAX = (int) Math.floor(Math.pow(2, 4 * 8) - 1);
- private int R = (int) Math.floor(Math.random() * MAX);
- private boolean[] ackArray;
- private boolean init = true;
- /**
- * VÆR OPMÆRKSOM PÅ
- * timeout
- * package lost
- */
- static public void main(String args[]) {
- new OpSysClientSendRecieve().run(args);
- }
- private void run(String[] args) {
- String desthost = "localhost";
- if (args.length >= 1) {
- desthost = args[0];
- }
- InetAddress dest; // DNS query
- DatagramSocket s = null;
- try {
- dest = InetAddress.getByName(desthost);
- s = new DatagramSocket();
- System.err.println("Our own port is " + s.getLocalPort());
- System.out.println("R IS: " + R);
- DatagramPacket receiveMsg = new DatagramPacket(new byte[bufsize], bufsize);
- /*************************************************
- * LOOP
- *************************************************/
- while (true) {
- handleSend(s, dest);
- handleReceive(s, receiveMsg);
- } // while
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (s != null) {
- s.close();
- }
- }
- }
- private void handleSend(DatagramSocket s, InetAddress dest) throws IOException {
- Path path = Paths.get("/home/datstorm/IdeaProjects/Opsys-BFTP/src/testerFiles/test.txt");
- File file =new File("/home/datstorm/IdeaProjects/Opsys-BFTP/src/testerFiles/test.txt");
- if(file.exists()) {
- long S = file.length();
- byte[] fileInBytes = Files.readAllBytes(path);
- // long S = fileInBytes.length;
- if (init) {
- int blocksOfSizeB = (int) (Math.ceil((double) S / B)); // FIXME Removed -1 HERE
- ackArray = new boolean[blocksOfSizeB];
- init = false;
- }
- // TODO: SEND ONLY NOT SENT PACKAGES
- int T = (int) (Math.ceil((double) S / B) ); // FIXME ADD -1
- // int blockNum = 0;
- ByteBuffer bb = ByteBuffer.wrap(fileInBytes);
- for (int blockNum = 0; blockNum < T; blockNum++) {
- byte b = bb.get(blockNum);
- // We have recieved ACK here
- if (ackArray[blockNum]) {
- continue;
- }
- //Creating packet
- ByteBuffer bufAllocate = ByteBuffer.allocate(16 + B);
- bufAllocate.putInt(R);
- bufAllocate.putLong(S);
- bufAllocate.putInt(blockNum);
- bufAllocate.put(b);
- DatagramPacket sendMsg = new DatagramPacket(bufAllocate.array(), bufAllocate.capacity(), dest, serverport);
- s.send(sendMsg);
- }
- }
- }
- private void handleReceive(DatagramSocket s, DatagramPacket receive) throws IOException {
- receive.setLength(bufsize); // max received packet size
- s.receive(receive); // the actual receive operation
- ByteBuffer buffer = ByteBuffer.wrap(receive.getData());
- int recieveR = buffer.getInt();
- int receiveI = buffer.getInt();
- // we got our ACK
- if (recieveR == R) {
- ackArray[receiveI] = true;
- System.out.println("R: " + recieveR + ". i: " + receiveI);
- }
- }
- }
- /* FRAKLIP
- // String buf;
- // buf = bin.readLine();
- // if (buf == null) {
- // buf = "null"; // user typed EOF character
- // }
- // buf = buf + "\n"; // append newline character
- // int slen = buf.length();
- // byte[] bbuf = buf.getBytes();
- // msg.setData(bbuf);
- // msg.setLength(slen);
- */
Advertisement
Add Comment
Please, Sign In to add comment