Advertisement
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.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.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 volatile BitSet received;
- 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() throws IOException, InterruptedException {
- while(!Thread.interrupted() && received.cardinality() != 0) {
- for (int id = 0; id < nbLines; id++) {
- if (!received.get(id)) {
- ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
- String line = lines.get(id);
- buffer.putLong(id);
- buffer.put(UTF8.encode(line));
- buffer.flip();
- dc.send(buffer, serverAddress);
- }
- }
- Thread.sleep(timeout);
- }
- }
- private void launch () throws IOException {
- Thread senderThread = new Thread(() -> {
- try {
- senderThreadRun();
- } catch (IOException | InterruptedException e) {
- throw new IllegalStateException("Sender failed to send");
- }
- });
- senderThread.start();
- ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
- while(received.cardinality() < nbLines) {
- buffer.clear();
- dc.receive(buffer);
- buffer.flip();
- if (buffer.remaining() < Long.BYTES) {
- continue;
- }
- int id = (int)buffer.getLong();
- String msg = UTF8.decode(buffer).toString();
- System.out.println(msg);
- if (!received.get(id)) {
- upperCaseLines[id] = msg;
- received.set(id);
- }
- }
- 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 {
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement