-
Notifications
You must be signed in to change notification settings - Fork 0
/
EchoServer.java
163 lines (142 loc) · 4.57 KB
/
EchoServer.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
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
// 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
import java.io.*;
import ocsf.server.*;
import common.*;
/**
* @author Benedek Balazs (blasio99)
* @version Oct 2020
*/
public class EchoServer extends AbstractServer {
final public static int DEFAULT_PORT = 5555;
ChatIF serverUI;
public EchoServer(int port) {
super(port);
}
public EchoServer(int port, ChatIF serverUI) throws IOException {
super(port);
this.serverUI = serverUI;
}
public void handleMessageFromClient(Object message, ConnectionToClient client)
{
if (message.toString().startsWith("#login "))
{
if (client.getInfo("loginID") != null)
{
try
{
client.sendToClient("You are already logged in.");
}
catch (IOException e) {}
return;
}
client.setInfo("loginID", message.toString().substring(7));
}
else
{
if (client.getInfo("loginID") == null)
{
try
{
client.sendToClient("You need to login before you can chat.");
client.close();
}
catch (IOException e) {}
return;
}
System.out.println("Message received: " + message + " from \"" + client.getInfo("loginID") + "\" " + client);
this.sendToAllClients(client.getInfo("loginID") + "> " + message);
}
}
public void handleMessageFromServerUI(String message) {
if (message.charAt(0) == '#')
{
runCommand(message);
}
else
{
serverUI.display(message);
this.sendToAllClients("SERVER> " + message);
}
}
private void runCommand(String message) {
if (message.equalsIgnoreCase("#quit")) {quit();}
else if (message.equalsIgnoreCase("#stop")) {stopListening();}
else if (message.equalsIgnoreCase("#close")) {try {close();} catch (IOException e) {}}
else if (message.toLowerCase().startsWith("#setport")) {
if (getNumberOfClients() == 0 && !isListening()) {
int newPort = Integer.parseInt(message.substring(9));
setPort(newPort);
serverUI.display("Server port changed to " + getPort());
}
else
{
serverUI.display("The server is not closed. Port cannot be changed.");
}
}
else if (message.equalsIgnoreCase("#start")) {
if (!isListening())
{
try
{
listen();
}
catch (Exception ex) {
serverUI.display("Error - Could not listen for clients!");
}
}
else {
serverUI.display("The server is already listening for clients.");
}
}
else if (message.equalsIgnoreCase("#getport")) {
serverUI.display("Currently port: " + Integer.toString(getPort()));
}
}
protected void serverStarted() {
System.out.println("Server listening for connections on port " + getPort());
}
protected void serverStopped() {
System.out.println("Server has stopped listening for connections.");
}
protected void clientConnected(ConnectionToClient client) {
// display on server and clients that the client has connected.
String message = "A Client has connected";
System.out.println(message);
this.sendToAllClients(message);
}
synchronized protected void clientDisconnected(ConnectionToClient client) {
// display on server and clients when a user disconnects
String message = client.getInfo("loginID").toString() + " has disconnected";
System.out.println(message);
this.sendToAllClients(message);
}
synchronized protected void clientException(ConnectionToClient client, Throwable exception) {
String message = client.getInfo("loginID").toString() + " has disconnected";
System.out.println(message);
this.sendToAllClients(message);
}
public void quit() {
try {
close();
} catch (IOException e) {
}
System.exit(0);
}
public static void main(String[] args) {
int port = 0; // Port to listen on
try {
port = Integer.parseInt(args[0]); // Get port from command line
} catch (Throwable t) {
port = DEFAULT_PORT; // Set port to 5555
}
EchoServer sv = new EchoServer(port);
try {
sv.listen(); // Start listening for connections
} catch (Exception ex) {
System.out.println("ERROR - Could not listen for clients!");
}
}
}
// End of EchoServer class