Merge pull request #16 from PM2-IT21bWIN-ruiz-mach-krea/ErrorTesting_Andrin

Functional issues fixed Nr. #4,#3,#8
This commit is contained in:
Roman Schenk 2022-04-10 21:46:54 +02:00 committed by GitHub Enterprise
commit e0ca1c77bb
4 changed files with 9 additions and 3 deletions

View File

@ -86,6 +86,7 @@ public class ChatWindowController {
return;
}
String messageString = messageField.getText().strip();
messageField.clear();
Matcher matcher = messagePattern.matcher(messageString);
if (matcher.find()) {
String receiver = matcher.group(1);

View File

@ -305,7 +305,7 @@ public class NetworkHandler {
* @param data data object of type T to be submitted through the connection.
* @throws IOException if an error occurs (e.g. connection interrupted while sending, ...)
*/
public void send(T data) throws IOException {
public synchronized void send(T data) throws IOException {
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.writeObject(data);
}

View File

@ -67,7 +67,7 @@ public class Server {
while (true) {
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
connectionHandler.startReceiving();
new Thread(connectionHandler).start();
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
connectionHandler.getUserName(),
connection.getRemoteHost(),

View File

@ -14,7 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
public class ServerConnectionHandler {
public class ServerConnectionHandler implements Runnable{
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
private final int connectionId = connectionCounter.incrementAndGet();
private final NetworkHandler.NetworkConnection<String> connection;
@ -33,6 +33,11 @@ public class ServerConnectionHandler {
private String userName = "Anonymous-"+connectionId;
private State state = NEW;
@Override
public void run() {
startReceiving();
}
enum State {
NEW, CONNECTED, DISCONNECTED;
}