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);

        // 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());

        // Write our greetings
        out.writeBytes(CLIENT_GREET);

        // Expect server to return with correct greetings
        byte greet[] = new byte[SERVER_GREET.length()];
        in.readFully(greet, 0, SERVER_GREET.length());

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

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

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

    }
}

@@ -630,24 +630,113 @@
 , tracks);%0A%0A
+        logger.fine(String.format(%22Trying to connect to Rocket at %25s:%25d%22, host, port));%0A%0A
         // C
@@ -934,16 +934,17 @@
 d failed
+.
 %22, host,
@@ -1117,66 +1117,492 @@
     
-// Write our greetings%0A        out.writeBytes(CLIENT_GREET
+logger.finer(%22Connection established.%22);%0A        %0A        // This raises if something goes terribly wrong%0A        greetServer();%0A%0A        logger.info(String.format(%22Connected to Rocket running at %25s:%25d.%22, host, port));%0A    %7D%0A%0A    private void greetServer() throws Exception %7B%0A        logger.finer(%22Sending greetings to Rocket.%22);%0A        %0A        // Write our greetings%0A        out.writeBytes(CLIENT_GREET);%0A%0A        logger.finer(%22Greetings sent. Now reading greetings from Rocket...%22
 );%0A%0A
@@ -1715,16 +1715,34 @@
 gth()%5D;%0A
+        try %7B%0A    
         
@@ -1783,24 +1783,249 @@
 T.length());
+%0A        %7D catch (Exception e) %7B%0A            logger.severe(%22Couldn't read greetings from server.%22);%0A            throw new Exception(%22Reading greetings failed%22);%0A        %7D%0A%0A        logger.finer(%22Greetings read successfully.%22);
 %0A%0A        if
@@ -2088,23 +2088,22 @@
  logger.
-warning
+severe
 (%22Server
@@ -2205,97 +2205,8 @@
    %7D
-%0A%0A        logger.info(String.format(%22Connected to server running at %25s:%25d%22, host, port));
 %0A   

Refactor server greetings to separate method