I have a user input blocking problem. I have a menu where thne user enters commands at the command prompt. The application is always listening for user input with the following line which is blocking:
String InputRead.readLine();
However this code is for the client of an RMI Callback server. The server can callback the client with messages that need client feedback. So I have another blocking readLine(); call for user input.
So when the callback looks for user feedback, what the user enters goes to the menu readline() rather than the callback readline(). Is there anyway I can send the menu class to sleep while I give the server feedback - or is there a better way around this?
client menu code:
client server callback:
String InputRead.readLine();
However this code is for the client of an RMI Callback server. The server can callback the client with messages that need client feedback. So I have another blocking readLine(); call for user input.
So when the callback looks for user feedback, what the user enters goes to the menu readline() rather than the callback readline(). Is there anyway I can send the menu class to sleep while I give the server feedback - or is there a better way around this?
client menu code:
Code:
boolean userDone = false;
while (!userDone) {
//Get User Response
String userResponse = br.readLine();
// SEND CHAT MESSAGE
if ((userResponse.trim()).equals("1") || (userResponse.trim()).equals("1.") || (userResponse.trim()).equals("Send")) {// Send a Chat Message
System.out.println( // Get UserName
"Who do you want to send a message to?");
String sendToUser = br.readLine();
System.out.println( // Get Message
"Enter message to send to " + sendToUser + " :");
String sendMessage = br.readLine();
serverInterface.directMessage(sendToUser, UserName, sendMessage);
//Show User the Main Menu
clientMenu();
}
// LIST CHATROOM MEMBERS
else if ((userResponse.trim()).equals("2") || (userResponse.trim()).equals("2.") || (userResponse.trim()).equals("List")) {// List all members in Chatroom
//Call List Members from Server
String listMembers = serverInterface.listChatMembers();
System.out.println("Members in Chatroom: \n" + listMembers);
//Show User the Main Menu
clientMenu();
}
// DISCONNECT FROM SERVER
else if ((userResponse.trim()).equals("3") || (userResponse.trim()).equals("3.") || (userResponse.trim()).equals("Leave")) {// if user wants to disconnect
serverInterface.unregisterForCallback(UserName, callbackObj);
System.out.println("You have left the Chatroom.");
//Show User the Connect / End Program Menu
connectMenu();
}
//INCORRECT USER SELECTION
else { // if user makes wrong selection
System.out.println("Select a number from the menu !");
} // end if
} // end while
} // end try
catch (Exception ex) {
ex.printStackTrace( );
} //end catch
client server callback:
Code:
public class CallbackClientImpl extends UnicastRemoteObject
implements CallbackClientInterface {
public CallbackClientImpl() throws RemoteException {
super( );
}
public String notifyMe(String message){
String returnMessage = message;
System.out.println(returnMessage);
return returnMessage;
}
public String getMessage(int messageNumber, String userName, String message) throws RemoteException, IOException {
boolean userDone = false;
String userResponse = "";
System.out.println("Do you wish to receive a message from " + userName + "? (Y/N)");
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
while (!userDone) {
userResponse = br.readLine();
//USER ACCEPTS MESSAGE
if ((userResponse.trim()).equals("Y") || (userResponse.trim()).equals("Y.") || (userResponse.trim()).equals("Yes")) {
userDone = true;
System.out.println(userName + ": " + message);
//USER REJECTS MESSAGE
} else if ((userResponse.trim()).equals("N") || (userResponse.trim()).equals("N.") || (userResponse.trim()).equals("No")) {
userDone = true;
System.out.println("Message Rejected.");
}
//INCORRECT USER SELECTION
else { // if user makes wrong selection
System.out.println("Please enter 'Y' for Yes or 'N' for No !");
}//end if
}// loop
return userResponse.trim();
}
}// end CallbackClientImpl class