Fixed whitespaces ChatWindow.fxml other javadocs in other classes aswell deleted unneccessary declarations
This commit is contained in:
parent
154b9d435d
commit
8cb83ef7ef
|
@ -23,6 +23,7 @@ import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
* To Contact the Model Elements needed it also holds references to message and the Connectionhandler.
|
* To Contact the Model Elements needed it also holds references to message and the Connectionhandler.
|
||||||
*/
|
*/
|
||||||
public class ChatWindowController {
|
public class ChatWindowController {
|
||||||
|
public Button sendButton; //TODO necessary to have a attribute when not used or delete?
|
||||||
private ClientConnectionHandler connectionHandler;
|
private ClientConnectionHandler connectionHandler;
|
||||||
private ClientMessageList messages;
|
private ClientMessageList messages;
|
||||||
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
|
private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler();
|
||||||
|
@ -156,10 +157,6 @@ public class ChatWindowController {
|
||||||
connectionHandler.initialize(serverAddress, serverPort, userName);
|
connectionHandler.initialize(serverAddress, serverPort, userName);
|
||||||
new Thread(connectionHandler).start();
|
new Thread(connectionHandler).start();
|
||||||
|
|
||||||
//register Listener //TODO what todo with methods?
|
|
||||||
//startConnectionHandlerListener();
|
|
||||||
|
|
||||||
// register window close handler
|
|
||||||
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
|
||||||
} else {
|
} else {
|
||||||
addError("It is not allowed to have spaces in username!");
|
addError("It is not allowed to have spaces in username!");
|
||||||
|
@ -240,6 +237,7 @@ public class ChatWindowController {
|
||||||
* Nested Class in charge of Closing the wind
|
* Nested Class in charge of Closing the wind
|
||||||
*/
|
*/
|
||||||
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* //TODO
|
* //TODO
|
||||||
*
|
*
|
||||||
|
|
|
@ -12,7 +12,9 @@
|
||||||
<?import javafx.scene.layout.HBox?>
|
<?import javafx.scene.layout.HBox?>
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
|
||||||
<BorderPane fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0" prefWidth="420.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
|
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0"
|
||||||
|
prefWidth="420.0" xmlns="http://javafx.com/javafx/18"
|
||||||
|
fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
|
||||||
<top>
|
<top>
|
||||||
<VBox BorderPane.alignment="CENTER">
|
<VBox BorderPane.alignment="CENTER">
|
||||||
<children>
|
<children>
|
||||||
|
@ -31,20 +33,25 @@
|
||||||
</MenuBar>
|
</MenuBar>
|
||||||
<HBox fillHeight="false" spacing="5.0">
|
<HBox fillHeight="false" spacing="5.0">
|
||||||
<children>
|
<children>
|
||||||
<TextField fx:id="userNameField" alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308" minWidth="110.0" promptText="Username" HBox.hgrow="SOMETIMES" />
|
<TextField fx:id="userNameField" alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308"
|
||||||
<Label alignment="CENTER" contentDisplay="CENTER" text="\@" textAlignment="CENTER" textOverrun="CLIP" HBox.hgrow="NEVER">
|
minWidth="110.0" promptText="Username" HBox.hgrow="SOMETIMES"/>
|
||||||
|
<Label alignment="CENTER" contentDisplay="CENTER" text="\@" textAlignment="CENTER"
|
||||||
|
textOverrun="CLIP" HBox.hgrow="NEVER">
|
||||||
<HBox.margin>
|
<HBox.margin>
|
||||||
<Insets bottom="5.0" top="5.0"/>
|
<Insets bottom="5.0" top="5.0"/>
|
||||||
</HBox.margin>
|
</HBox.margin>
|
||||||
</Label>
|
</Label>
|
||||||
<TextField fx:id="serverAddressField" alignment="CENTER_RIGHT" minWidth="110.0" promptText="Host" HBox.hgrow="SOMETIMES" />
|
<TextField fx:id="serverAddressField" alignment="CENTER_RIGHT" minWidth="110.0"
|
||||||
|
promptText="Host" HBox.hgrow="SOMETIMES"/>
|
||||||
<Label text=":" HBox.hgrow="NEVER">
|
<Label text=":" HBox.hgrow="NEVER">
|
||||||
<HBox.margin>
|
<HBox.margin>
|
||||||
<Insets bottom="5.0" top="5.0"/>
|
<Insets bottom="5.0" top="5.0"/>
|
||||||
</HBox.margin>
|
</HBox.margin>
|
||||||
</Label>
|
</Label>
|
||||||
<TextField fx:id="serverPortField" minWidth="-Infinity" prefWidth="60.0" promptText="Port" HBox.hgrow="NEVER" />
|
<TextField fx:id="serverPortField" minWidth="-Infinity" prefWidth="60.0" promptText="Port"
|
||||||
<Button fx:id="connectButton" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#toggleConnection" prefWidth="80.0" text="Connect" HBox.hgrow="NEVER">
|
HBox.hgrow="NEVER"/>
|
||||||
|
<Button fx:id="connectButton" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false"
|
||||||
|
onAction="#toggleConnection" prefWidth="80.0" text="Connect" HBox.hgrow="NEVER">
|
||||||
<HBox.margin>
|
<HBox.margin>
|
||||||
<Insets left="5.0"/>
|
<Insets left="5.0"/>
|
||||||
</HBox.margin>
|
</HBox.margin>
|
||||||
|
@ -64,7 +71,8 @@
|
||||||
<HBox spacing="5.0">
|
<HBox spacing="5.0">
|
||||||
<children>
|
<children>
|
||||||
<TextField fx:id="messageField" onAction="#message" HBox.hgrow="ALWAYS"/>
|
<TextField fx:id="messageField" onAction="#message" HBox.hgrow="ALWAYS"/>
|
||||||
<Button fx:id="sendButton" alignment="CENTER" maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#message" prefWidth="50.0" text="Send" textAlignment="CENTER">
|
<Button fx:id="sendButton" alignment="CENTER" maxWidth="-Infinity" minWidth="-Infinity"
|
||||||
|
mnemonicParsing="false" onAction="#message" prefWidth="50.0" text="Send" textAlignment="CENTER">
|
||||||
<HBox.margin>
|
<HBox.margin>
|
||||||
<Insets left="5.0"/>
|
<Insets left="5.0"/>
|
||||||
</HBox.margin>
|
</HBox.margin>
|
||||||
|
|
|
@ -120,12 +120,10 @@ public abstract class ConnectionHandler {
|
||||||
protected void sendData(String sender, String receiver, String type, String payload) {
|
protected void sendData(String sender, String receiver, String type, String payload) {
|
||||||
if (connection.isAvailable()) {
|
if (connection.isAvailable()) {
|
||||||
new StringBuilder();
|
new StringBuilder();
|
||||||
String data = new StringBuilder()
|
String data = sender + "\n" +
|
||||||
.append(sender + "\n")
|
receiver + "\n" +
|
||||||
.append(receiver + "\n")
|
type + "\n" +
|
||||||
.append(type + "\n")
|
payload + "\n";
|
||||||
.append(payload + "\n")
|
|
||||||
.toString();
|
|
||||||
try {
|
try {
|
||||||
connection.send(data);
|
connection.send(data);
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
|
|
|
@ -6,8 +6,7 @@ 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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Class represents a Server. The user can start the programm with the port number as a argument.
|
* This Class represents a Server. The user can start the programm with the port number as a argument.
|
||||||
|
@ -84,13 +83,11 @@ public class Server {
|
||||||
* The connection will be registered in the connection registry if successful.
|
* The connection will be registered in the connection registry if successful.
|
||||||
*/
|
*/
|
||||||
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();
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
|
|
|
@ -14,7 +14,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 java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,10 +47,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
private final int connectionId = connectionCounter.incrementAndGet();
|
private final int connectionId = connectionCounter.incrementAndGet();
|
||||||
private final Map<String, ServerConnectionHandler> connectionRegistry;
|
private final Map<String, ServerConnectionHandler> connectionRegistry;
|
||||||
|
|
||||||
private final ReentrantLock mutex;
|
|
||||||
|
|
||||||
private final Condition nameComplete;
|
|
||||||
|
|
||||||
private String userName = "Anonymous-" + connectionId;
|
private String userName = "Anonymous-" + connectionId;
|
||||||
private State state = NEW;
|
private State state = NEW;
|
||||||
|
|
||||||
|
@ -68,21 +63,19 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
*
|
*
|
||||||
* @param connection representing the socket connection between server and client
|
* @param connection representing the socket connection between server and client
|
||||||
* @param registry map containing all active connections between server and clients
|
* @param registry map containing all active connections between server and clients
|
||||||
* @param mutex to lock thread
|
|
||||||
* @param nameComplete condition to call threads
|
|
||||||
*/
|
*/
|
||||||
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
||||||
Map<String, ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
|
Map<String, ServerConnectionHandler> registry) {
|
||||||
super();
|
super();
|
||||||
setConnection(connection);
|
setConnection(connection);
|
||||||
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.connectionRegistry = registry;
|
this.connectionRegistry = registry;
|
||||||
this.mutex = mutex;
|
|
||||||
this.nameComplete = nameComplete;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** //TODO needed method?
|
||||||
* @return the username of the connected client
|
* @return the username of the connected client
|
||||||
*/
|
*/
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
|
|
Loading…
Reference in New Issue