01
// EchoCommandHandler.java
02
package
echoserver;
03
04
import
java.net.*;
05
import
java.io.*;
06
import
org.quickserver.net.server.ClientCommandHandler;
07
import
org.quickserver.net.server.ClientHandler;
08
09
public class
EchoCommandHandler
implements
ClientCommandHandler {
10
11
public void
gotConnected(ClientHandler handler)
12
throws
SocketTimeoutException, IOException {
13
handler.sendClientMsg(
"+++++++++++++++++++++++++++++++"
);
14
handler.sendClientMsg(
"| Welcome to EchoServer v 1.0 |"
);
15
handler.sendClientMsg(
"| Note: Password = Username |"
);
16
handler.sendClientMsg(
"| Send 'Quit' to exit |"
);
17
handler.sendClientMsg(
"+++++++++++++++++++++++++++++++"
);
18
}
19
public void
lostConnection(ClientHandler handler)
20
throws
IOException {
21
handler.sendSystemMsg(
"Connection lost : "
+
22
handler.getSocket().getInetAddress());
23
}
24
public void
closingConnection(ClientHandler handler)
25
throws
IOException {
26
handler.sendSystemMsg(
"Closing connection : "
+
27
handler.getSocket().getInetAddress());
28
}
29
30
public void
handleCommand(ClientHandler handler, String command)
31
throws
SocketTimeoutException, IOException {
32
if
(command.equals(
"Quit"
)) {
33
handler.sendClientMsg(
"Bye ;-)"
);
34
handler.closeConnection();
35
return
;
36
}
37
if
(command.equals(
"What's interest?"
)) {
38
handler.sendClientMsg(
"Interest is : "
+
39
(String)handler.getServer().getStoreObjects()[
0
]+
40
"%"
);
41 }
else if(command.equalsIgnoreCase(
"hello"
)) {
42
EchoServerData data = (EchoServerData) handler.getClientData();
43
data.setHelloCount(data.getHelloCount()+
1
);
44
if(data.getHelloCount()==
1
) {
45
handler.sendClientMsg(
"Hello "
+data.getUsername());
46 }
else
{
47
handler.sendClientMsg(
"You told Hello "
+data.getHelloCount()+
48
" times. "
);
49
}
50 }
else
{
51
handler.sendClientMsg(
"Echo : "
+command);
52
}
53
}
54
}
|