Browse Source

Merge branch 'cam'

proto
Thomas Johnson 2 years ago
parent
commit
27fe787107
  1. 61
      window/ChatWindow.java
  2. 105
      window/LoginWindow.java
  3. 32
      window/WindowProtoHandler.java

61
window/ChatWindow.java

@ -4,6 +4,7 @@ import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
@ -27,30 +28,28 @@ import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
public class ChatWindow extends Application {
public ChatWindow(String servername, String username) throws IOException {
public ChatWindow(String servername, String username, Socket socket, LoginWindow loginWindow) throws IOException {
this.servername = servername;
this.username = username;
this.userList = new ArrayList<>();
this.socket = socket;
this.loginWindow = loginWindow;
windowProtoHandler = new WindowProtoHandler(this);
WindowProtoHandler windowProtoHandler = new WindowProtoHandler(this);
ServerSocket serverSocket = new ServerSocket(25519);
Socket socketOut = new Socket("127.0.0.1", 25519);
Socket socketIn = serverSocket.accept();
protoOut = new ProtoOut(new DataOutputStream(socketOut.getOutputStream()));
protoIn = new ProtoIn(new DataInputStream(socketIn.getInputStream()), windowProtoHandler);
protoOut = new ProtoOut(new DataOutputStream(socket.getOutputStream()));
protoOut.sendJoin(username);
ProtoIn protoIn = new ProtoIn(new DataInputStream(socket.getInputStream()), windowProtoHandler);
protoIn.init();
}
public ChatWindow() throws IOException {
this("Serverless Chat Window", "Localhost");
}
private void printHelp() {
addMessage("Client", "The following commands are implemented:\n" +
"\t\"/download <filenumber>\": Downloads the nth file that has been uploaded to the server.\n" +
@ -88,6 +87,15 @@ public class ChatWindow extends Application {
}
}
public void updateUserList() {
Platform.runLater(()-> {
userListLabel.setText("");
userList.forEach(u -> {
userListLabel.setText(userListLabel.getText() + "[" + u + "]\n");
});
});
}
public void addMessage(String user, String message) { Platform.runLater(() -> addMessage(user, message, true)); }
public void addMessage(String user, String message, boolean doTimestamp) {
@ -173,12 +181,19 @@ public class ChatWindow extends Application {
inputBox.prefWidthProperty().bind(inputPane.widthProperty().subtract(inputButton.prefWidthProperty()).subtract(uploadButton.prefWidthProperty()).subtract(24.0));
inputPane.getChildren().addAll(inputBox, inputButton);
FlowPane usersPane = new FlowPane();
ScrollPane usersPane = new ScrollPane();
AnchorPane.setTopAnchor(usersPane, 0.0);
AnchorPane.setRightAnchor(usersPane, 0.0);
usersPane.prefWidthProperty().setValue(300);
usersPane.prefHeightProperty().bind(root.heightProperty().subtract(statusBar.heightProperty()).subtract(inputPane.heightProperty()));
userListLabel = new Label();
userListLabel.prefWidthProperty().bind(usersPane.prefWidthProperty().subtract(18.0));
userListLabel.translateXProperty().setValue(6.0);
userListLabel.translateYProperty().setValue(6.0);
userListLabel.setAlignment(Pos.TOP_LEFT);
usersPane.setContent(userListLabel);
ScrollPane messagePane_scroll = new ScrollPane();
AnchorPane.setTopAnchor(messagePane_scroll, 0.0);
AnchorPane.setLeftAnchor(messagePane_scroll, 0.0);
@ -193,16 +208,30 @@ public class ChatWindow extends Application {
root.getChildren().addAll(statusBar, inputPane, usersPane, messagePane_scroll);
primaryStage.show();
try {
protoOut.requestUserList(username);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void stop() throws IOException {
protoOut.sendPart(username);
socket.close();
loginWindow.show();
}
public static void main(String[] args) {launch(args);}
public ArrayList<String> userList;
private String servername;
private String username;
private FlowPane messagePane;
private Label userListLabel;
private Stage stage;
private Socket socket;
private ProtoOut protoOut;
private ProtoIn protoIn;
private WindowProtoHandler windowProtoHandler;
private LoginWindow loginWindow;
}

105
window/LoginWindow.java

@ -0,0 +1,105 @@
package window;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
public class LoginWindow extends Application {
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
FlowPane root = new FlowPane();
stage.setMaximized(true);
stage.setTitle("Clype Login");
stage.setScene(new Scene(root, 345, 100));
root.setPrefHeight(70);
root.setPrefWidth(345);
Label servernameLabel = new Label(" Server name and port: ");
TextField servernameTextField = new TextField();
Label usernameLabel = new Label(" Username: ");
TextField usernameTextField = new TextField();
Button connectButton = new Button("Connect");
servernameTextField.setTranslateY(6);
usernameTextField.setTranslateY(6);
servernameLabel.setTranslateY(6);
usernameLabel.setTranslateY(6);
connectButton.setTranslateX(6);
connectButton.setTranslateY(6);
connectButton.setOnMouseClicked(mouseEvent -> {
String[] servernameAndPort = parseServernameAndPort(servernameTextField.getText());
int port;
try {
port = Integer.parseInt(servernameAndPort[1]);
if (port > 65536 || port < 0) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Parsing error");
alert.setContentText("The port you specified isn't valid! It must be between 0 and 65535");
alert.show();
}
else {
Socket socket = new Socket(servernameAndPort[0], port);
ChatWindow chatWindow = new ChatWindow(servernameAndPort[0], usernameTextField.getText(), socket, this);
Stage chatWindowStage = new Stage();
chatWindow.start(chatWindowStage);
}
} catch (NumberFormatException nfe) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Parsing error");
alert.setContentText("The port you specified isn't an integer!");
alert.show();
} catch (UnknownHostException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Unknown Host");
alert.setContentText("This server does not exist.");
alert.show();
} catch(ConnectException ce) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Connection Problems");
alert.setContentText("There was an issue connecting to the server.");
alert.show();
} catch (IOException e) {
e.printStackTrace();
}
stage.hide();
});
root.getChildren().addAll(servernameLabel, servernameTextField, usernameLabel, usernameTextField, connectButton);
stage.show();
}
public void show() {
stage.show();
}
private String[] parseServernameAndPort(String servernameAndPort) {
int colonLoc = servernameAndPort.indexOf(':');
if (colonLoc == -1) {
return new String[]{servernameAndPort, "25519"};
}
else {
return new String[]{servernameAndPort.substring(0, colonLoc - 1), servernameAndPort.substring(colonLoc + 1)};
}
}
void main() {launch();}
private Stage stage;
}

32
window/WindowProtoHandler.java

@ -1,5 +1,6 @@
package window;
import javafx.application.Platform;
import proto.ProtoHandler;
import java.util.ArrayList;
@ -12,47 +13,64 @@ public class WindowProtoHandler implements ProtoHandler {
@Override
public void handleError(String username, byte[] msg) {
Platform.runLater(() -> {
});
}
@Override
public void handleJoin(String username) {
Platform.runLater(() -> {
chatWindow.addMessage("Server", "User \"" + username + "\" has joined.");
chatWindow.userList.add(username);
chatWindow.updateUserList();
});
}
@Override
public void handlePart(String username) {
Platform.runLater(() -> {
chatWindow.addMessage("Server", "User \"" + username + "\" has left.");
chatWindow.userList.remove(username);
chatWindow.updateUserList();
});
}
@Override
public void handleListing(String username, ArrayList<String> names) {
Platform.runLater(() -> {
chatWindow.userList = names;
chatWindow.updateUserList();
});
}
@Override
public void handleListingRequest(String username) {
}
public void handleListingRequest(String username) {}
@Override
public void handleMessage(String username, String message) {
chatWindow.addMessage(username, message);
Platform.runLater(()-> chatWindow.addMessage(username, message));
}
@Override
public void handleFile(String username, String filename, byte[] data) {
Platform.runLater(() -> {
});
}
@Override
public void handleUnknownCommand(int length, byte id, String username) {
Platform.runLater(() -> {
});
}
@Override
public void handleClose() {
Platform.runLater(() -> {
});
}
private ChatWindow chatWindow;

Loading…
Cancel
Save