01
package
echoserver;
02
03
import
org.quickserver.net.*;
04
import
org.quickserver.net.server.*;
05
06
import
java.io.*;
07
import
java.util.logging.*;
08
09
public class
EchoServer {
10
public static void
main(String s[]) {
11
12
String cmd =
"echoserver.EchoCommandHandler"
;
13
String auth =
"echoserver.EchoServerQuickAuthenticator"
;
14
String data =
"echoserver.EchoServerPoolableData"
;
//Poolable
15
16
QuickServer myServer =
new
QuickServer();
17
18
//setup logger to log to file
19
Logger logger =
null
;
20
FileHandler xmlLog =
null
;
21
File log =
new
File(
"./log/"
);
22
if
(!log.canRead())
23
log.mkdir();
24
try
{
25
logger = Logger.getLogger(
""
);
//get root logger
26
logger.setLevel(Level.INFO);
27 xmlLog = new FileHandler("log/EchoServer.xml"
);
28 logger.addHandler(xmlLog);
29 } catch(IOException e){
30 System.err.println("Could not create xmlLog FileHandler : "+e);
31 }
32
//set logging level to fine
33 myServer setConsoleLoggingLevel(Level INFO);
34
35
36
myServer.setClientCommandHandler(cmd);
37
myServer.setAuthenticator(auth);
38
myServer.setClientData(data);
39
40
myServer.setPort(
4123
);
41
myServer.setName(
"Echo Server v 1.0"
);
42
43
//store data needed to be changed by QSAdminServer
44
Object[] store = new Object[]{
"12.00"
};
45
myServer.setStoreObjects(store);
46
47
//config QSAdminServer
48
myServer.setQSAdminServerPort(
4124
);
49
myServer.getQSAdminServer().getServer().setName(
"EchoAdmin v 1.0"
);
50 try
{
51
//add command plugin
52
myServer.getQSAdminServer().setCommandPlugin(
53
"echoserver.QSAdminCommandPlugin"
);
54
myServer.startQSAdminServer();
55
myServer.startServer();
56
} catch
(AppException e){
57
System.out.println(
"Error in server : "
+e);
58
} catch
(Exception e){
59
System.out.println(
"Error : "
+e);
60
}
61
}
62
}
|
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
import
java.util.logging.*;
09
10
public class
EchoCommandHandler
implements
ClientCommandHandler {
11
12
public void
gotConnected(ClientHandler handler)
13
throws
SocketTimeoutException, IOException {
14
handler.sendSystemMsg(
"New Client : "
+
15
handler.getSocket().getInetAddress().getHostAddress(),
16
Level.INFO);
17
handler.sendClientMsg(
"+++++++++++++++++++++++++++++++"
);
18
handler.sendClientMsg(
"| Welcome to EchoServer v 1.0 |"
);
19
handler.sendClientMsg(
"| Note: Password = Username |"
);
20
handler.sendClientMsg(
"| Send 'Quit' to exit |"
);
21
handler.sendClientMsg(
"+++++++++++++++++++++++++++++++"
);
22
}
23
public void
lostConnection(ClientHandler handler)
24
throws
IOException {
25
handler.sendSystemMsg(
"Connection lost : "
+
26
handler.getSocket().getInetAddress());
27
}
28
public void
closingConnection(ClientHandler handler)
29
throws
IOException {
30
handler.sendSystemMsg(
"Closing connection : "
+
31
handler.getSocket().getInetAddress());
32
}
33
34
public void
handleCommand(ClientHandler handler, String command)
35
throws
SocketTimeoutException, IOException {
36
if
(command.equals(
"Quit"
)) {
37
handler.sendClientMsg(
"Bye ;-)"
);
38
handler.closeConnection();
39
return
;
40
}
41
if
(command.equals(
"What's interest?"
)) {
42
handler.sendClientMsg(
"Interest is : "
+
43
(String)handler.getServer().getStoreObjects()[
0
]+
44
"%"
);
45
}
else if
(command.equalsIgnoreCase(
"hello"
)) {
46
EchoServerData data = (EchoServerData) handler.getClientData();
47
data.setHelloCount(data.getHelloCount()+
1
);
48
if
(data.getHelloCount()==
1
) {
49
handler.sendClientMsg(
"Hello "
+data.getUsername());
50
}
else
{
51
handler.sendClientMsg(
"You told Hello "
+data.getHelloCount()+
52
" times. "
);
53
}
54
}
else
{
55
handler.sendClientMsg(
"Echo : "
+command);
56
}
57
}
58
}
|
01
package
echoserver;
02
03
import
org.quickserver.net.*;
04
import
org.quickserver.net.server.*;
05
06
import
java.io.*;
07
import
java.util.logging.*;
08
09
public class
EchoServer {
10
public static void
main(String s[]) {
11
12
String cmd =
"echoserver.EchoCommandHandler"
;
13
String auth =
"echoserver.EchoServerQuickAuthenticator"
;
14
String data =
"echoserver.EchoServerPoolableData"
;
//Poolable
15
16
QuickServer myServer =
new
QuickServer();
17
18
//setup logger to log to file
19
Logger logger =
null
;
20
FileHandler xmlLog =
null
;
21
FileHandler txtLog =
null
;
22
File log =
new
File(
"./log/"
);
23
if
(!log.canRead())
24
log.mkdir();
25
try
{
26
logger = Logger.getLogger(
"org.quickserver.net"
);
//get QS logger
27
logger.setLevel(Level.FINEST);
28
xmlLog =
new
FileHandler(
"log/EchoServer.xml"
);
29
logger.addHandler(xmlLog);
30
31
logger = Logger.getLogger(
"echoserver"
);
//get App logger
32
logger.setLevel(Level.FINEST);
33
txtLog =
new
FileHandler(
"log/EchoServer.txt"
);
34
txtLog.setFormatter(
new
SimpleFormatter());
35
logger.addHandler(txtLog);
36
myServer.setAppLogger(logger);
//img : Sets logger to be used for app.
37
}
catch
(IOException e){
38
System.err.println(
"Could not create xmlLog FileHandler : "
+e);
39
}
40
//set logging level to fine
41
myServer.setConsoleLoggingLevel(Level.INFO);
42
43
44
myServer.setClientCommandHandler(cmd);
45
myServer.setAuthenticator(auth);
46
myServer.setClientData(data);
47
48
myServer.setPort(
4123
);
49
myServer.setName(
"Echo Server v 1.0"
);
50
51
//store data needed to be changed by QSAdminServer
52
Object[] store =
new
Object[]{
"12.00"
};
53
myServer.setStoreObjects(store);
54
55
//config QSAdminServer
56
myServer.setQSAdminServerPort(
4124
);
57
myServer.getQSAdminServer().getServer().setName(
"EchoAdmin v 1.0"
);
58
try
{
59
//add command plugin
60
myServer.getQSAdminServer().setCommandPlugin(
61
"echoserver.QSAdminCommandPlugin"
);
62
myServer.startQSAdminServer();
63
myServer.startServer();
64
}
catch
(AppException e){
65
System.out.println(
"Error in server : "
+e);
66
}
catch
(Exception e){
67
System.out.println(
"Error : "
+e);
68
}
69
}
70
}
|