-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.java
More file actions
151 lines (130 loc) · 5.21 KB
/
Client.java
File metadata and controls
151 lines (130 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.Scanner;
import augie.edu.finalProgram.Belgovi.Encrpytion;
import augie.edu.finalProgram.Belgovi.FileStorage;
import augie.edu.finalProgram.Belgovi.MyArrayList;
public class Client {
private Socket socket;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private String clientName;
private FileStorage fileStorage;
private Encrpytion encrpytion;
// Constructor
public Client(Socket socket, String clientName) {
try {
this.socket = socket;
this.clientName = clientName;
this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.fileStorage = new FileStorage(clientName + ".csv");
this.encrpytion = new Encrpytion(1234);
// Check if history of previous conversation exists, and load history
Path path = Paths.get(clientName + ".csv");
if (Files.exists(path)) {
MyArrayList<String[]> history = fileStorage.readfromFile();
Iterator<String[]> iterator = history.iterator();
while (iterator.hasNext()) {
String[] singleLine = iterator.next();
System.out.println(singleLine[0] + ": " + encrpytion.decrpytText(singleLine[2]));
}
}
} catch (Exception e) {
// Disconnect everything
disconnect();
}
}
// Send a message to the server
public void sendMessage() {
try {
bufferedWriter.write(clientName);
bufferedWriter.newLine();
bufferedWriter.flush();
// TODO: Make sure that the client can send messages to the server using GUI
Scanner scanner = new Scanner(System.in);
while (socket.isConnected()) {
// Get datetime to store to file database
LocalDateTime now = LocalDateTime.now();
String message = scanner.nextLine();
bufferedWriter.write(message);
bufferedWriter.newLine();
bufferedWriter.flush();
// Store into file
// Encrpyt Message before file
message = encrpytion.encrpytText(message);
fileStorage.writeToFile(new String[] {clientName, now.toString(), message});
}
} catch (Exception e) {
// Disconnect everything
e.printStackTrace();
disconnect();
}
}
// Listen for messages from the server
// Using a thread to listen for messages
// This uses concurrency
public void listenForMessage() {
new Thread(new Runnable() {
@Override
public void run() {
try {
while (socket.isConnected()) {
// Get datetime to store to file database
LocalDateTime now = LocalDateTime.now();
String message = bufferedReader.readLine();
System.out.println(message);
// Decrpyt Message before storing
String[] messagePart = message.split(": ");
messagePart[1] = encrpytion.encrpytText(messagePart[1]);
// Store message from incoming chat
fileStorage.writeToFile(new String[] {messagePart[0], now.toString(), messagePart[1]});
}
} catch (Exception e) {
// Disconnect everything
disconnect();
}
}
}).start();
}
private void disconnect() {
try {
if (socket != null) {
socket.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Main method to run the client
public static void main(String[] args) throws UnknownHostException, IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String clientName = scanner.nextLine();
Socket socket = new Socket("localhost", 8080);
Client client = new Client(socket, clientName);
// These are blocking methods
// The client will not be able to send messages until it receives a message from
// the server
// Since they are sperate threads, the client can send messages and listen for
// messages at the same time
client.listenForMessage();
client.sendMessage();
}
}