Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cviko1A;
- import java.io.File;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class FileInfoSender {
- public static final String FILE_PATH = "C:\\Users\\pc12\\Downloads\\SYS_PRGM.ova";
- public static final int INFO_PORT = 5500;
- public static final int REQUEST_PORT = 5501;
- public static final int DATA_PORT = 5502;
- public static final String TARGET_HOST = "158.197.35.101"; // 158.197.35.255
- public static final long SLEEP_TO_NEXT_SEND = 1000; // 1sec
- public static void main(String[] args) {
- File file = new File(FILE_PATH);
- long fileLength = file.length();
- String fileName = file.getName();
- ExecutorService threadManager = Executors.newCachedThreadPool();
- Interval commonInterval = Interval.empty(0, fileLength - 1);
- threadManager.execute(new FileSender(file, commonInterval));
- threadManager.execute(new RequestReceiver(commonInterval));
- try (DatagramSocket soket = new DatagramSocket()) {
- String message = fileName + "\n" + fileLength;
- byte[] messageBytes = message.getBytes();
- DatagramPacket paket = new DatagramPacket(messageBytes, messageBytes.length,
- InetAddress.getByName(TARGET_HOST), INFO_PORT);
- while (true) {
- soket.send(paket);
- Thread.currentThread().sleep(SLEEP_TO_NEXT_SEND);
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- ========================================================
- package cviko1A;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.SocketTimeoutException;
- import java.util.List;
- import java.util.Scanner;
- public class FileReceiver {
- public static final int MAX_PACKET_SIZE = 1200;
- private Interval recievedBytes;
- private String fileName;
- private long fileLength;
- private InetAddress fileSenderIP;
- private DatagramSocket socket;
- public FileReceiver(String fileName, long fileLength, InetAddress fileSenderIP) {
- this.fileName = fileName;
- this.fileLength = fileLength;
- this.fileSenderIP = fileSenderIP;
- recievedBytes = Interval.empty(0, fileLength - 1);
- try {
- socket = new DatagramSocket(FileInfoSender.DATA_PORT);
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void sendMissingIntervaosRequest() {
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(MAX_PACKET_SIZE);
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- int maxCount = (MAX_PACKET_SIZE - Integer.BYTES) / (2 * Long.BYTES);
- List<Interval> emptySubintervals = recievedBytes.getEmptySubintervals(maxCount);
- oos.writeInt(emptySubintervals.size());
- for (Interval i : emptySubintervals) {
- oos.writeLong(i.getMin());
- oos.writeLong(i.getMax());
- }
- oos.flush();
- oos.close();
- byte[] messageBytes = baos.toByteArray();
- DatagramPacket paket = new DatagramPacket(messageBytes, messageBytes.length, fileSenderIP,
- FileInfoSender.REQUEST_PORT);
- socket.send(paket);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- try (DatagramSocket soket = new DatagramSocket(FileInfoSender.INFO_PORT)) {
- while (true) { // zo srandy
- byte[] buffer = new byte[soket.getReceiveBufferSize()];
- DatagramPacket paket = new DatagramPacket(buffer, buffer.length);
- soket.setSoTimeout((int) (FileInfoSender.SLEEP_TO_NEXT_SEND * 3));
- try {
- soket.receive(paket);
- } catch (SocketTimeoutException e) {
- System.err.println("Server nie je v sieti");
- return;
- }
- byte[] data = paket.getData();
- String message = new String(data).trim();
- System.out.println(message);
- Scanner scanner = new Scanner(message);
- String filename = scanner.nextLine();
- long fileLength = scanner.nextLong();
- scanner.close();
- System.out.println("Doslo: subor = " + filename);
- System.out.println("dlzka suboru = " + fileLength);
- System.out.println();
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- =========================================================
- package cviko1A;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.io.RandomAccessFile;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- public class FileSender implements Runnable {
- private DatagramSocket socket;
- private Interval partsToSend;
- private File file;
- private RandomAccessFile raf;
- public FileSender(File file, Interval partsToSend) {
- this.file = file;
- this.partsToSend = partsToSend;
- }
- @Override
- public void run() {
- try (DatagramSocket socket = new DatagramSocket(FileInfoSender.REQUEST_PORT);
- RandomAccessFile raf = new RandomAccessFile(file, "r")) {
- this.raf = raf;
- this.socket = socket;
- while (true) {
- Interval interval = partsToSend.getAndEraseNextFullSubintervalBlocked(FileReceiver.MAX_PACKET_SIZE);
- sendFilePart(interval.getMin(), (int) interval.getMax());
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private void sendFilePart(long offset, int maxLength) throws IOException {
- raf.seek(offset);
- byte[] bytes = new byte[maxLength];
- int length = raf.read(bytes);
- int packetLength = Long.BYTES + Integer.BYTES + length;
- ByteArrayOutputStream baos = new ByteArrayOutputStream(packetLength);
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- oos.writeLong(offset);
- oos.writeInt(length);
- oos.write(bytes, 0, length);
- oos.flush();
- oos.close();
- byte[] messageBytes = baos.toByteArray();
- DatagramPacket paket = new DatagramPacket(messageBytes, messageBytes.length,
- InetAddress.getByName(FileInfoSender.TARGET_HOST), FileInfoSender.REQUEST_PORT);
- socket.send(paket);
- }
- }
- =======================================================================================
- package cviko1A;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.SocketException;
- import java.net.SocketTimeoutException;
- import java.util.Scanner;
- public class RequestReceiver implements Runnable {
- private Interval partsToSend;
- public RequestReceiver(Interval partsToSend) {
- this.partsToSend = partsToSend;
- }
- @Override
- public void run() {
- try {
- DatagramSocket socket = new DatagramSocket(FileInfoSender.REQUEST_PORT);
- while (true) { // zo srandy
- byte[] buffer = new byte[socket.getReceiveBufferSize()];
- DatagramPacket paket = new DatagramPacket(buffer, buffer.length);
- socket.setSoTimeout((int) (FileInfoSender.SLEEP_TO_NEXT_SEND * 3));
- socket.receive(paket);
- byte[] data = paket.getData();
- ByteArrayInputStream bais = new ByteArrayInputStream(data);
- ObjectInputStream ois = new ObjectInputStream(bais);
- int intervalsCount = ois.readInt();
- for (int i = 0; i < intervalsCount; i++) {
- long min = ois.readLong();
- long max = ois.readLong();
- partsToSend.addFullSubinterval(min, max);
- }
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement