daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 63 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 (received.cardinality() != nbLines) {
  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(i).put(msg);
  62.                             bbMsg.flip();
  63.                             dc.send(bbMsg, serverAddress);
  64.  
  65.                         }
  66.                     }
  67.                     UTF8.wait(300);
  68.                 }
  69.             } catch (InterruptedException | AsynchronousCloseException e) {
  70.                 // normal behaviour
  71.             } catch (IOException e) {
  72.                 logger.log(Level.SEVERE, "Sender thread killed by IOException", e);
  73.             } finally {
  74.                 logger.info("Sender thread stopped.");
  75.             }
  76.         }
  77.     }
  78.  
  79.     private void launch() throws IOException {
  80.         Thread senderThread = new Thread(this::senderThreadRun);
  81.         senderThread.start();
  82.  
  83.  
  84.         while (received.cardinality() != nbLines) {
  85.             ByteBuffer bbReceiver = ByteBuffer.allocateDirect(BUFFER_SIZE);
  86.             dc.receive(bbReceiver);
  87.             bbReceiver.flip();
  88.             long id = bbReceiver.getLong();
  89.             if (!received.get((int) id)) {
  90.                 received.set((int) id);
  91.                 upperCaseLines[(int) id] = UTF8.decode(bbReceiver).toString();
  92.             }
  93.         }
  94.         senderThread.interrupt();
  95.         Files.write(Paths.get(outFilename), Arrays.asList(upperCaseLines), UTF8,
  96.                 StandardOpenOption.CREATE,
  97.                 StandardOpenOption.WRITE,
  98.                 StandardOpenOption.TRUNCATE_EXISTING);
  99.  
  100.     }
  101.  
  102.     public static void main(String[] args) throws IOException, InterruptedException {
  103.         if (args.length != 5) {
  104.             usage();
  105.             return;
  106.         }
  107.  
  108.         String inFilename = args[0];
  109.         String outFilename = args[1];
  110.         int timeout = Integer.valueOf(args[2]);
  111.         String host = args[3];
  112.         int port = Integer.valueOf(args[4]);
  113.         InetSocketAddress serverAddress = new InetSocketAddress(host, port);
  114.  
  115.         //Read all lines of inFilename opened in UTF-8
  116.         List<String> lines = Files.readAllLines(Paths.get(inFilename), UTF8);
  117.         //Create client with the parameters and launch it
  118.         ClientIdUpperCaseUDPBurst client = new ClientIdUpperCaseUDPBurst(lines, timeout, serverAddress, outFilename);
  119.         client.launch();
  120.  
  121.     }
  122. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top