//Stream for keyboard read
BufferedReader _keyboardStream = new BufferedReader(new InputStreamReader(System.in));
//Addition params...
String _tmpMessage = DefaultParams.getDelimiter();
String _tmpCall = "";
Message _mess = new Message(_tmpMessage);
Boolean _isWaitForInput = true;
Boolean _isWasInput = true;
//Commands
System.out.println("*** Commands ***");
System.out.println("** " + DefaultParams.getCommandLogin() + " - command for login");
System.out.println("** " + DefaultParams.getCommandLogout() + " - command for logout");
System.out.println("** " + DefaultParams.getCommandSendTo() + " - persnal message for user (4ex: print '" + DefaultParams.getCommandSendTo() + "USER_NAME Text_Message')");
System.out.println("** " + DefaultParams.getCommandUserList() + " - show active users");
System.out.println("** " + DefaultParams.getCommandExit() + " - quit");
while (true) {
_isWaitForInput = true;
_isWasInput = true;
/* Global command
* **/
if (_mess.getCommand().equals(DefaultParams.getCommandClearConsole())){//Login and clean console
clearConsole();
System.out.println("You are online. Please press Enter for continue...");
_mess = null;
_mess = new Message(DefaultParams.getCommandMess() + DefaultParams.getDelimiter());
}else if (_mess.getCommand().equals(DefaultParams.getCommandError())){//Get error
System.out.print(_mess.getValue());
_mess = null;
_mess = new Message(/*DefaultParams.getCommandMess() + */DefaultParams.getDelimiter());
}
else if (_mess.getCommand().equals(DefaultParams.getCommandPrint())){//Print message
System.out.print(_mess.getValue());
_mess = null;
_mess = new Message(/*DefaultParams.getCommandMess() + */DefaultParams.getDelimiter());
_isWaitForInput = false;
}else if (_mess.getCommand().equals(DefaultParams.getCommandName())){//Input user name
System.out.print(_mess.getValue());
_mess = new Message(DefaultParams.getCommandName() + DefaultParams.getDelimiter());
}else if (_mess.getCommand().equals(DefaultParams.getCommandPass())){//Input password
System.out.print(_mess.getValue());
_mess = new Message(DefaultParams.getCommandPass() + DefaultParams.getDelimiter());
}else{
//Input messagies
System.out.print(_mess.getValue());
_mess = new Message(DefaultParams.getCommandMess() + DefaultParams.getDelimiter());
}
/* Input from console
* **/
if(_isWaitForInput){
while(_isWasInput/*!Thread.currentThread().isInterrupted()*/){
try {
if (_keyboardStream.ready()) {
_tmpMessage = _keyboardStream.readLine();
_isWasInput = false;
}else{
try{
//Ping server jedes 1 sec
Thread.sleep(DefaultParams.getPingServer());
//Send string to server
_outputDataStream.writeUTF(DefaultParams.getCommandCall());
_outputDataStream.flush();
//Read answer
_tmpCall = _inputDataStream.readUTF();
//if we need to show message....
if(!_tmpCall.equals(DefaultParams.getCommandEmpty())){
System.out.println(_tmpCall);
}
} catch (InterruptedException e){}
}
} catch (Exception e) {
try {
Thread.currentThread().wait(500);
} catch (InterruptedException e1) {
System.out.println(e.getMessage());
}
}
}
}
_tmpMessage = _mess.getCommand() + DefaultParams.getDelimiter() + _tmpMessage;
_isWasInput = true;
_mess = null;
_mess = new Message(_tmpMessage);
/* Send to server
* **/
_outputDataStream.writeUTF(_mess.getCurrentStr()); // �������� ��������� ������ ������ �������.
_outputDataStream.flush(); // ���������� ����� ��������� �������� ������.
/* Waiting for server
* **/
_tmpMessage = _inputDataStream.readUTF(); // ���� ���� ������ ������ ������ ������.
_mess = null;
_mess = new Message(_tmpMessage);
}
} catch (Exception x) {
x.printStackTrace();
}
}