-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHandler.java
More file actions
95 lines (89 loc) · 3.46 KB
/
Copy pathClientHandler.java
File metadata and controls
95 lines (89 loc) · 3.46 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
/**
* ClientHandler.java
*
* This class handles communication between the client
* and the server. It runs in a separate thread but has a
* link to a common list of sockets to handle broadcast.
*
*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ClientHandler implements Runnable {
private Socket connectionSock = null;
private ArrayList<Socket> socketList;
private ChattyPatty patty;
ClientHandler(Socket sock, ArrayList<Socket> socketList) {
this.connectionSock = sock;
this.socketList = socketList; // Keep reference to master list
patty = new ChattyPatty();
}
/**
* received input from a client.
* sends it to other clients.
*/
public void run() {
try {
System.out.println("Connection made with socket " + connectionSock);
BufferedReader clientInput = new BufferedReader(
new InputStreamReader(connectionSock.getInputStream()));
String userName = clientInput.readLine();
for (Socket s : socketList) {
DataOutputStream clientOutput;
clientOutput = new DataOutputStream(s.getOutputStream());
clientOutput.writeBytes("Patty: " + userName
+ " has joined the chatroom. Everyone say hello to them!\n");
}
while (true) {
// Get data sent from a client
String clientText = clientInput.readLine();
if (clientText != null) {
System.out.println("Received: " + clientText);
// Turn around and output this data
// to all other clients except the one
// that sent us this information
// Generate a random number between 0 and 1
Random generator = new Random();
double number = generator.nextDouble();
// If random number is gretaer than 0.5 execute the Bot code
for (Socket s : socketList) {
DataOutputStream clientOutput;
if (s != connectionSock) {
clientOutput = new DataOutputStream(s.getOutputStream());
clientOutput.writeBytes(clientText + "\n");
}
if (number >= 0.5) {
if (clientText.substring(clientText.length() - 1).equals("!")
|| clientText.substring(clientText.length() - 1).equals(".")
|| clientText.substring(clientText.length() - 1).equals("?")) {
clientText = clientText.substring(0, clientText.length() - 1);
}
String[] messageToBot = clientText.split(":", 2);
messageToBot[1] = messageToBot[1].substring(1, messageToBot[1].length());
patty.setUserInput(messageToBot[1]);
clientOutput = new DataOutputStream(s.getOutputStream());
clientOutput.writeBytes("Chatty Patty: " + patty.getMessage() + "\n");
}
//write chattypatty's message here in this for loop
}
} else {
// Connection was lost
System.out.println("Closing connection for socket " + connectionSock);
// Remove from arraylist
socketList.remove(connectionSock);
connectionSock.close();
break;
}
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
// Remove from arraylist
socketList.remove(connectionSock);
}
}
} // ClientHandler for MtServer.java