package rocketpower.library;


import java.net.Socket;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.util.logging.Logger;

/*
 * Implements connection with GNU Rocket.
 */
class SocketDevice extends RocketDevice {
    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;

    private final String CLIENT_GREET = "hello, synctracker!";
    private final String SERVER_GREET = "hello, demo!";

    /*
     * Connects to GNU Rocket.
     */
    public SocketDevice(Logger logger, TrackContainer tracks, String host, int port) throws Exception {
        super(logger, tracks);

        logger.fine(String.format("Trying to connect to Rocket at %s:%d", host, port));

        // Connect to rocket and initialize streams
        try {
            socket = new Socket(host, port);
        } catch (Exception e) {
            logger.warning(String.format("Connection to %s:%d failed.", host, port));
            throw e;
        }
        out = new DataOutputStream(socket.getOutputStream());
        in = new DataInputStream(socket.getInputStream());

        logger.finer("Connection established.");
        
        // This raises if something goes terribly wrong
        greetServer();

        logger.info(String.format("Connected to Rocket running at %s:%d.", host, port));
    }

    private void greetServer() throws Exception {
        logger.finer("Sending greetings to Rocket.");
        
        // Write our greetings
        out.writeBytes(CLIENT_GREET);

        logger.finer("Greetings sent. Now reading greetings from Rocket...");

        // Expect server to return with correct greetings
        byte greet[] = new byte[SERVER_GREET.length()];
        try {
            in.readFully(greet, 0, SERVER_GREET.length());
        } catch (Exception e) {
            logger.severe("Couldn't read greetings from server.");
            throw new Exception("Reading greetings failed");
        }

        logger.finer("Greetings read successfully.");

        if (!SERVER_GREET.equals(new String(greet))) {
            logger.severe("Server didn't send correct greetings.");
            throw new Exception("Greetings mismatch");
        }
    }

    /*
     * Reads socket for updates
     */
    public void update() {

    }
}

@@ -452,16 +452,329 @@
 emo!%22;%0A%0A
+    private static class Commands %7B%0A        static final int SET_KEY        = 0;%0A        static final int DELETE_KEY     = 1;%0A        static final int GET_TRACK      = 2;%0A        static final int SET_ROW        = 3;%0A        static final int PAUSE          = 4;%0A        static final int SAVE_TRACKS    = 5;%0A    %7D%0A%0A
     /*%0A 
@@ -2595,16 +2595,1110 @@
 date() %7B
+%0A        try %7B%0A            readCommand();%0A        %7D catch (Exception e) %7B%0A            logger.severe(%22Error in readCommand%22);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Reads pending rocket commands from socket.%0A     *%0A     * Throws exception if command cannot be read%0A     * successfully; this usually means that Rocket%0A     * has died and input stream cannot be read anymore.%0A     */%0A    private void readCommand() throws Exception %7B%0A        byte h;%0A        while (in.available() != 0) %7B%0A            h = in.readByte();%0A            logger.finer(String.format(%22Read command: %25d%22, h));%0A            switch (h) %7B%0A                case Commands.SET_KEY:%0A                    break;%0A                case Commands.DELETE_KEY:%0A                    break;%0A                case Commands.GET_TRACK:%0A                    break;%0A                case Commands.SET_ROW:%0A                    in.readInt();%0A                    break;%0A                case Commands.PAUSE:%0A                    in.readByte();%0A                    break;%0A                case Commands.SAVE_TRACKS:%0A                    break;%0A%0A            %7D%0A        %7D
 %0A%0A    %7D%0A

Implement loop for reading commands from Rocket