Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added work/Clients/NetworkClients.jar
Binary file not shown.
19 changes: 19 additions & 0 deletions work/Clients/src/Clients/AphorismClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Clients;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class AphorismClient extends ClientModule {

public AphorismClient(TextArea ta_Resp, TextArea ta_Logs) {
super(ta_Resp, ta_Logs);
}
public AphorismClient(TextArea ta_Resp, TextArea ta_Logs, TextField tf_request) {
super(ta_Resp, ta_Logs, tf_request);
}

@Override
public void write() {
out.println("get");
}
}
78 changes: 78 additions & 0 deletions work/Clients/src/Clients/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package Clients;

import java.io.IOException;
import java.net.Socket;

public class Client extends Thread {

private Socket server;
private ClientModule module;
private boolean connected = false;

private Client(String host, int port, ClientModule module) {
setDaemon(true);
this.module = module;
try {
server = new Socket(host, port);
connected = true;
module.printLog("Connection established");
} catch (IOException ex) {
module.printLog("Connecting failed");
connected = false;
}
}

public static Client getInstance(String host, int port, ClientModule module) {
return new Client(host, port, module);
}
public String getDialog() {
return module.getDialog();
}
public void setSelected(boolean state) {
module.setSelected(state);
}
public boolean isConnected() {
return connected;
}

@Override
public String toString() {
return module.toString();
}

public void close() {
try {
if (server != null) {
server.close();
module.printLog("Client disconnected successfully");
connected = false;
}
} catch (IOException ex) {
module.printLog("Client terminating failed");
connected = false;
}
}

@Override
public void run() {
try{
module.workWith(server);
} catch (IOException ex) {
module.printLog("Input / Output closing failed");
} catch (NullPointerException ex) {
module.printLog("");
} finally {
close();
}
}

public void sendRequest() {
module.printLog("send request to server");
module.write();
}
public void disconnect() {
module.printLog("send disconnecting command");
module.write(module.getDisconnectCmd());
module.go = false;
}
}
101 changes: 101 additions & 0 deletions work/Clients/src/Clients/ClientModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package Clients;

import javafx.application.Platform;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public abstract class ClientModule {
//stores dialog with server
StringBuilder dialog = new StringBuilder();
//indicates if the client is selected in the list of clients
boolean selected = false;
//terminate tool
public volatile boolean go = true;
public TextArea taResp;
public TextArea taLogs;
public TextField tfRequest;
public BufferedReader in;
public PrintWriter out;
private String disconnectCmd = "exit";

public ClientModule(TextArea ta_Resp, TextArea ta_Logs) {
taResp = ta_Resp;
taLogs = ta_Logs;
}
public ClientModule(TextArea ta_Resp, TextArea ta_Logs, TextField tf_request) {
taResp = ta_Resp;
taLogs = ta_Logs;
tfRequest = tf_request;
}

public String getDialog() {
return dialog.toString();
}
public void setSelected(boolean state) {
selected = state;
}

public String getDisconnectCmd() {
return disconnectCmd;
}

public void printLog(String log) {
Platform.runLater(() -> taLogs.appendText(log + "\n"));

}
public void printResp(String response) {
Platform.runLater(() -> taResp.setText(response));
}
public void workWith(Socket server) throws IOException {
try {
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out = new PrintWriter(server.getOutputStream(), true);
printLog("in/out streams opened");
listen();
} catch (IOException ex) {
printLog("Input / Output opening failed");
} finally {
in.close();
out.close();
printLog("in/out streams closed");
}
}

public void write() {
out.println(tfRequest.getText());
tfRequest.clear();
}
public void write(String command) {
out.println(command);
}


public void listen() {
Thread listener = new Thread(() -> {
String input = "";
try {
while (!input.equalsIgnoreCase("exit")) {
printResp(input);
try {
input = in.readLine();
} catch (IOException ex) {
printLog("input reading failed");
break;
}
printLog("Server response : " + input);
}
} catch (NullPointerException ex) {
printLog("Server connection lost");
}
});
listener.setDaemon(true);
listener.start();
while (go && listener.isAlive()) Thread.onSpinWait();
}
}
22 changes: 22 additions & 0 deletions work/Clients/src/Clients/InfoClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Clients;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class InfoClient extends ClientModule {

public InfoClient(TextArea ta_Resp, TextArea ta_Logs) {
super(ta_Resp, ta_Logs);
}

public InfoClient(TextArea ta_Resp, TextArea ta_Logs, TextField tf_request) {
super(ta_Resp, ta_Logs, tf_request);
}

@Override
public void printResp(String response) {
taResp.clear();
for (String line : response.split("\\|"))
taResp.appendText(line + "\n");
}
}
33 changes: 33 additions & 0 deletions work/Clients/src/Clients/NoticeModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Clients;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class NoticeModule extends ClientModule {
private static int numOfClients = 0;
private int index;

public NoticeModule(TextArea ta_Resp, TextArea ta_Logs) {
super(ta_Resp, ta_Logs);
numOfClients++;
index = numOfClients;
}

public NoticeModule(TextArea ta_Resp, TextArea ta_Logs, TextField tf_request) {
super(ta_Resp, ta_Logs, tf_request);
numOfClients++;
index = numOfClients;
}

@Override
public void printResp(String response) {
dialog.append(response).append("\n");
if (selected)
taResp.appendText(response + "\n");
}

@Override
public String toString() {
return "Listener#" + index;
}
}
19 changes: 19 additions & 0 deletions work/Clients/src/Clients/TimeClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Clients;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class TimeClient extends ClientModule {

public TimeClient(TextArea ta_Resp, TextArea ta_Logs) {
super(ta_Resp, ta_Logs);
}
public TimeClient(TextArea ta_Resp, TextArea ta_Logs, TextField tf_request) {
super(ta_Resp, ta_Logs, tf_request);
}

@Override
public void write() {
out.println("get");
}
}
3 changes: 3 additions & 0 deletions work/Clients/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: sample.Main

Loading