Compare commits

..
Author SHA1 Message Date
schrom01 8c588ee75c fixed Disconnecting when window is closed
#6
2022-04-14 19:13:21 +02:00
Roman SchenkandGitHub Enterprise 800528cc37 Merge pull request #33 from PM2-IT21bWIN-ruiz-mach-krea/Server_Console_Display
fixes Issue #20
2022-04-14 16:02:06 +02:00
Leonardo Brandenberger 886d9e5e43 fixes #20 2022-04-14 06:55:10 +02:00
fassbandandGitHub Enterprise 7c68472859 Merge pull request #32 from PM2-IT21bWIN-ruiz-mach-krea/Fixing_private_Messages
fixed Problem "private Messages are visible for sender"
2022-04-13 17:35:48 +02:00
Roman SchenkandGitHub Enterprise b9ffb2b133 Update ServerConnectionHandler.java
added Commment for documentation.
2022-04-13 17:35:07 +02:00
schrom01 e69ced4081 fixed Problem "private Messages are visible for sender"
#28
2022-04-13 17:24:22 +02:00
Roman SchenkandGitHub Enterprise d1dfe6c1ab Merge pull request #31 from PM2-IT21bWIN-ruiz-mach-krea/Remove_Controller_From_Handler
Remove ChatWindowController from ClientConnectionHandler
2022-04-13 16:13:33 +02:00
3 changed files with 62 additions and 30 deletions
@@ -49,7 +49,7 @@ public class ChatWindowController {
}
private void applicationClose() {
connectionHandler.setState(DISCONNECTED);
disconnect();
}
@FXML
@@ -6,6 +6,8 @@ import java.io.IOException;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Server {
@@ -62,23 +64,34 @@ public class Server {
}
private void start() {
ReentrantLock mutex = new ReentrantLock();
Condition nameComplete = mutex.newCondition();
System.out.println("Server started.");
try {
while (true) {
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete);
new Thread(connectionHandler).start();
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
connectionHandler.getUserName(),
connection.getRemoteHost(),
connection.getRemotePort()
));
mutex.lock();
try {
nameComplete.await();
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
connectionHandler.getUserName(),
connection.getRemoteHost(),
connection.getRemotePort()
));
}
finally {
mutex.unlock();
}
}
} catch(SocketException e) {
System.out.println("Server connection terminated");
}
catch (IOException e) {
System.err.println("Communication error " + e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// close server
System.out.println("Server Stopped.");
@@ -11,6 +11,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
@@ -30,6 +32,10 @@ public class ServerConnectionHandler implements Runnable{
private static final String USER_NONE = "";
private static final String USER_ALL = "*";
private ReentrantLock mutex;
private Condition nameComplete;
private String userName = "Anonymous-"+connectionId;
private State state = NEW;
@@ -43,11 +49,13 @@ public class ServerConnectionHandler implements Runnable{
}
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
Map<String,ServerConnectionHandler> registry) {
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
Objects.requireNonNull(connection, "Connection must not be null");
Objects.requireNonNull(registry, "Registry must not be null");
this.connection = connection;
this.connectionRegistry = registry;
this.mutex = mutex;
this.nameComplete = nameComplete;
}
public String getUserName() {
@@ -55,28 +63,29 @@ public class ServerConnectionHandler implements Runnable{
}
public void startReceiving() {
System.out.println("Starting Connection Handler for new User");
try {
System.out.println("Start receiving data...");
while (connection.isAvailable()) {
String data = connection.receive();
processData(data);
System.out.println("Starting Connection Handler for new User");
try {
System.out.println("Start receiving data...");
while (connection.isAvailable()) {
String data = connection.receive();
processData(data);
}
System.out.println("Stopped recieving data");
} catch (SocketException e) {
System.out.println("Connection terminated locally");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (EOFException e) {
System.out.println("Connection terminated by remote");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (IOException e) {
System.err.println("Communication error: " + e);
} catch (ClassNotFoundException e) {
System.err.println("Received object of unknown type: " + e.getMessage());
}
System.out.println("Stopped recieving data");
} catch (SocketException e) {
System.out.println("Connection terminated locally");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (EOFException e) {
System.out.println("Connection terminated by remote");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch(IOException e) {
System.err.println("Communication error: " + e);
} catch(ClassNotFoundException e) {
System.err.println("Received object of unknown type: " + e.getMessage());
}
System.out.println("Stopping Connection Handler for " + userName);
System.out.println("Stopping Connection Handler for " + userName);
}
public void stopReceiving() {
@@ -124,7 +133,14 @@ public class ServerConnectionHandler implements Runnable{
if (sender == null || sender.isBlank()) sender = this.userName;
if (connectionRegistry.containsKey(sender))
throw new ChatProtocolException("User name already taken: " + sender);
this.userName = sender;
mutex.lock();
try {
this.userName = sender;
nameComplete.signal();
}
finally {
mutex.unlock();
}
connectionRegistry.put(userName, this);
sendData(USER_NONE, userName, DATA_TYPE_CONFIRM, "Registration successfull for " + userName);
this.state = CONNECTED;
@@ -149,6 +165,9 @@ public class ServerConnectionHandler implements Runnable{
ServerConnectionHandler handler = connectionRegistry.get(reciever);
if (handler != null) {
handler.sendData(sender, reciever, type, payload);
if(!reciever.equals(sender)){
sendData(sender, reciever, type, payload); //send message to sender if it's a direct message and sender is not receiver.
}
} else {
this.sendData(USER_NONE, userName, DATA_TYPE_ERROR, "Unknown User: " + reciever);
}