- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.java
More file actions
Latest commit
executable file
·176 lines (153 loc) · 5 KB
/
Copy pathEchoServer.java
File metadata and controls
executable file
·176 lines (153 loc) · 5 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
// 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.*;
importjava.util.*;
importocsf.server.*;
/**
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @author François Bélanger
* @author Paul Holden
* @version July 2000
*/
publicclassEchoServerextendsAbstractServer
{
//Class variables *************************************************
/**
* The default port to listen on.
*/
finalpublicstaticintDEFAULT_PORT = 5555;
privateHashMap<String,ArrayList<String>> blockList = newHashMap<String,ArrayList<String>>();
privateArrayList<String> userList = newArrayList<String>();
//Constructors ****************************************************
/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
*/
publicEchoServer(intport)
{
super(port);
userList.add("server");
}
//Instance methods ************************************************
/**
* This method handles any messages received from the client.
*
* @param msg The message received from the client.
* @param client The connection from which the message originated.
*/
publicvoidhandleMessageFromClient (Objectmsg, ConnectionToClientclient)
{
System.out.println("Message received: " + msg + " from " + client);
if( ((String) msg).charAt(0) == '#' ){
Stringcmd = ((String) msg).substring(1);
Stringc[] = cmd.split(" ");
if( c[0].equals("login") ){
client.setInfo("id",c[1]);
client.setInfo("ip", client.getInetAddress().getHostAddress());
client.setInfo("hostname", client.getInetAddress().getHostName());
userList.add(c[1]);
sendToAllClients(client.getInfo("id") + " Connected");
}elseif(c[0].equals("block") ){
if(userList.contains(c[1])){
ArrayList<String> block = blockList.get(c[1]);
if(block != null){
block.add((String) client.getInfo("id"));
}else{
block = newArrayList<String>();
block.add((String) client.getInfo("id"));
blockList.put(c[1], block);
}
try {
client.sendToClient("Messages from " + c[1] + " will be blocked.");
} catch (IOExceptione) {}
}else{
try {
client.sendToClient("User " + c[1] + " does not exist.");
client.sendToClient(c[1]);
} catch (IOExceptione) {}
}
}elseif(c[0].equals("unblock") ){
ArrayList<String> block = blockList.get(c[1]);
block.remove(client.getInfo("id"));
}elseif(c[0].equals("whoblocksme") ){
ArrayList<String> block = blockList.get(client.getInfo("id"));
Stringblockme = "";
if(block != null){
for(Strings : block){
try {
client.sendToClient("Messages to " + s + " are being blocked.");
} catch (IOExceptione) {}
}
}
try {
client.sendToClient(blockme);
} catch (IOExceptione) {}
}
}else{
this.sendToAllClients( client.getInfo("id") + "> " + msg );
}
}
/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
*/
protectedvoidserverStarted()
{
System.out.println
("Server listening for connections on port " + getPort());
}
publicvoidclientException( ConnectionToClientclient, Throwableexception) {
System.out.println(client.getInfo( "ip" )+ " ("+ client.getInfo("id")+")"+ " Disconnected");
userList.remove(client.getInfo("id"));
blockList.remove( client.getInfo("id"));
sendToAllClients(client.getInfo("id" )+ " Disconnected");
}
/**
* This method overrides the one in the superclass. Called
* when the server stops listening for connections.
*/
protectedvoidserverStopped()
{
System.out.println("Server has stopped listening for connections.");
}
publicvoidclientConnected(ConnectionToClientclient){
System.out.println(client.toString()+" Connected.");
}
publicvoidclientDisconnected(ConnectionToClientclient){
System.out.println(client.getInfo("ip") + " ("+ client.getInfo("id")+")"+ " Disconnected.");
userList.remove(client.getInfo("id"));
blockList.remove( client.getInfo("id"));
sendToAllClients(client.getInfo("id") + " Disconnected.");
}
//Class methods ***************************************************
/**
* This method is responsible for the creation of
* the server instance (there is no UI in this phase).
*
* @param args[0] The port number to listen on. Defaults to 5555
* if no argument is entered.
*/
publicstaticvoidmain(String[] args)
{
intport = 0; //Port to listen on
try
{
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwablet)
{
port = DEFAULT_PORT; //Set port to 5555
}
ServerConsolesc = newServerConsole(port);
try {
sc.commandListen();
} catch (IOExceptione) {}
}
}
//End of EchoServer class