diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..82969bc --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_binding_binding_2_12_11_9_0_56_3af5a32a.xml b/.idea/libraries/Maven__com_thoughtworks_binding_binding_2_12_11_9_0_56_3af5a32a.xml new file mode 100644 index 0000000..78df4ea --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_binding_binding_2_12_11_9_0_56_3af5a32a.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_binding_fxml_2_12_11_9_0_56_3af5a32a.xml b/.idea/libraries/Maven__com_thoughtworks_binding_fxml_2_12_11_9_0_56_3af5a32a.xml new file mode 100644 index 0000000..d926942 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_binding_fxml_2_12_11_9_0_56_3af5a32a.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_binding_safebuffer_2_12_11_9_0_56_3af5a32a.xml b/.idea/libraries/Maven__com_thoughtworks_binding_safebuffer_2_12_11_9_0_56_3af5a32a.xml new file mode 100644 index 0000000..3808b27 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_binding_safebuffer_2_12_11_9_0_56_3af5a32a.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_binding_xmlextractor_2_12_11_9_0_56_3af5a32a.xml b/.idea/libraries/Maven__com_thoughtworks_binding_xmlextractor_2_12_11_9_0_56_3af5a32a.xml new file mode 100644 index 0000000..a367764 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_binding_xmlextractor_2_12_11_9_0_56_3af5a32a.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_enableIf_enableif_2_12_1_1_7.xml b/.idea/libraries/Maven__com_thoughtworks_enableIf_enableif_2_12_1_1_7.xml new file mode 100644 index 0000000..a23baed --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_enableIf_enableif_2_12_1_1_7.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_extractor_extractor_2_12_2_1_2.xml b/.idea/libraries/Maven__com_thoughtworks_extractor_extractor_2_12_2_1_2.xml new file mode 100644 index 0000000..81e93f5 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_extractor_extractor_2_12_2_1_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_q_q_2_12_1_0_4.xml b/.idea/libraries/Maven__com_thoughtworks_q_q_2_12_1_0_4.xml new file mode 100644 index 0000000..6f2f6cc --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_q_q_2_12_1_0_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_sde_core_2_12_3_3_2.xml b/.idea/libraries/Maven__com_thoughtworks_sde_core_2_12_3_3_2.xml new file mode 100644 index 0000000..57dc649 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_sde_core_2_12_3_3_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml new file mode 100644 index 0000000..9050e00 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_base_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_base_11.xml new file mode 100644 index 0000000..88eebdf --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_base_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_base_mac_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_base_mac_11.xml new file mode 100644 index 0000000..f8b66bd --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_base_mac_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_controls_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_controls_11.xml new file mode 100644 index 0000000..40e5c7e --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_controls_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_controls_mac_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_controls_mac_11.xml new file mode 100644 index 0000000..a2793fc --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_controls_mac_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_fxml_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_fxml_11.xml new file mode 100644 index 0000000..3eaa3ca --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_fxml_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11.xml new file mode 100644 index 0000000..e8a768e --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_graphics_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_graphics_11.xml new file mode 100644 index 0000000..adbd0c7 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_graphics_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11.xml b/.idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11.xml new file mode 100644 index 0000000..b04140a --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_scala_lang_scala_library_2_12_10.xml b/.idea/libraries/Maven__org_scala_lang_scala_library_2_12_10.xml new file mode 100644 index 0000000..80b67f7 --- /dev/null +++ b/.idea/libraries/Maven__org_scala_lang_scala_library_2_12_10.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_scala_lang_scala_reflect_2_12_10.xml b/.idea/libraries/Maven__org_scala_lang_scala_reflect_2_12_10.xml new file mode 100644 index 0000000..ab4bd03 --- /dev/null +++ b/.idea/libraries/Maven__org_scala_lang_scala_reflect_2_12_10.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_scalaz_scalaz_core_2_12_7_2_27.xml b/.idea/libraries/Maven__org_scalaz_scalaz_core_2_12_7_2_27.xml new file mode 100644 index 0000000..04916b7 --- /dev/null +++ b/.idea/libraries/Maven__org_scalaz_scalaz_core_2_12_7_2_27.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_scalaz_scalaz_effect_2_12_7_2_27.xml b/.idea/libraries/Maven__org_scalaz_scalaz_effect_2_12_7_2_27.xml new file mode 100644 index 0000000..71bd09a --- /dev/null +++ b/.idea/libraries/Maven__org_scalaz_scalaz_effect_2_12_7_2_27.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_typelevel_macro_compat_2_12_1_1_1.xml b/.idea/libraries/Maven__org_typelevel_macro_compat_2_12_1_1_1.xml new file mode 100644 index 0000000..6a3194c --- /dev/null +++ b/.idea/libraries/Maven__org_typelevel_macro_compat_2_12_1_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_xerial_sqlite_jdbc_3_36_0_3.xml b/.idea/libraries/Maven__org_xerial_sqlite_jdbc_3_36_0_3.xml new file mode 100644 index 0000000..35d2f81 --- /dev/null +++ b/.idea/libraries/Maven__org_xerial_sqlite_jdbc_3_36_0_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2e289ef --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3e9894b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..83bc6dc --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,266 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1646087030962 + + + 1646256709011 + + + 1646256984318 + + + 1646257098309 + + + 1646489511920 + + + 1646525502805 + + + 1646839660802 + + + 1647828285514 + + + 1648467015982 + + + 1648899327288 + + + 1649376819371 + + + 1649719077520 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chatty.iml b/Chatty.iml new file mode 100644 index 0000000..f409c0e --- /dev/null +++ b/Chatty.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Client/Client.iml b/Client/Client.iml new file mode 100644 index 0000000..03fe45e --- /dev/null +++ b/Client/Client.iml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Client/pom.xml b/Client/pom.xml index a47135e..c7dfb9d 100644 --- a/Client/pom.xml +++ b/Client/pom.xml @@ -15,5 +15,44 @@ 8 8 + + + + + com.zenjava + javafx-maven-plugin + 2.0 + + Main + + + + + + + com.thoughtworks.binding + fxml_2.12 + 11.9.0+56-3af5a32a + + + org.openjfx + javafx-fxml + 11 + + + org.example + Command + 1.0-SNAPSHOT + compile + + + org.example + Server + 1.0-SNAPSHOT + compile + + + + \ No newline at end of file diff --git a/Client/src/main/java/Client.java b/Client/src/main/java/Client.java deleted file mode 100644 index c64e2de..0000000 --- a/Client/src/main/java/Client.java +++ /dev/null @@ -1,57 +0,0 @@ -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.net.Socket; -import java.util.Scanner; - -public class Client { - private static final int PORT = 8189; - private static final String ADDRESS = "localhost"; - - public static void main(String[] args) { - Socket socket = null; - Scanner scanner = new Scanner(System.in); - try{ - socket = new Socket(ADDRESS,PORT); - System.out.println("Connected to server:" + socket.getRemoteSocketAddress()); - DataInputStream in = new DataInputStream(socket.getInputStream()); - DataOutputStream out = new DataOutputStream(socket.getOutputStream()); - - - Thread thread = new Thread(()->{ - while(true) { - try { - out.writeUTF(scanner.nextLine()); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - thread.setDaemon(true); - thread.start(); - - while(true){ - String str = in.readUTF(); - if(str.equals("/end")){ - System.out.println("lost connection to the server..."); - out.writeUTF("/end"); - break; - - }else{ - System.out.println("Server : " + str); - } - } - - } catch (IOException e) { - e.printStackTrace(); - }finally { - try { - socket.close(); - } catch (IOException | NullPointerException e) { - e.printStackTrace(); - } - } - } - -} \ No newline at end of file diff --git a/Client/src/main/java/Controller.java b/Client/src/main/java/Controller.java new file mode 100644 index 0000000..02d012f --- /dev/null +++ b/Client/src/main/java/Controller.java @@ -0,0 +1,249 @@ +import constants.Command; +import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.ListView; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.HBox; +import javafx.stage.Modality; +import javafx.stage.Stage; +import javafx.stage.StageStyle; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.URL; +import java.util.ResourceBundle; + +public class Controller implements Initializable { + @FXML + public TextField textField; + @FXML + public TextArea textArea; + @FXML + public TextField loginField; + @FXML + public PasswordField passwordField; + @FXML + public HBox authPanel; + @FXML + public HBox msgPanel; + @FXML + public ListView clientList; + + private Socket socket; + private static final int PORT = 8189; + private static final String ADDRESS ="localhost"; + + private DataInputStream in; + private DataOutputStream out; + + private boolean authenticated; + private String nickname; + private Stage stage; + private Stage regStage; + private RegController regController; + private String login; + + public void setAuthenticated(boolean authenticated) { + this.authenticated = authenticated; + authPanel.setVisible(!authenticated); + authPanel.setManaged(!authenticated); + msgPanel.setVisible(authenticated); + msgPanel.setManaged(authenticated); + clientList.setVisible(authenticated); + clientList.setManaged(authenticated); + + if (!authenticated){ + nickname = ""; + History.stop(); + } + + textArea.clear(); + setTitle(nickname); + } + + @Override + public void initialize(URL location, ResourceBundle resources) { + Platform.runLater(() -> { + stage = (Stage) textField.getScene().getWindow(); + stage.setOnCloseRequest(event -> { + System.out.println("bye"); + if (socket != null && !socket.isClosed()) { + try { + out.writeUTF(Command.END); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + }); + setAuthenticated(false); + } + + private void connect() { + try { + socket = new Socket(ADDRESS, PORT); + in = new DataInputStream(socket.getInputStream()); + out = new DataOutputStream(socket.getOutputStream()); + + new Thread(() -> { + try { + //цикл аутентификации + while (true) { + String str = in.readUTF(); + + if (str.startsWith("/")) { + if (str.equals(Command.END)) { + break; + } + if (str.startsWith(Command.AUTH_OK)) { + nickname = str.split(" ")[1]; + setAuthenticated(true); + textArea.appendText(History.getLastHundredLinesOfHistory(login)); + History.start(login); + break; + } + if (str.equals(Command.REG_OK)||str.equals(Command.REG_NO)){ + regController.result(str); + } + } else { + textArea.appendText(str + "\n"); + } + } + //цикл работы + while (authenticated) { + String str = in.readUTF(); + + if (str.startsWith("/")) { + if (str.equals(Command.END)) { + break; + } + if (str.startsWith(Command.CLIENTLIST)) { + String[] token = str.split(" "); + + Platform.runLater(()->{ + clientList.getItems().clear(); + for (int i=1;i { + stage.setTitle(title); + }); + } + + public void clientListMouseAction(MouseEvent mouseEvent) { + System.out.println(clientList.getSelectionModel().getSelectedItems()); + String receiver = clientList.getSelectionModel().getSelectedItem(); + textField.setText(String.format("/w %s",receiver)); + } + + private void createRegStage(){ + try { + FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("reg.fxml")); + Parent root = fxmlLoader.load(); + + regStage = new Stage(); + regStage.setTitle("Chatty registration"); + regStage.setScene(new Scene(root, 600, 500)); + + regController = fxmlLoader.getController(); + regController.setController(this); + + regStage.initStyle(StageStyle.UTILITY); + regStage.initModality(Modality.APPLICATION_MODAL); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void tryToReg(ActionEvent actionEvent) { + if (regStage == null){ + createRegStage(); + } + regStage.show(); + } + + public void registration(String login, String password, String nickname){ + String msg = String.format("/reg %s %s %s",login, password, nickname); + + if (socket == null|| socket.isClosed()){ + connect(); + } + + try { + out.writeUTF(msg); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/Client/src/main/java/History.java b/Client/src/main/java/History.java new file mode 100644 index 0000000..563460b --- /dev/null +++ b/Client/src/main/java/History.java @@ -0,0 +1,54 @@ +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +public class History { + + private static PrintWriter out; + + private static String getHistoryFilenameByLogin(String login){ + return "history/history_" + login +".txt"; + } + + public static void start(String login){ + try { + out = new PrintWriter(new FileOutputStream(getHistoryFilenameByLogin(login),true),true); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public static void stop(){ + if (out!=null){ + out.close(); + } + } + + public static void writeLine(String msg){ + out.println(msg); + } + + public static String getLastHundredLinesOfHistory(String login){ + if (!Files.exists(Paths.get(getHistoryFilenameByLogin(login)))){ + return ""; + } + StringBuilder stringBuilder = new StringBuilder(); + try { + + List historyLines = Files.readAllLines(Paths.get(getHistoryFilenameByLogin(login))); + int startpos = 0; + if (historyLines.size()>100){ + startpos = historyLines.size()-100; + } + for (int i = startpos;i + + + + + + + + + + + + + + + +