Skip to content
Snippets Groups Projects
Select Git revision
  • 7335234d71e09073acd19b26784f5a7df3a1c0c1
  • master default protected
  • removing_internal_representations
3 results

Dstore.java

Blame
  • Dstore.java 3.55 KiB
    package ftp;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    public class Dstore {
    
        int port;
        int cport;
        int timeout;
        String file_folder;
    
        private static SocketChannel dstore;
        private static ByteBuffer buffer;
    
    
        /**
         * @desc constructs a client
         * @param port port to listen on
         * @param cport controller port to talk to
         * @param timeout timeout (ms)
         * @param file_folder where to store data locally
         */
        public Dstore(int port, int cport, int timeout, String file_folder) throws IOException{
            this.port = port;
            this.cport = cport;
            this.timeout = timeout;
            this.file_folder = file_folder;
    
    
            // opening connection to controller
            // open(addr) acts as convenience method for open() and connect()
            // in blocking mode, so will wait for response before progressing
            //
            try {
                dstore = SocketChannel.open(new InetSocketAddress(cport));
                buffer = ByteBuffer.allocate(256);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            sendMessage("DSTORE");
    
    
            // creating server socket to receive requests and files
            //
            ServerSocketChannel serverSocket = ServerSocketChannel.open();
            serverSocket.bind(new InetSocketAddress(port));
            while (true) {
                SocketChannel client = serverSocket.accept();
    
                new Thread( () -> {
    
                } ).start();
    
            }
        }
    
    
        public String sendMessage(String msg) {
            buffer = ByteBuffer.wrap(msg.getBytes());
            String response = null;
            try {
                dstore.write(buffer);
                buffer.clear();
                dstore.read(buffer);
                response = new String(buffer.array()).trim();
                buffer.clear();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
    
        }
    
    
        private static void takeRequest(ByteBuffer buffer, SelectionKey key)
                throws IOException {
    
            SocketChannel client = (SocketChannel) key.channel();
            client.read(buffer);
            String command = new String(buffer.array()).trim();
            System.out.println("Received: " + command);
    
            buffer.flip();              //resets pos to 0 to read from buffer and sets limit to what was put there
            client.write(buffer);
            buffer.clear();             //empties buffer and clears, ready for writing
        }
    
    
        private static void register(Selector selector, ServerSocketChannel serverSocket)
                throws IOException {
    
            SocketChannel client = serverSocket.accept();
            client.configureBlocking(false);
            client.register(selector, SelectionKey.OP_READ);
        }
    
    
        public static void main(String args[]) {
            String numberArgs[] = Arrays.copyOf(args, args.length-1);
            Stream<String> str = Arrays.stream(numberArgs);
    
            List<Integer> intArgs = str.map(x -> {return Integer.parseInt(x);})
                    .collect(Collectors.toList());
    
            try {
                Dstore dstore = new Dstore(intArgs.get(0), intArgs.get(1), intArgs.get(2), args[3]);
            } catch (IOException e) {
                System.out.println("IOException " + e.getMessage());
            }
        }
    
    }