/** * Receives some data from a client once a connection * has been established. This will block until the client * calls <code>request(Object)</code> but by establishing * a connection. * * @return the <code>Object</code> sent by the client. */ public Object request() //throws IllegalStateException { if (currentServerState == SERVER_STATE_RECEIVED) { throw new InvalidOperationException("Cannot call request() twice on ConnectionServer without replying to the client first."); } ConnectionClientMessage msg = (ConnectionClientMessage)currentInputChannel.read(); if (currentServerState == SERVER_STATE_CLOSED) { if (msg is ConnectionClientOpenMessage) { //channel to use to reply to client toClient = ((ConnectionClientOpenMessage)msg).replyChannel; setAltingChannel(furtherRequestIn); currentInputChannel = furtherRequestIn; //create a new msg for connection established //don't know if client implementation will have finished with //message after connection closed this.msg = new ConnectionServerMessage(); } else { throw new InvalidOperationException("Invalid message received from client"); } } currentServerState = SERVER_STATE_RECEIVED; return(msg.data); }
/** * Receives some data back from the server after * <code>request(Object)</code> has been called. * * @return the <code>Object</code> sent from the server. */ public Object reply()// throws { try { //moved it out from the if statement below and had to initialize it with empty object - KP ConnectionServerMessage serverReply = new ConnectionServerMessage(); if (currentClientState != CLIENT_STATE_MADE_REQ) { try { serverReply = (ConnectionServerMessage)fromServer.read(); } catch (InvalidOperationException) { throw new InvalidOperationException("Cannot call reply() on a ConnectionClient that is not waiting for a reply."); } } //check whether the server closed the connection currentClientState = serverReply.open ? CLIENT_STATE_OPEN : CLIENT_STATE_CLOSED; if (serverReply.open) { currentClientState = CLIENT_STATE_OPEN; } else { currentClientState = CLIENT_STATE_CLOSED; release(); } return(serverReply.data); } catch (InvalidOperationException) { throw; } }