forked from husseinalosman/simpleChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConsole.java
More file actions
198 lines (153 loc) · 5.29 KB
/
ServerConsole.java
File metadata and controls
198 lines (153 loc) · 5.29 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
import java.io.*;
import java.util.Scanner;
import client.*;
import common.*;
import ocsf.server.ConnectionToClient;
public class ServerConsole implements ChatIF {
//Class variables *************************************************
/**
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;
//Instance variables **********************************************
/**
* The instance of the server that created this ServerConsole.
*/
EchoServer server;
/**
* Scanner to read from the console
*/
Scanner fromConsole;
//Constructors ****************************************************
/**
* Constructs an instance of the ClientConsole UI.
*
* @param port The port to connect on.
*/
public ServerConsole(int port) {
try
{
server = new EchoServer(port, this);
// Create scanner object to read from console
fromConsole = new Scanner(System.in);
}
catch(IOException exception)
{
System.out.println("ERROR - Could not listen for clients!");
System.exit(1);
}
}
//Instance methods ************************************************
/**
* This method waits for input from the console. Once it is
* received, it sends it to the client's message handler.
*/
public void accept()
{
try
{
String message;
while (true)
{
message = fromConsole.nextLine();
if (message.toString().charAt(0) == '#') { // if the first character of the input is a #
if (!message.contains(" ")) { // only one word input
String command = message.substring(1); // command is the second part of the input (after #)
switch(command) {
case "quit":
System.out.println("Closing program.");
server.close(); // kills server
break;
case "stop":
server.stopListening(); // stop listening for new clients
server.sendToAllClients("WARNING - The server has stopped listening for connections\r\n" +
"SERVER SHUTTING DOWN! DISCONNECTING!\r\n");
break;
case "close":
server.stopListening();
Thread[] clientThreadList = server.getClientConnections(); // array of all clients
for (int i = 0; i < clientThreadList.length; i++) {
try {
server.sendToAllClients("Abnormal termination of connection.");
((ConnectionToClient)clientThreadList[i]).close(); // Close the client sockets of the already connected clients
}
catch(Exception e) {}
}
break;
case "getport":
System.out.println(Integer.toString(server.getPort()));
break;
case "start":
if (!server.isListening()) {
server.listen();
} else if (server.isListening()) {
System.out.println("Error: server is already listening for connections.");
}
break;
default:
System.out.println("Not a command."); // if used # but not a valid command
break;
}
}
else if (message.contains(" ")) { // two word input
String command = message.split(" ")[0];
String para = message.split(" ")[1];
switch (command) {
case "#setport":
if (!server.isListening()) {
server.setPort(Integer.parseInt(para));
System.out.println("port: set to: " + server.getPort());
} else if (server.isListening()) {
System.out.println("Error: server is already running.");
}
break;
default:
System.out.println("Not a valid command.");
break;
}
}
}
else { // if the input isn't a command
display("SERVER MSG> " + message);
server.sendToAllClients("SERVER MSG> " + message);
}
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}
/**
* This method overrides the method in the ChatIF interface. It
* displays a message onto the screen.
*
* @param message The string to be displayed.
*/
public void display(String message)
{
System.out.println("> " + message);
}
//Class methods ***************************************************
/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
*/
public static void main(String[] args)
{
int port = DEFAULT_PORT; // initialize port number to default
try
{
port = Integer.parseInt(args[0]); //get port from command line
}
catch(Throwable t)
{
System.out.println("Didn't specify port or wrong input. Using default port.");
}
ServerConsole serverChat = new ServerConsole(port);
serverChat.accept(); //Wait for console data
}
}
//End of ServerConsole class