Browse Source

Files are sendable now!

cam
Cameron Weinfurt 2 years ago
parent
commit
a925425be4
  1. 34
      window/ChatWindow.java
  2. 22
      window/WindowProtoHandler.java

34
window/ChatWindow.java

@ -50,13 +50,6 @@ public class ChatWindow extends Application {
protoIn.init();
}
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" +
"\t\"/help\": Prints this help.\n" +
"", false);
}
private int parseArgForInt(String s) {
int r = Integer.MIN_VALUE;
@ -67,26 +60,6 @@ public class ChatWindow extends Application {
return r;
}
private void handleInputText(String inputText) throws IOException {
if (inputText.startsWith("/")) {
String[] args = inputText.split(" ");
if (inputText.startsWith("/download")) {
if (args.length == 1) {
addMessage("Client", "\"/download\" takes one argument.", false);
}
} else if (inputText.startsWith("/help")) {
printHelp();
} else {
addMessage("Client", "Command \"" + inputText.substring(1) + "\" is not recognized", false);
printHelp();
}
}
else {
addMessage(username, inputText);
protoOut.sendMessage(username, inputText);
}
}
public void updateUserList() {
Platform.runLater(()-> {
userListLabel.setText("");
@ -148,7 +121,8 @@ public class ChatWindow extends Application {
inputBox.setOnKeyReleased(keyEvent -> {
if (keyEvent.getCode().equals(KeyCode.ENTER) && !inputBox.getText().equals("")) {
try {
handleInputText(inputBox.getText());
addMessage(username, inputBox.getText());
protoOut.sendMessage(username, inputBox.getText());
} catch (IOException e) {
e.printStackTrace();
}
@ -166,6 +140,7 @@ public class ChatWindow extends Application {
File file = fileChooser.showOpenDialog(stage);
try {
protoOut.sendFile(username, file.getName(), Files.readAllBytes(file.toPath()));
addMessage("Client", "File \"" + file.getName() + "\" sent to other clients.");
} catch (IOException e) {
e.printStackTrace();
}
@ -179,7 +154,8 @@ public class ChatWindow extends Application {
inputButton.setOnMouseClicked(mouseEvent -> {
if (!inputBox.getText().equals("")) {
try {
handleInputText(inputBox.getText());
addMessage(username, inputBox.getText());
protoOut.sendMessage(username, inputBox.getText());
} catch (IOException e) {
e.printStackTrace();
}

22
window/WindowProtoHandler.java

@ -1,8 +1,13 @@
package window;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import proto.ProtoHandler;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AlgorithmConstraints;
import java.util.ArrayList;
public class WindowProtoHandler implements ProtoHandler {
@ -55,7 +60,13 @@ public class WindowProtoHandler implements ProtoHandler {
@Override
public void handleFile(String username, String filename, byte[] data) {
Platform.runLater(() -> {
try {
chatWindow.addMessage("Server", "User [" + username + "] has sent the file \"" + filename + "\".");
FileOutputStream fileOutputStream = new FileOutputStream(filename);
fileOutputStream.write(data);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@ -69,7 +80,14 @@ public class WindowProtoHandler implements ProtoHandler {
@Override
public void handleClose() {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Server has closed");
alert.setContentText("The server has closed the connection with the client.");
try {
chatWindow.stop();
} catch (Exception e) {
e.printStackTrace();
}
});
}

Loading…
Cancel
Save