-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientDriver.java
More file actions
636 lines (559 loc) · 18.1 KB
/
Copy pathClientDriver.java
File metadata and controls
636 lines (559 loc) · 18.1 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
* Networked Chat with RSA Encryption/Decryption
* Project 5
* CS 342 - Fall 2017
*
* - Margi Katwala
* - Bushra Baqui
* - Aditya Sinha
*
* **ClientDriver.java**
*/
import java.awt.BorderLayout;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.omg.CORBA.portable.InputStream;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.PublicKey;
@SuppressWarnings("serial")
public class ClientDriver extends JFrame
{
private static final Font TEXT_FONT = new Font("Times new Roman", Font.PLAIN, 15);
private static final Font BUTTON_FONT = new Font(Font.SERIF, Font.BOLD, 15);
private static final Border OUTLINE_BORDER = BorderFactory.createLineBorder(new Color(139,0,139), 3, true);
JPanel panelConnect, chatPanel, chooseUserPanel;
JTextField IPText, portText, userText, mainMessage , firstPrime, secondPrime;
JTextArea chatTextArea;
JButton connectButton, selectClientButton, allClientButton, exitButton, userChooseButton;
JList<String> listConnected;
DefaultListModel<String> listAllConnected;
JMenu file = new JMenu("File");
private MessageListener messageListener;
private String userName;
public ClientDriver()
{
super("Client");
// creates the menu panel
JPanel topPanel = new JPanel(new GridLayout(2, 0, 20, 20));
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("Menu");
menuBar.setForeground(Color.BLUE);
panelConnect = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 20));
panelConnect.add(menuBar);
// creating the menu items
JMenuItem open = new JMenuItem("Load File");
JMenuItem help = new JMenuItem("Help ");
JMenuItem about = new JMenuItem("About");
file.add(help);
file.add(about);
menuBar.add(file);
//create text box for IP address and Port number
JLabel IPLabel = new JLabel("IP Address");
IPLabel.setFont(TEXT_FONT);
IPLabel.setForeground(Color.BLACK);
panelConnect.add(IPLabel);
IPText = new JTextField(20);
IPText.setFont(TEXT_FONT);
IPText.setForeground(Color.BLACK);
panelConnect.add(IPText);
JLabel portLabel = new JLabel("Port");
portLabel.setFont(TEXT_FONT);
portLabel.setForeground(Color.BLACK);
panelConnect.add(portLabel);
portText = new JTextField(20);
portText.setFont(TEXT_FONT);
portText.setForeground(Color.BLACK);
panelConnect.add(portText);
connectButton = new JButton("Connect");
connectButton.setFont(BUTTON_FONT);
// action listener for the connect button
connectButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
new Thread(new Runnable()
{
public void run()
{
try
{
connectToserver(IPText.getText(), Integer.parseInt(portText.getText()));
connectPanelSwitch(false);
chooseUsernamePanelSwitch(true);
} catch (NumberFormatException e1)
{
showMessageDialog("Port number should be an integer.");
}
catch (IOException e2)
{
showMessageDialog("Error: Cannot Connect to server.");
}
}
}).start();
}
});
panelConnect.add(connectButton);
panelConnect.setBorder(OUTLINE_BORDER);
topPanel.add(panelConnect);
chooseUserPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,40,40));
// Panel for inputing username and two prime keys
JLabel userLabel = new JLabel("Choose Username");
JLabel PrimeNum1 = new JLabel("Prime Num 1");
JLabel PrimeNum2 = new JLabel("Prime Num 2");
userLabel.setFont(TEXT_FONT);
userLabel.setForeground(Color.BLACK);
chooseUserPanel.add(userLabel);
firstPrime= new JTextField(5);
secondPrime= new JTextField(5);
userText = new JTextField(20);
userText.setFont(TEXT_FONT);
userText.setForeground(Color.BLACK);
chooseUserPanel.add(userText);
chooseUserPanel.add(PrimeNum1);
chooseUserPanel.add(firstPrime);
chooseUserPanel.add(PrimeNum2);
chooseUserPanel.add(secondPrime);
userChooseButton = new JButton("Enter");
userChooseButton.setFont(BUTTON_FONT);
// action listener for help menu item
help.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
JOptionPane.showMessageDialog(help,
"How to Use the Network Chat: "
+ "\n 1. Connect to the server using the IP address and Port number "
+ "\n 2. Choose username and two prime numbers to secure your message between clients ."
+ "\n If you enter -1 for both prime numbers you will allow to choose file to create the random numbers "
+ "\n 3. If you want to send message to specific select Send to Select else Send to All "
+ "\n 4.How to Use Menu :"
+ "\n Load File: Lets you load the file of prime numbers which will generate the prime key randomly. "
+ "\n Help: It will help you to familarzie with the game "
+ "\n About: Project Managment Team "
+ "\n 5. To exit the chat press Exit in the bottom corner ");
}
});
about.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
JOptionPane.showMessageDialog(help,
" Class CS - 342 Project 5"
+ "\n Project Manged by "
+ "\n 1. Margi Katwala "
+ "\n 2. Aditya Sinha "
+ "\n 3.Bushra Baqui "
+ "\n ");
}
});
// After the connection is done
userChooseButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String username = userText.getText();
LargeInteger publicKey;
publicKey= null;
boolean errorFlag = false;
List<Long> numbers = new ArrayList<Long>();
LargeInteger primeNum = new LargeInteger(firstPrime.getText());
if (LargeInteger.compare(primeNum, new LargeInteger(-1)) == 0)
{
try
{
BufferedReader in;
File selectedFile;
JFileChooser openFile = new JFileChooser();
FileReader reader = null;
int returnValue = openFile.showOpenDialog(null);
if (returnValue == openFile.APPROVE_OPTION)
{
returnValue= 1;
}
else
returnValue =0;
selectedFile = openFile.getSelectedFile();
if(selectedFile.canRead() && selectedFile.exists())
{
reader = new FileReader(selectedFile);
}
// reads the buffer
in = new BufferedReader(reader);
String inputLine =in.readLine();
int lineNum = 0;
while(inputLine!=null)
{
lineNum++;
StringTokenizer tokenNum = new StringTokenizer(inputLine);
inputLine = in.readLine();
}
in.close();
}
catch(Exception ex)
{
}
primeNum = RSA.random_prime(50);
}
if (!new BigInteger(primeNum.toString()).isProbablePrime(10))
{
JOptionPane.showMessageDialog(null, primeNum + " is not prime. Please try again");
errorFlag = true;
}
LargeInteger q1 = new LargeInteger(secondPrime.getText());
if (LargeInteger.compare(q1, new LargeInteger(-1)) == 0)
{
try
{
BufferedReader in;
// lets the user choose the file for the random primes
File selectedFile;
JFileChooser openFile = new JFileChooser();
FileReader reader = null;
int returnVal = openFile.showOpenDialog(null);
if (returnVal == openFile.APPROVE_OPTION)
{
returnVal= 1;
} else
returnVal =0;
selectedFile = openFile.getSelectedFile();
if(selectedFile.canRead() && selectedFile.exists())
{
reader = new FileReader(selectedFile);
}
in = new BufferedReader(reader);
String inputLine =in.readLine();
int LineNumber = 0;
while(inputLine!=null)
{
LineNumber++;
StringTokenizer newToken = new StringTokenizer(inputLine);
inputLine = in.readLine();
}
in.close();
}
catch(Exception ex)
{
}
q1 = RSA.random_prime(50);
}
if (!new BigInteger(q1.toString()).isProbablePrime(10))
{
JOptionPane.showMessageDialog(null, q1 + " is not prime. Please try again");
errorFlag = true;
}
if (!errorFlag)
{
@SuppressWarnings("unused")
RSA.Key key = RSA.generateKeys(primeNum, q1);
JOptionPane.showMessageDialog(null, "Successfully created keys!");
messageListener.sendMessage(new Message(MessageType.STORE_USER, username, "SERVER", username,null));
userChooseButton.setEnabled(false);
}
else
{
JOptionPane.showMessageDialog(null,"Please try again");
}
}
});
// Adding items to the chat box
chooseUserPanel.add(userChooseButton);
chooseUserPanel.setBorder(OUTLINE_BORDER);
topPanel.add(chooseUserPanel);
getContentPane().add(topPanel, BorderLayout.NORTH);
chatPanel = new JPanel(new BorderLayout(5, 5));
chatTextArea = new JTextArea(20, 40);
chatTextArea.setEditable(false);
chatTextArea.setBorder(OUTLINE_BORDER);
chatTextArea.setFont(new Font(Font.MONOSPACED, Font.ITALIC, 15));
chatTextArea.setForeground(Color.BLACK);
JScrollPane spChat = new JScrollPane(chatTextArea);
spChat.setBorder(new EmptyBorder(20, 20, 0, 20));
chatPanel.add(spChat, BorderLayout.CENTER);
listAllConnected = new DefaultListModel<>();
listConnected = new JList<>(listAllConnected);
listConnected.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
JScrollPane spConnected = new JScrollPane(listConnected);
spConnected.setFont(TEXT_FONT);
spConnected.setBorder(BorderFactory.createTitledBorder(OUTLINE_BORDER,
"Connected Clients",TitledBorder.CENTER, TitledBorder.DEFAULT_JUSTIFICATION, TEXT_FONT, Color.BLACK));
chatPanel.add(spConnected, BorderLayout.WEST);
JPanel sendMessagePane = new JPanel(new FlowLayout(FlowLayout.LEFT, 20 ,10));
mainMessage = new JTextField(40);
mainMessage.setFont(TEXT_FONT);
mainMessage.setForeground(Color.BLACK);
mainMessage.setBorder(OUTLINE_BORDER);
sendMessagePane.add(mainMessage);
selectClientButton = new JButton("Send to Selected");
selectClientButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String messageContent = mainMessage.getText();
if(messageContent.length() == 0)
{
showMessageDialog("Please enter some text");
return;
}
List<String> recipients = listConnected.getSelectedValuesList();
if(recipients.size() == 0)
{
showMessageDialog("Please choose atleast one recipient");
return;
}
else if(recipients.size() == 1)
{
Message message = new Message(MessageType.DISPLAY_SINGLETEXT, userName, recipients.get(0), messageContent,null);
System.out.println("unicast message sent to "+recipients.get(0));
displayMessage(message.toString());
messageListener.sendMessage(message);
mainMessage.setText("");
listConnected.clearSelection();
}
else if(recipients.size() > 1)
{
String recipientsNames = "";
for (String recipient : recipients)
{
recipientsNames+=recipient+"||";
}
recipientsNames = recipientsNames.substring(0,recipientsNames.length()-2);
Message message = new Message(MessageType.DISPLAY_MULTITEXT, userName, recipientsNames, messageContent,null);
messageListener.sendMessage(message);
displayMessage(message.toString());
mainMessage.setText("");
listConnected.clearSelection();
}
}
});
sendMessagePane.add(selectClientButton);
allClientButton = new JButton("Send to All");
allClientButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String content = mainMessage.getText();
RSA.Key key = RSA.getlocalKey();
ArrayList<LargeInteger> econtent = RSA.encrypt(content, key.n, key.e, 4);
if(content.length() == 0)
{
showMessageDialog("Please enter some text");
return;
}
@SuppressWarnings("unused")
ArrayList<LargeInteger> sum = econtent;
if(listAllConnected.size() == 0)
{
showMessageDialog("Cannot send the message. No users connected");
return;
}
Message message = new Message(MessageType.DISPLAY_TEXT, userName, "ALL", content,null);
messageListener.sendMessage(message);
displayMessage(message.toString());
mainMessage.setText("");
}
});
sendMessagePane.add(allClientButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
messageListener.sendMessage(new Message(MessageType.DISCONNECT_CLIENT, userName, "SERVER", userName, null));
System.exit(0);
}
});
sendMessagePane.add(exitButton);
chatPanel.add(sendMessagePane, BorderLayout.SOUTH);
getContentPane().add(chatPanel, BorderLayout.CENTER);
connectPanelSwitch(true);
chooseUsernamePanelSwitch(false);
chatPanelSwitch(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
IPText.setText("127.0.0.1");
portText.setText("8585");
}
private void connectPanelSwitch(boolean isEnabled)
{
IPText.setEnabled(isEnabled);
portText.setEnabled(isEnabled);
connectButton.setEnabled(isEnabled);
}
private void chooseUsernamePanelSwitch(boolean isEnabled)
{
userText.setEnabled(isEnabled);
userChooseButton.setEnabled(isEnabled);
}
private void chatPanelSwitch(boolean isEnabled)
{
chatTextArea.setEnabled(isEnabled);
mainMessage.setEnabled(isEnabled);
selectClientButton.setEnabled(isEnabled);
allClientButton.setEnabled(isEnabled);
exitButton.setEnabled(isEnabled);
listConnected.setEnabled(isEnabled);
if(isEnabled == false)
listAllConnected.clear();
}
private void connectToserver(String ip, int port) throws IOException
{
Socket socket = new Socket(ip, port);
messageListener = new MessageListener(socket);
messageListener.start();
}
public static void main(String[] args)
{
new ClientDriver();
}
public void displayMessage(String message)
{
chatTextArea.append(message+"\n");
}
public void showMessageDialog(String message)
{
JOptionPane.showMessageDialog(this, message);
}
public class MessageListener extends Thread
{
private ObjectInputStream inputStream;
private ObjectOutputStream outputStream;
private boolean running;
public MessageListener(Socket socket) throws IOException
{
inputStream = new ObjectInputStream(socket.getInputStream());
outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.flush();
running = true;
}
public void run()
{
try
{
while (running)
{
Message message = (Message) inputStream.readObject();
if(message.getMessageType() == MessageType.DISPLAY_SINGLETEXT
|| message.getMessageType() == MessageType.DISPLAY_MULTITEXT
||message.getMessageType() == MessageType.DISPLAY_TEXT)
{
displayMessage(message.toString());
}
else if(message.getMessageType() == MessageType.STORE_USER)
{
if(message.getMainMessage().equals("SUCCESS")){
displayMessage("Username choosen successfully");
userName = message.getMessageReceiver();
chooseUsernamePanelSwitch(false);
chatPanelSwitch(true);
}
else
{
showMessageDialog("Error: Please choose a different username.");
userChooseButton.setEnabled(true);
}
}
else if(message.getMessageType() == MessageType.CONNECTED_CLIENTS)
{
String[] connected = message.getMainMessage().split(Pattern.quote("||"));
for (String client : connected)
{
if(!client.equals(userName))
{
listAllConnected.addElement(client);
displayMessage(client+" connected.");
}
}
}
else if(message.getMessageType() == MessageType.CONNECT_CLIENT)
{
listAllConnected.addElement(message.getMainMessage());
displayMessage(message.getMainMessage()+" connected.");
}
else if(message.getMessageType() == MessageType.DISCONNECT_CLIENT)
{
listAllConnected.removeElement(message.getMainMessage());
displayMessage(message.getMainMessage()+" disconnected.");
}
}
}
catch (Exception e)
{
showMessageDialog("Error: Connection to server lost. Please try reconnecting.");
}
chatPanelSwitch(false);
chooseUsernamePanelSwitch(false);
connectPanelSwitch(true);
}
public void sendMessage(Message message)
{
try
{
outputStream.writeObject(message);
outputStream.flush();
}
catch (IOException e)
{
showMessageDialog("Error: Cannot send message to server. Try Reconnecting..");
chatPanelSwitch(false);
chooseUsernamePanelSwitch(false);
connectPanelSwitch(true);
}
}
}
}