import java.net.*;
import java.util.Scanner;
import java.io.*;
import classes.Adress;
import classes.DefaultParams;
import classes.Message;
/*
* MultiLient class - receives and sends messages
*
* **/
public class MultiClient {
public static void main(String[] ar) {
//Connection adresses
Adress _connAdress = new Adress(6000,"127.0.0.2");
//System.out.println("Client: connection's params ");
//Print connections params
//_connAdress.printInfo();
try {
Socket _socket = new Socket(_connAdress.getIPAdress(), _connAdress.getPort()); // ������� ����� ��������� IP-����� � ���� �������.
// Take in/output sreams
InputStream _inputStream = _socket.getInputStream();
OutputStream _outputStream = _socket.getOutputStream();
//Convertation streams
DataInputStream _inputDataStream = new DataInputStream(_inputStream);
DataOutputStream _outputDataStream = new DataOutputStream(_outputStream);
//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();
}
}
//Clear Console
private static void clearConsole(){
try{
String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (Exception exception)
{
// Handle exception.
}
}
}