01
//---- EchoServerData.java ----
02 package echoserver;
03
04
import
org.quickserver.net.server.*;
05
import
java.io.*;
06
07
public class
EchoServerData
implements
ClientData {
08
private int
helloCount;
09
private
String username;
10
11
public void
setHelloCount(
int
count) {
12
helloCount = count;
13
}
14
public int
getHelloCount() {
15
return
helloCount;
16
}
17
18
public void
setUsername(String username) {
19
this
.username = username;
20
}
21
public
String getUsername() {
22
return
username;
23
}
24
}
25
//--- end of code ---
|
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.EchoServerData"
;
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
try
{
22
myServer.startServer();
23
}
catch
(AppException e){
24
System.out.println(
"Error in server : "
+e);
25
}
26
}
27
}
|
01
package
echoserver;
02
03
import
org.quickserver.net.server.*;
04
import
java.io.*;
05
06
public class
EchoServerQuickAuthenticator
extends
QuickAuthenticator {
07
08
public boolean
askAuthorisation(ClientHandler clientHandler)
09
throws
IOException {
10
String username = askStringInput(clientHandler,
"User Name :"
);
11
if
(username!=null && username.equalsIgnoreCase(
"QUIT"
)) {
12
sendString(clientHandler,
"Logged out."
);
13
//close the connection
14
clientHandler.closeConnection();
15
return false
;
16
}
17
18
String password = askStringInput(clientHandler,
"Password :"
);
19
20
if
(username==
null
|| password ==
null
)
21
return false
;
22
23
if
(username.equals(password)) {
24
sendString(clientHandler,
"Auth OK"
);
25
//store the username in ClientData
26
EchoServerData data = (EchoServerData)clientHandler.getClientData();
27
data.setUsername(username);
28
return true
;
29
}
else
{
30
sendString(clientHandler,
"Auth Failed"
);
31
return false
;
32
}
33
}
34 } |
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
}
if
(command.equalsIgnoreCase(
"hello"
)) {
36
EchoServerData data = (EchoServerData) handler.getClientData();
37
data.setHelloCount(data.getHelloCount()+
1
);
38
if
(data.getHelloCount()==
1
) {
39
handler.sendClientMsg(
"Hello "
+data.getUsername());
40
}
else
{
41 handler.sendClientMsg("You told Hello "
+data.getHelloCount()+
42 " times. ");
43 }
44 } else
{
45
handler.sendClientMsg(
"Echo : "
+command);
46
}
47
}
48
}
|
01
//---- EchoServerPoolableData.java ----
02
package
echoserver;
03
04
import
org.quickserver.net.server.*;
05
import
java.io.*;
06
07
public class
EchoServerPoolableData
08
extends
EchoServerData
09
implements
org.apache.commons.pool.PoolableObjectFactory {
10
11
public void
activateObject(Object obj) {
12
}
13
public void
destroyObject(Object obj) {
14
if
(obj==
null
)
return
;
15
passivateObject(obj);
16
obj =
null
;
17
}
18
public
Object makeObject() {
19
return new
EchoServerPoolableData();
20
}
21
public void
passivateObject(Object obj) {
22
EchoServerPoolableData pd = (EchoServerPoolableData)obj;
23
pd.setHelloCount(
0
);
24
pd.setUsername(
null
);
25
}
26
public boolean
validateObject(Object obj) {
27
if
(obj==
null
)
28
return false
;
29
else
30
return true
;
31
}
32
}
33 //--- end of code --- |