01
package
echoserver;
02
03
import
org.quickserver.net.*;
04
import
org.quickserver.net.server.*;
05
06
import
java.io.*;
07
08
public class
EchoServer {
09
public static void
main(String s[]) {
10
11
String cmd =
"echoserver.EchoCommandHandler"
;
12
String auth =
"echoserver.EchoServerQuickAuthenticator"
;
13
String data =
"echoserver.EchoServerPoolableData"
;
//Poolable
14
15
QuickServer myServer =
new
QuickServer(cmd);
16
myServer.setAuthenticator(auth);
17
myServer.setClientData(data);
18
19
myServer setPort(
4123
);
20
myServer.setName(
"Echo Server v 1.0"
);
21
22
//config QSAdminServer
23
myServer.setQSAdminServerPort(
4124
);
24
myServer.getQSAdminServer().getServer().setName(
"EchoAdmin v 1.0"
);
25
try
{
26
myServer.startQSAdminServer();
27
myServer.startServer();
28
}
catch
(AppException e){
29
System.out.println(
"Error in server : "
+e);
30
}
31
}
32
}
33
|
01
package
echoserver;
02
03
import
org.quickserver.net.*;
04
import
org.quickserver.net.server.*;
05
06
import
java.io.*;
07
08
public class
EchoServer {
09
public static void
main(String s[]) {
10
11
String cmd =
"echoserver.EchoCommandHandler"
;
12
String auth =
"echoserver.EchoServerQuickAuthenticator"
;
13
String data =
"echoserver.EchoServerPoolableData"
;
//Poolable
14
15
QuickServer myServer =
new
QuickServer(cmd);
16
myServer.setAuthenticator(auth);
17
myServer.setClientData(data);
18
19
myServer.setPort(
4123
);
20
myServer.setName(
"Echo Server v 1.0"
);
21
22
//store data needed to be changed by QSAdminServer
23
Object[] store =
new
Object[]{
"12.00"
};
24
myServer setStoreObjects(store);
25
26
//config QSAdminServer
27
myServer.setQSAdminServerPort(
4124
);
28
myServer.getQSAdminServer().getServer().setName(
"EchoAdmin v 1.0"
);
29
try
{
30
myServer.startQSAdminServer();
31
myServer.startServer();
32
}
catch
(AppException e){
33
System.out.println(
"Error in server : "
+e);
34
}
35
}
36
}
37
|
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
}
|
01
package
echoserver;
02
03
04
import
java.io.*;
05
import
java.net.SocketTimeoutException;
06
import
org.quickserver.net.server.*;
07
import
org.quickserver.net.qsadmin.*;
08
09
public class
QSAdminCommandPlugin
implements
CommandPlugin {
10
/**
11
* Echoserver commands
12
* ----------------------------------
13
* set interest value
14
* get interest
15
*/
16
public boolean
handleCommand(ClientHandler handler, String command)
17
throws
SocketTimeoutException, IOException {
18
19
QuickServer echoserver = (QuickServer)
20
handler.getServer().getStoreObjects()[
0
];
21
Object obj[] = echoserver.getStoreObjects();
22
23
if
(command.toLowerCase().startsWith(
"set interest "
)) {
24
String temp =
""
;
25
temp = command.substring(
"set interest "
.length());
26
obj[
0
] = temp;
27
echoserver.setStoreObjects(obj);
28
handler.sendClientMsg(
"+OK interest changed"
);
29
return true
;
30
}
else if
(command.toLowerCase().equals(
"get interest"
)) {
31
handler.sendClientMsg(
"+OK "
+ (String)obj[
0
]);
32
return true
;
33
}
34
//ask QSAdminServer to process the command
35
return false
;
36
}
37
}
|
01
package
echoserver;
02
03
import
org.quickserver.net.*;
04
import
org.quickserver.net.server.*;
05
06
import
java.io.*;
07
08
public class
EchoServer {
09
public static void
main(String s[]) {
10
11
String cmd =
"echoserver.EchoCommandHandler"
;
12
String auth =
"echoserver.EchoServerQuickAuthenticator"
;
13
String data =
"echoserver.EchoServerPoolableData"
;
//Poolable
14
15
QuickServer myServer =
new
QuickServer(cmd);
16
myServer.setAuthenticator(auth);
17
myServer.setClientData(data);
18
19
myServer.setPort(
4123
);
20
myServer.setName(
"Echo Server v 1.0"
);
21
22
//store data needed to be changed by QSAdminServer
23
Object[] store =
new
Object[]{
"12.00"
};
24
myServer.setStoreObjects(store);
25
26
//config QSAdminServer
27
myServer.setQSAdminServerPort(
4124
);
28 myServer.getQSAdminServer().getServer().setName("EchoAdmin v 1.0");
29
try
{
30
//add command plugin
31
myServer.getQSAdminServer().setCommandPlugin(
32
"echoserver.QSAdminCommandPlugin"
);
33
myServer startQSAdminServer();
34
myServer.startServer();
35
}
catch
(AppException e){
36
System.out.println(
"Error in server : "
+e);
37
}
catch
(Exception e){
38
System.out.println(
"Error : "
+e);
39
}
40
}
41
}
|