Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package fr.upem.net.udp;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.nio.ByteBuffer;
  6. import java.nio.channels.DatagramChannel;
  7. import java.nio.charset.Charset;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardOpenOption;
  11. import java.util.Arrays;
  12. import java.util.BitSet;
  13. import java.util.List;
  14. import java.util.logging.Logger;
  15.  
  16. public class ClientIdUpperCaseUDPBurst {
  17.  
  18.     private static Logger logger = Logger.getLogger(ClientIdUpperCaseUDPBurst.class.getName());
  19.     private static final Charset UTF8 = Charset.forName("UTF8");
  20.     private static final int BUFFER_SIZE = 1024;
  21.     private final List<String> lines;
  22.     private final int nbLines;
  23.     private final String[] upperCaseLines;
  24.     private final int timeout;
  25.     private final String outFilename;
  26.     private final InetSocketAddress serverAddress;
  27.     private final DatagramChannel dc;
  28.     private volatile BitSet received;
  29.  
  30.     private static void usage() {
  31.         System.out.println("Usage : ClientIdUpperCaseUDPBurst in-filename out-filename timeout host port");
  32.     }
  33.  
  34.     private ClientIdUpperCaseUDPBurst(List<String> lines, int timeout, InetSocketAddress serverAddress, String outFilename) throws IOException {
  35.         this.lines = lines;
  36.         this.nbLines = lines.size();
  37.         this.timeout = timeout;
  38.         this.outFilename = outFilename;
  39.         this.serverAddress = serverAddress;
  40.         this.dc = DatagramChannel.open();
  41.         dc.bind(null);
  42.         this.received = new BitSet(nbLines);
  43.         this.upperCaseLines = new String[nbLines];
  44.     }
  45.  
  46.     private void senderThreadRun() throws IOException, InterruptedException {
  47.         while(!Thread.interrupted() && received.cardinality() != 0) {
  48.             for (int id = 0; id < nbLines; id++) {
  49.                 if (!received.get(id)) {
  50.                     ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
  51.                     String line = lines.get(id);
  52.                     buffer.putLong(id);
  53.                     buffer.put(UTF8.encode(line));
  54.                     buffer.flip();
  55.                     dc.send(buffer, serverAddress);
  56.                 }
  57.             }
  58.             Thread.sleep(timeout);
  59.         }
  60.     }
  61.  
  62.     private void launch () throws IOException {
  63.         Thread senderThread = new Thread(() -> {
  64.             try {
  65.                 senderThreadRun();
  66.             } catch (IOException | InterruptedException e) {
  67.                 throw new IllegalStateException("Sender failed to send");
  68.             }
  69.         });
  70.         senderThread.start();
  71.         ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
  72.         while(received.cardinality() < nbLines) {
  73.             buffer.clear();
  74.             dc.receive(buffer);
  75.             buffer.flip();
  76.  
  77.             if (buffer.remaining() < Long.BYTES) {
  78.                 continue;
  79.             }
  80.             int id = (int)buffer.getLong();
  81.             String msg = UTF8.decode(buffer).toString();
  82.             System.out.println(msg);
  83.  
  84.             if (!received.get(id)) {
  85.                 upperCaseLines[id] = msg;
  86.                 received.set(id);
  87.             }
  88.         }
  89.         senderThread.interrupt();
  90.         Files.write(Paths.get(outFilename), Arrays.asList(upperCaseLines), UTF8, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
  91.     }
  92.  
  93.     public static void main(String[] args) throws IOException {
  94.         if (args.length != 5) {
  95.             usage();
  96.             return;
  97.         }
  98.  
  99.         String inFilename = args[0];
  100.         String outFilename = args[1];
  101.         int timeout = Integer.valueOf(args[2]);
  102.         String host = args[3];
  103.         int port = Integer.valueOf(args[4]);
  104.         InetSocketAddress serverAddress = new InetSocketAddress(host, port);
  105.  
  106.         //Read all lines of inFilename opened in UTF-8
  107.         List<String> lines = Files.readAllLines(Paths.get(inFilename), UTF8);
  108.         //Create client with the parameters and launch it
  109.         ClientIdUpperCaseUDPBurst client = new ClientIdUpperCaseUDPBurst(lines, timeout, serverAddress, outFilename);
  110.         client.launch();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement