- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConsole.java
More file actions
Latest commit
executable file
·94 lines (80 loc) · 2.21 KB
/
Copy pathServerConsole.java
File metadata and controls
executable file
·94 lines (80 loc) · 2.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
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com
importjava.io.*;
importcommon.*;
/**
*
*
*
*
*
*/
publicclassServerConsoleimplementsChatIF
{
privatebooleanclosed = false;
privateEchoServeres;
publicServerConsole(intport) {
es = newEchoServer(port);
try
{
es.listen(); //Start listening for connections
}
catch (Exceptionex)
{
System.out.println("ERROR - Could not listen for clients!");
}
}
publicvoidcommandListen() throwsIOException{
BufferedReaderfromConsole = newBufferedReader(newInputStreamReader(System.in));
Stringmessage = fromConsole.readLine();
while(message != null){
if(message.charAt(0) == '#')
processCommand(message.substring(1));
else{
es.sendToAllClients( "SERVER MSG> " + message );
display("SERVER MSG> " + message);
}
message = fromConsole.readLine();
}
}
privatevoidprocessCommand(Stringcmd) throwsIOException{
Stringc[] = cmd.split(" ");
if( c[0].equalsIgnoreCase("quit")){
display("Server Terminating!");
es.sendToAllClients("Server Terminating!");
System.exit(0);
}elseif ( c[0].equalsIgnoreCase("stop") ){
es.stopListening();
}elseif ( c[0].equalsIgnoreCase("close") ){
es.close();
closed = true;
}elseif ( c[0].equalsIgnoreCase("setport") ){
if(closed)
es.setPort(Integer.parseInt(c[1]));
else
System.out.print("Must close server before setting port!");
}elseif ( c[0].equalsIgnoreCase("start") ){
closed = false;
if(!es.isListening()){
try {
es.listen();
} catch (IOExceptione) {}
}else
display("The server is already started.");
}elseif ( c[0].equalsIgnoreCase("getport") )
display(Integer.toString(es.getPort()));
else
display("Invalid server command!");
}
/**
* This method overrides the method in the ChatIF interface. It
* displays a message onto the screen.
*
* @param message The string to be displayed.
*/
publicvoiddisplay(Stringmessage)
{
System.out.println(message);
}
}