daily pastebin goal
41%
SHARE
TWEET

Untitled

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