Browse Source

Implemented some protocall stuff in tandem with server development

cam
Cameron Weinfurt 2 years ago
parent
commit
1cb8627940
  1. 42
      window/ChatWindow.java
  2. 32
      window/WindowProtoHandler.java

42
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,6 +28,8 @@ 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 {
@ -34,20 +37,20 @@ public class ChatWindow extends Application {
public ChatWindow(String servername, String username) throws IOException {
this.servername = servername;
this.username = username;
this.userList = new ArrayList<>();
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);
Socket socket = new Socket(servername, 25519);
protoOut = new ProtoOut(new DataOutputStream(socket.getOutputStream()));
protoOut.sendJoin(username);
protoIn = new ProtoIn(new DataInputStream(socket.getInputStream()), windowProtoHandler);
protoIn.init();
}
public ChatWindow() throws IOException {
this("Serverless Chat Window", "Localhost");
this("128.153.144.21", "Cam");
}
@ -88,6 +91,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 +185,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,13 +212,22 @@ public class ChatWindow extends Application {
root.getChildren().addAll(statusBar, inputPane, usersPane, messagePane_scroll);
primaryStage.show();
try {
protoOut.requestUserList(username);
} catch (IOException e) {
e.printStackTrace();
}
}
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 ProtoOut protoOut;

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