Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6fd5b569d | ||
|
|
b47d98b960 |
@@ -49,7 +49,7 @@ public class ChatWindowController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void applicationClose() {
|
private void applicationClose() {
|
||||||
disconnect();
|
connectionHandler.setState(DISCONNECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -106,6 +106,7 @@ public class ChatWindowController {
|
|||||||
|
|
||||||
private void startConnectionHandler() throws IOException {
|
private void startConnectionHandler() throws IOException {
|
||||||
String userName = userNameField.getText();
|
String userName = userNameField.getText();
|
||||||
|
if(!userName.contains(" ")) {
|
||||||
String serverAddress = serverAddressField.getText();
|
String serverAddress = serverAddressField.getText();
|
||||||
int serverPort = Integer.parseInt(serverPortField.getText());
|
int serverPort = Integer.parseInt(serverPortField.getText());
|
||||||
connectionHandler = new ClientConnectionHandler(
|
connectionHandler = new ClientConnectionHandler(
|
||||||
@@ -118,6 +119,9 @@ public class ChatWindowController {
|
|||||||
|
|
||||||
// register window close handler
|
// register window close handler
|
||||||
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
||||||
|
} else {
|
||||||
|
addError("It is not allowed to have spaces in username!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void terminateConnectionHandler() {
|
private void terminateConnectionHandler() {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class ClientConnectionHandler implements Runnable {
|
|||||||
public static final String USER_NONE = "";
|
public static final String USER_NONE = "";
|
||||||
public static final String USER_ALL = "*";
|
public static final String USER_ALL = "*";
|
||||||
|
|
||||||
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
|
private final Pattern messagePattern = Pattern.compile( "^(?:@(\\S*))?\\s*(.*)$" );
|
||||||
|
|
||||||
private SimpleStringProperty userName;
|
private SimpleStringProperty userName;
|
||||||
private SimpleObjectProperty<State> state;
|
private SimpleObjectProperty<State> state;
|
||||||
@@ -63,7 +63,6 @@ public class ClientConnectionHandler implements Runnable {
|
|||||||
|
|
||||||
public void setState (State newState) {
|
public void setState (State newState) {
|
||||||
state.set(newState);
|
state.set(newState);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run () {
|
public void run () {
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import java.io.IOException;
|
|||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.locks.Condition;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
@@ -64,34 +62,23 @@ public class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void start() {
|
private void start() {
|
||||||
ReentrantLock mutex = new ReentrantLock();
|
|
||||||
Condition nameComplete = mutex.newCondition();
|
|
||||||
System.out.println("Server started.");
|
System.out.println("Server started.");
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
|
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
|
||||||
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete);
|
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
|
||||||
new Thread(connectionHandler).start();
|
new Thread(connectionHandler).start();
|
||||||
mutex.lock();
|
|
||||||
try {
|
|
||||||
nameComplete.await();
|
|
||||||
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
|
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
|
||||||
connectionHandler.getUserName(),
|
connectionHandler.getUserName(),
|
||||||
connection.getRemoteHost(),
|
connection.getRemoteHost(),
|
||||||
connection.getRemotePort()
|
connection.getRemotePort()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
mutex.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(SocketException e) {
|
} catch(SocketException e) {
|
||||||
System.out.println("Server connection terminated");
|
System.out.println("Server connection terminated");
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
System.err.println("Communication error " + e);
|
System.err.println("Communication error " + e);
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
// close server
|
// close server
|
||||||
System.out.println("Server Stopped.");
|
System.out.println("Server Stopped.");
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
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.*;
|
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
|
||||||
|
|
||||||
@@ -32,10 +30,6 @@ public class ServerConnectionHandler implements Runnable{
|
|||||||
private static final String USER_NONE = "";
|
private static final String USER_NONE = "";
|
||||||
private static final String USER_ALL = "*";
|
private static final String USER_ALL = "*";
|
||||||
|
|
||||||
private ReentrantLock mutex;
|
|
||||||
|
|
||||||
private Condition nameComplete;
|
|
||||||
|
|
||||||
private String userName = "Anonymous-"+connectionId;
|
private String userName = "Anonymous-"+connectionId;
|
||||||
private State state = NEW;
|
private State state = NEW;
|
||||||
|
|
||||||
@@ -49,13 +43,11 @@ public class ServerConnectionHandler implements Runnable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
||||||
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
|
Map<String,ServerConnectionHandler> registry) {
|
||||||
Objects.requireNonNull(connection, "Connection must not be null");
|
Objects.requireNonNull(connection, "Connection must not be null");
|
||||||
Objects.requireNonNull(registry, "Registry must not be null");
|
Objects.requireNonNull(registry, "Registry must not be null");
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.connectionRegistry = registry;
|
this.connectionRegistry = registry;
|
||||||
this.mutex = mutex;
|
|
||||||
this.nameComplete = nameComplete;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
@@ -79,13 +71,12 @@ public class ServerConnectionHandler implements Runnable{
|
|||||||
System.out.println("Connection terminated by remote");
|
System.out.println("Connection terminated by remote");
|
||||||
connectionRegistry.remove(userName);
|
connectionRegistry.remove(userName);
|
||||||
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
|
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch(IOException e) {
|
||||||
System.err.println("Communication error: " + e);
|
System.err.println("Communication error: " + e);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch(ClassNotFoundException e) {
|
||||||
System.err.println("Received object of unknown type: " + e.getMessage());
|
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() {
|
public void stopReceiving() {
|
||||||
@@ -133,14 +124,7 @@ public class ServerConnectionHandler implements Runnable{
|
|||||||
if (sender == null || sender.isBlank()) sender = this.userName;
|
if (sender == null || sender.isBlank()) sender = this.userName;
|
||||||
if (connectionRegistry.containsKey(sender))
|
if (connectionRegistry.containsKey(sender))
|
||||||
throw new ChatProtocolException("User name already taken: " + sender);
|
throw new ChatProtocolException("User name already taken: " + sender);
|
||||||
mutex.lock();
|
|
||||||
try {
|
|
||||||
this.userName = sender;
|
this.userName = sender;
|
||||||
nameComplete.signal();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
mutex.unlock();
|
|
||||||
}
|
|
||||||
connectionRegistry.put(userName, this);
|
connectionRegistry.put(userName, this);
|
||||||
sendData(USER_NONE, userName, DATA_TYPE_CONFIRM, "Registration successfull for " + userName);
|
sendData(USER_NONE, userName, DATA_TYPE_CONFIRM, "Registration successfull for " + userName);
|
||||||
this.state = CONNECTED;
|
this.state = CONNECTED;
|
||||||
|
|||||||
Reference in New Issue
Block a user