-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
421 lines (382 loc) · 12.7 KB
/
Client.java
File metadata and controls
421 lines (382 loc) · 12.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import java.io.*;
import java.net.Socket;
import java.sql.*;
import java.util.Date;
import java.util.Scanner;
public class Client {
private Socket socket;
private boolean online;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private String username;
private String password;
public void setUsername(String uname)
{
username=uname;
}
public void setPassword(String pass)
{
password=pass;
}
//Client class constructor
public Client(Socket socket)
{
try{
this.socket=socket;
this.bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e)
{
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
//function to listen for messages from other users
public void listenForMessage(){
new Thread(new Runnable() {
@Override
public void run() {
String msgFromGroupChat;
while (socket.isConnected()){
try{
msgFromGroupChat=bufferedReader.readLine();
if (msgFromGroupChat.length() > 0 && msgFromGroupChat.charAt(0)!='\\')
{
System.out.println(msgFromGroupChat);
}
}catch (IOException e)
{
closeEverything(socket,bufferedReader,bufferedWriter);
}
}
}
}).start();
}
//function to send messages to other users
public void sendMessage(String roomName){
try{
bufferedWriter.write(username);
bufferedWriter.newLine();
bufferedWriter.flush();
Scanner scanner=new Scanner(System.in);
while(socket.isConnected()){
String messageToSend =scanner.nextLine();
bufferedWriter.write(username+": "+messageToSend);
//code to input into file/room
FileWriter myWriter = new FileWriter(roomName+".txt",true);
myWriter.write("Line Added on: " + new Date()+"\n"+username+": "+messageToSend+"\n");
myWriter.close();
//code for commands
if(messageToSend.contains("\\help"))
{
System.out.print("Leave(\\leave), list(\\list)");
String instruction=scanner.nextLine();
if(instruction.equals("\\leave"))
{
bufferedWriter.write(username+" has left the chat");
System.out.println("you left the chat");
dashboard();
}
if (instruction.contains("\\list")) {
bufferedWriter.write("\\list");
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
bufferedWriter.newLine();
bufferedWriter.flush();
}
}catch (IOException e){
closeEverything(socket,bufferedReader,bufferedWriter);
}
}
public void setOnline(boolean ol)
{
online=ol;
}
//function checks if a room exists or not
public boolean roomExists(String roomName)
{
File f=new File(roomName+".txt");
return f.exists();
}
//this function creates a room(file) in which the chats are saved
public void createRoom(String roomName)
{
try {
File myObj = new File(roomName+".txt");
if (myObj.createNewFile()) {
System.out.println("Room created: " + roomName);
} else {
System.out.println("Welcome");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
//this function closes all reader and writers
public void closeEverything(Socket socket,BufferedReader bufferedReader, BufferedWriter bufferedWriter){
try{
if(bufferedReader!=null){
bufferedReader.close();
}
if(bufferedWriter!=null){
bufferedWriter.close();
}
if(socket!=null)
{
socket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
// this function loads the messages in that room
public void loadMessages(String roomName)
{
try (BufferedReader br = new BufferedReader(new FileReader(roomName+".txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//this method checks if a username or password exists in the database
public boolean LoginSuccess()
{
boolean a=false;
try {
Class.forName("org.postgresql.Driver");
Connection c= DriverManager.getConnection("jdbc:postgresql://localhost:5432/test","postgres","ajay2022");
if(c!=null)
{
PreparedStatement p=c.prepareStatement("SELECT * FROM db WHERE user_name=? AND password=?");
p.setString(1,username);
p.setString(2,password);
ResultSet r=p.executeQuery();
a=r.next();
if (!a) {
System.out.println("please try again");
}
return a;
}
else {
System.out.println("Connection failed");
}
}catch (ClassNotFoundException e)
{
System.out.println("Class not found");
}
catch (SQLException e)
{
System.out.println(e);
}
return a;
}
//this function registers an unregistered user
public void register()
{
System.out.println("please register below: ");
Scanner scanner=new Scanner(System.in);
System.out.print("Enter Username: ");
String username=scanner.nextLine();
System.out.print("Enter password: ");
String password=scanner.nextLine();
if(userExits(username))
{
System.out.println("User already Exists");
login();
}
else {
try {
Class.forName("org.postgresql.Driver");
Connection c= DriverManager.getConnection("jdbc:postgresql://localhost:5432/test","postgres","ajay2022");
if(c!=null)
{
PreparedStatement p=c.prepareStatement("INSERT INTO db(user_name,password) VALUES(?,?);");
p.setString(1,username);
p.setString(2,password);
int i=p.executeUpdate();
c.close();
if (i==1) {
System.out.println("Registered");
dashboard();
}
else {
System.out.println("already registered");
}
}
else {
System.out.println("Connection failed");
}
}catch (ClassNotFoundException e)
{
System.out.println("Class not found");
}
catch (SQLException e)
{
System.out.println(e);
}
}
}
public boolean getOnline()
{
return online;
}
//this method updates a user's username and password
public void update()
{
System.out.println("Update username and password");
Scanner scanner=new Scanner(System.in);
System.out.print("change username: ");
String uname=scanner.nextLine();
System.out.print("Change password: ");
String pass=scanner.nextLine();
try {
Class.forName("org.postgresql.Driver");
Connection c= DriverManager.getConnection("jdbc:postgresql://localhost:5432/test","postgres","ajay2022");
if(c!=null)
{
PreparedStatement p=c.prepareStatement("UPDATE db SET user_name=?, password=? WHERE user_name=?");
p.setString(1,uname);
p.setString(2,pass);
p.setString(3,username);
int i=p.executeUpdate();
c.close();
if(i==1)
{
System.out.println("Account Updates Successfully");
setUsername(uname);
setPassword(pass);
dashboard();
}
else{
System.out.println("error occurred");
}
}
else {
System.out.println("Connection failed");
}
}catch (ClassNotFoundException e)
{
System.out.println("Class not found");
}
catch (SQLException e)
{
System.out.println(e);
}
}
public void dashboard()
{
Scanner scanner=new Scanner(System.in);
String roomName="";
System.out.println("join(J) or create(C) or Update(U) or LogOut(L)");
String instruction=scanner.nextLine();
while(getOnline()) {
if (instruction.equals("J")) {
System.out.print("Enter the room name: ");
roomName = scanner.nextLine();
if (roomExists(roomName)) {
System.out.println("Welcome to room: " + roomName);
loadMessages(roomName);
listenForMessage();
sendMessage(roomName);
}
} else if (instruction.equals("C")) {
System.out.print("Enter room name: ");
roomName = scanner.nextLine();
if (roomName.equals("")) {
System.out.print("Enter a valid room name");
} else {
createRoom(roomName);
System.out.println("Welcome to room: " + roomName);
listenForMessage();
sendMessage(roomName);
}
} else if(instruction.equals("U"))
{
update();
}
else if(instruction.equals("L"))
{
System.out.println("You have successfully logged out");
first_page();
}
else {
System.out.println("Enter a valid instruction");
dashboard();
}
}
}
public boolean userExits(String uname)
{
boolean a=false;
try {
Class.forName("org.postgresql.Driver");
Connection c= DriverManager.getConnection("jdbc:postgresql://localhost:5432/test","postgres","ajay2022");
if(c!=null)
{
PreparedStatement p=c.prepareStatement("SELECT ? FORM db");
p.setString(1,uname);
ResultSet r=p.executeQuery();
c.close();
a=r.next();
}
else {
System.out.println("Connection failed");
}
}catch (ClassNotFoundException e)
{
System.out.println("Class not found");
}
catch (SQLException e)
{
System.out.println(e);
}
return a;
}
public boolean login()
{
Scanner scanner=new Scanner(System.in);
System.out.print("Enter Username: ");
String username=scanner.nextLine();
System.out.print("Enter password: ");
String password=scanner.nextLine();
setPassword(password);
setUsername(username);
if(LoginSuccess())
{
System.out.println("Login Successfully");
return true;
}
else {
System.out.println("please try again, incorrect username or password");
return login();
}
}
public void first_page()
{
Scanner scanner=new Scanner(System.in);
System.out.println("Login(L) or Register(R)");
String s=scanner.nextLine();
if(s.equals("L"))
{
if(login()){
setOnline(true);
dashboard();
}
}
else if(s.equals("R"))
{
register();
}
}
public static void main(String[] args) throws IOException {
Scanner scanner=new Scanner(System.in);
Socket socket=new Socket("localhost",1234);
Client client=new Client(socket);
client.first_page();
}
}