Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sendBackAndForth;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.SocketException;
- import java.nio.ByteBuffer;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.HashMap;
- import java.util.Map;
- public class OpSysServerRecieveAndSend {
- private int serverport = 5432;
- private int bufsize = 512;
- private final int timeout = 50000;
- // private HashMap<String, Integer> map;
- private int blockNum;
- private int INIT_VAL = 123456789;
- private int R = INIT_VAL;
- private long S = INIT_VAL;
- private int i = INIT_VAL;
- private int B = OpSysClientSendRecieve.B;
- private HashMap<Integer, Packet> byteMap;
- private boolean haveRecivedS = false;
- static public void main(String args[]) {
- new OpSysServerRecieveAndSend().run();
- } // end of main
- private void run() {
- try {
- byteMap = new HashMap<>();
- // map = new HashMap<>();
- DatagramSocket s = null; // UDP uses DatagramSockets
- s = new DatagramSocket(serverport);
- s.setSoTimeout(timeout); // set timeout in milliseconds
- // create DatagramPacket object for receiving data:
- DatagramPacket msg = new DatagramPacket(new byte[bufsize], bufsize);
- while (notRecievedAllPackages()) { // read loop
- handleReceive(s, msg);
- handleReply(s, msg);
- printAllEntris();
- } // while
- Path path = Paths.get("/home/datstorm/IdeaProjects/Opsys-BFTP/src/recieveFiles/recieve.txt"); // FIXME make to relative path
- byte[] bytes = getByteArray();
- Files.write(path, bytes, StandardOpenOption.APPEND);
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void printAllEntris() {
- for (int k = 0; k < recivedB.length; k++) {
- System.out.println("REC: " + k + ", =" + recivedB[k]);
- }
- }
- private boolean[] recivedB;
- private boolean init = true;
- private boolean notRecievedAllPackages() {
- // Make sure we have S
- if (!haveRecivedS) {
- return true;
- }
- // we check if we have recived all packages
- for (boolean b : recivedB) {
- // we are not done contine while
- if (!b) {
- return true;
- }
- }
- // we are done int while.
- return false;
- }
- private void handleReceive(DatagramSocket s, DatagramPacket msg) throws IOException {
- //Recive
- msg.setLength(bufsize); // max received packet size
- s.receive(msg); // the actual receive operation
- //Read header
- ByteBuffer buffer = ByteBuffer.wrap(msg.getData());
- R = buffer.getInt();
- S = buffer.getLong();
- i = buffer.getInt();
- haveRecivedS = true;
- // first time = have to do setup of packages
- if (init) {
- int blocksOfSizeB = (int) (Math.ceil((double) S / B)); // FIXME Removed -1 HERE
- recivedB = new boolean[blocksOfSizeB];
- init = false;
- }
- ByteBuffer data = buffer.slice();
- byte b = data.get();
- if (!recivedB[i]) {
- byteMap.put(i, new Packet(R, S, i, b));
- recivedB[i] = true;
- }
- System.out.println(String.format("Recived package R: %d, S: %d, i: %d and with %d bytes of data", R, S, i, data.capacity()));
- }
- private void handleReply(DatagramSocket s, DatagramPacket msg) throws IOException {
- /***********************************
- * ACK
- **********************************/
- String key = msg.getSocketAddress().toString();
- ByteBuffer buf = ByteBuffer.allocate(8);
- buf.putInt(R);
- buf.putInt(byteMap.get(i).getI());
- //Reply
- DatagramPacket reply = new DatagramPacket(buf.array(), buf.capacity(), msg.getSocketAddress());
- s.send(reply);
- }
- public byte[] getByteArray() {
- byte[] bytes = new byte[Math.toIntExact(S)];
- for (Map.Entry<Integer, Packet> integerByteEntry : byteMap.entrySet()) {
- bytes[integerByteEntry.getKey()] = integerByteEntry.getValue().getData();
- }
- return bytes;
- }
- }
- /* FRAKLIP
- //Store connection
- // String key = msg.getSocketAddress().toString();
- // blockNum = 0;
- // if (!map.containsKey(key)) {
- // map.put(key, blockNum);
- // } else {
- // blockNum = map.get(key);
- // blockNum++;
- // map.replace(key, blockNum);
- // }
- */
Advertisement
Add Comment
Please, Sign In to add comment