SHARE
TWEET
Untitled
a guest
Jan 29th, 2018
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- package fr.upem.net.udp;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.nio.ByteBuffer;
- import java.nio.channels.AsynchronousCloseException;
- import java.nio.channels.DatagramChannel;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.Arrays;
- import java.util.BitSet;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class ClientIdUpperCaseUDPBurst {
- private static Logger logger = Logger.getLogger(ClientIdUpperCaseUDPBurst.class.getName());
- private static final Charset UTF8 = Charset.forName("UTF8");
- private static final int BUFFER_SIZE = 1024;
- private final List<String> lines;
- private final int nbLines;
- private final String[] upperCaseLines; //
- private final int timeout;
- private final String outFilename;
- private final InetSocketAddress serverAddress;
- private final DatagramChannel dc;
- private final BitSet received; // BitSet marking received requests
- private static void usage() {
- System.out.println("Usage : ClientIdUpperCaseUDPBurst in-filename out-filename timeout host port ");
- }
- private ClientIdUpperCaseUDPBurst(List<String> lines, int timeout, InetSocketAddress serverAddress, String outFilename) throws IOException {
- this.lines = lines;
- this.nbLines = lines.size();
- this.timeout = timeout;
- this.outFilename = outFilename;
- this.serverAddress = serverAddress;
- this.dc = DatagramChannel.open();
- dc.bind(null);
- this.received = new BitSet(nbLines);
- this.upperCaseLines = new String[nbLines];
- }
- private void senderThreadRun() {
- while (!Thread.interrupted()) {
- try {
- synchronized (UTF8) {
- for (int i = 0; i < nbLines; i++) {
- if (!received.get(i)) {
- ByteBuffer msg = UTF8.encode(lines.get(i));
- ByteBuffer bbMsg = ByteBuffer.allocate(BUFFER_SIZE);
- bbMsg.putLong((long) i).put(msg);
- bbMsg.flip();
- dc.send(bbMsg, serverAddress);
- }
- }
- UTF8.wait(300);
- }
- } catch (IOException | InterruptedException e) {
- }
- }
- }
- private void launch() throws IOException {
- Thread senderThread = new Thread(this::senderThreadRun);
- senderThread.start();
- while (received.cardinality() != nbLines) {
- ByteBuffer bbReceiver = ByteBuffer.allocateDirect(BUFFER_SIZE);
- dc.receive(bbReceiver);
- long id = bbReceiver.getLong();
- synchronized (UTF8) {
- if (!received.get((int) id)) {
- received.set((int) id);
- System.out.println("received " + id);
- upperCaseLines[(int) id] = UTF8.decode(bbReceiver).toString();
- }
- }
- }
- senderThread.interrupt();
- Files.write(Paths.get(outFilename), Arrays.asList(upperCaseLines), UTF8,
- StandardOpenOption.CREATE,
- StandardOpenOption.WRITE,
- StandardOpenOption.TRUNCATE_EXISTING);
- }
- public static void main(String[] args) throws IOException, InterruptedException {
- if (args.length != 5) {
- usage();
- return;
- }
- String inFilename = args[0];
- String outFilename = args[1];
- int timeout = Integer.valueOf(args[2]);
- String host = args[3];
- int port = Integer.valueOf(args[4]);
- InetSocketAddress serverAddress = new InetSocketAddress(host, port);
- //Read all lines of inFilename opened in UTF-8
- List<String> lines = Files.readAllLines(Paths.get(inFilename), UTF8);
- //Create client with the parameters and launch it
- ClientIdUpperCaseUDPBurst client = new ClientIdUpperCaseUDPBurst(lines, timeout, serverAddress, outFilename);
- client.launch();
- }
- }
RAW Paste Data

