-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerConsole.java
94 lines (84 loc) · 2.37 KB
/
ServerConsole.java
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
import common.ChatIF;
import java.util.Scanner;
public class ServerConsole implements ChatIF {
/* class variables */
final public static int DEFAULT_PORT = 5555;
/* instances variables */
EchoServer server;
Scanner fromConsole;
/* constructors */
public ServerConsole(int port){
server = new EchoServer(port);
fromConsole = new Scanner(System.in);
}
/* instances methods */
@Override
public void display(String message) {
System.out.println(message);
}
// quand il y a un message, on le traite
public void accept()
{
try
{
server.listen();
String message;
while (true){
message = fromConsole.nextLine();
if(message.charAt(0) == '#' && message.length() != 1){
String[] comArg = message.substring(1)
.toLowerCase().split(" ");
switch(comArg[0]){
case "quit":
System.out.println("Shutting down server.");
server.quit();
break;
case "close":
System.out.println("Server has stopped listening for new"
+ " clients, and has disconnected all clients.");
server.close();
break;
case "start":
if(!server.isListening()){
server.listen();
}
else {
System.out.println("Cannot perform this action while listening.");
}
break;
case "stop":
if(server.isListening()){
server.stopListening();
}
else {
System.out.println("The server is currently not listening.");
}
break;
case "getport":
System.out.println("Current server port is " + server.getPort());
break;
case "setport":
if(!server.isListening()) {
System.out.println("Server port set to " + comArg[1]);
server.setPort(Integer.parseInt(comArg[1]));
}
else {
System.out.println("The server has to be offline to perform this action.");
}
break;
default:
System.out.println("Unknown : this server command does not exist.");
break;
}
}
else {
server.handleMessageFromServerUI("SERVERMSG>" + message, this);
}
}
}
catch (Exception ex)
{
System.out.println("Unexpected error while reading from server console!");
}
}
}