Browse Source

Start of Window and Proto interop. Accidental push to master

proto
Cameron Weinfurt 2 years ago
parent
commit
702f9780e2
  1. 55
      window/ChatWindow.java
  2. 59
      window/WindowProtoHandler.java

55
window/ChatWindow.java

@ -17,23 +17,39 @@ import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import proto.ProtoID;
import proto.ProtoIn;
import proto.ProtoOut;
import java.awt.*;
import java.io.File;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.Date;
public class ChatWindow extends Application {
public ChatWindow() {
this.servername = "Serverless Chat Window";
this.username = "Localhost";
}
public ChatWindow(String servername, String username) {
public ChatWindow(String servername, String username) throws IOException {
this.servername = servername;
this.username = username;
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);
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" +
@ -51,7 +67,7 @@ public class ChatWindow extends Application {
return r;
}
private void handleInputText(String inputText) {
private void handleInputText(String inputText) throws IOException {
if (inputText.startsWith("/")) {
String[] args = inputText.split(" ");
if (inputText.startsWith("/download")) {
@ -67,6 +83,7 @@ public class ChatWindow extends Application {
}
else {
addMessage(username, inputText);
protoOut.sendMessage(username, inputText);
}
}
@ -109,7 +126,11 @@ public class ChatWindow extends Application {
inputBox.setOnKeyReleased(keyEvent -> {
if (keyEvent.getCode().equals(KeyCode.ENTER) && !inputBox.getText().equals("")) {
handleInputText(inputBox.getText()); // Change to sending this to the server when we have the server set up.
try {
handleInputText(inputBox.getText());
} catch (IOException e) {
e.printStackTrace();
}
inputBox.clear();
}
});
@ -122,7 +143,11 @@ public class ChatWindow extends Application {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("File Upload");
File file = fileChooser.showOpenDialog(stage);
//Do the rest of the upload.
try {
protoOut.sendFile(username, file.getName(), Files.readAllBytes(file.toPath()));
} catch (IOException e) {
e.printStackTrace();
}
});
inputPane.getChildren().add(uploadButton);
@ -132,7 +157,11 @@ public class ChatWindow extends Application {
inputButton.prefWidthProperty().setValue(80);
inputButton.setOnMouseClicked(mouseEvent -> {
if (!inputBox.getText().equals("")) {
handleInputText(inputBox.getText()); // Change to sending this to the server when we have the server set up.
try {
handleInputText(inputBox.getText());
} catch (IOException e) {
e.printStackTrace();
}
inputBox.clear();
}
});
@ -168,4 +197,8 @@ public class ChatWindow extends Application {
private String username;
private FlowPane messagePane;
private Stage stage;
private ProtoOut protoOut;
private ProtoIn protoIn;
private WindowProtoHandler windowProtoHandler;
}

59
window/WindowProtoHandler.java

@ -0,0 +1,59 @@
package window;
import proto.ProtoHandler;
import java.util.ArrayList;
public class WindowProtoHandler implements ProtoHandler {
public WindowProtoHandler(ChatWindow chatWindow) {
this.chatWindow = chatWindow;
}
@Override
public void handleError(String username, byte[] msg) {
}
@Override
public void handleJoin(String username) {
}
@Override
public void handlePart(String username) {
}
@Override
public void handleListing(String username, ArrayList<String> names) {
}
@Override
public void handleListingRequest(String username) {
}
@Override
public void handleMessage(String username, String message) {
chatWindow.addMessage(username, message);
}
@Override
public void handleFile(String username, String filename, byte[] data) {
}
@Override
public void handleUnknownCommand(int length, byte id, String username) {
}
@Override
public void handleClose() {
}
private ChatWindow chatWindow;
}
Loading…
Cancel
Save