KBKrause / chemberry

Java program to facilitate classroom experiments using Arduino and Raspberry Pi
0 stars 0 forks source link

Instructor connection closes #11

Open KBKrause opened 6 years ago

KBKrause commented 6 years ago

commit 83f15d6f8fac115fc594af98dbabb357d7cca70c Client must account for instructor connection closing prematurely.

 public boolean sendString(String data)
    {
        try
        {
            String request = data;

            // Create a socket using the serverIP configured to this client's proxy.
            Socket clientSocket = new Socket(serverIP, serverPort);

            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));      

            // writeBytes() takes the string and a newline character to signify when there are no more characters coming.
            outToServer.writeBytes(request + "\n");

            // readLine() is a blocking call that is waiting for an ack from the server.
            String responseFromServer = inFromServer.readLine();

            //System.out.println("Received response: " + responseFromServer);

            clientSocket.close();

            return true;
        }
        catch(IOException e)
        {
            System.out.println("ERROR: Failed to connect to: " + serverIP + ":" + serverPort);
            e.printStackTrace();
            return false;
        }
    }
KBKrause commented 6 years ago

Exception is first thrown in GUIProxy.

 @Override
    public void update(String theUpdate) throws ConnectionFailedException
    {
        if (connToIns.sendString(theUpdate) == false)
        {
            throw new ConnectionFailedException();
        }
    }

This function is called by GUI when it is notifying all of its observers.

@Override
    public void notifyObservers(String updateText)
    {
        for (InstructorObserver io : observers)
        {
            try
            {
                io.update(updateText);
            }
            catch(ChemberryException ex)
            {
                ex.printStackTrace();
            }
        }
    }

So I think the exception handling should happen in GUI.

KBKrause commented 6 years ago

Exception handling will happen in GUI. Main thread of execution listens for signal from instructor. Signal failing to arrive will come later. GUI will use a listening proxy.

public class GUIListeningProxy extends ServerProxy
{

}

handleRequest will only listen for the "d:esync" token.