|
|
|
@ -42,6 +42,8 @@ class ServerThread extends Thread { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
System.out.println("New thread started."); |
|
|
|
|
|
|
|
|
|
try (InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream()) { |
|
|
|
|
byte[] clientMagicHeader = new byte[Constant.MAGIC_HEADER.length]; |
|
|
|
|
if (inputStream.read(clientMagicHeader) == -1) { |
|
|
|
@ -64,15 +66,17 @@ class ServerThread extends Thread { |
|
|
|
|
if (Arrays.equals(Constant.OPCODE_LOGIN, opcode)) { |
|
|
|
|
this.login(new String(dataPack.getData())); |
|
|
|
|
Common.writeMessage(SERVER_ID, Constant.MESSAGE_OK, outputStream); |
|
|
|
|
|
|
|
|
|
} else if (Arrays.equals(Constant.OPCODE_LOGOUT, opcode)) { |
|
|
|
|
this.logout(new String(dataPack.getData())); |
|
|
|
|
Common.writeMessage(SERVER_ID, Constant.MESSAGE_OK, outputStream); |
|
|
|
|
|
|
|
|
|
} else if (Arrays.equals(Constant.OPCODE_REQUEST_MESSAGE, opcode)) { |
|
|
|
|
this.sendRandomMessage(outputStream); |
|
|
|
|
|
|
|
|
|
} else if (Arrays.equals(Constant.OPCODE_SEND_MESSAGE, opcode)) { |
|
|
|
|
String receivedMessage = this.receiveMessage(dataPack); |
|
|
|
|
String responseMessage = String.format("Receive message '%s'", receivedMessage); |
|
|
|
|
|
|
|
|
|
Common.writeMessage(SERVER_ID, responseMessage, outputStream); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|