01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package dateserver;
16:
17: import java.net.*;
18: import java.io.*;
19: import java.util.Date;
20: import org.quickserver.net.server.*;
21:
22: public class CommandHandler implements ClientCommandHandler {
23:
24: public void gotConnected(ClientHandler handler)
25: throws SocketTimeoutException, IOException {
26: handler.sendSystemMsg("Connection opened : "
27: + handler.getSocket().getInetAddress());
28:
29: handler.sendClientMsg("Welcome to DateServer v "
30: + DateServer.VER);
31: }
32:
33: public void lostConnection(ClientHandler handler)
34: throws IOException {
35: handler.sendSystemMsg("Connection lost : "
36: + handler.getSocket().getInetAddress());
37: }
38:
39: public void closingConnection(ClientHandler handler)
40: throws IOException {
41: handler.sendSystemMsg("Connection closed : "
42: + handler.getSocket().getInetAddress());
43: }
44:
45: public void handleCommand(ClientHandler handler, String command)
46: throws SocketTimeoutException, IOException {
47:
48: if (command.toLowerCase().equals("quit")) {
49: handler.sendClientMsg("Bye ;-)");
50: handler.closeConnection();
51: } else if (command.toLowerCase().equals("exchange date")) {
52: float version = QuickServer.getVersionNo();
53: if (version >= 1.2) {
54: //we can use built in support
55: handler.setDataMode(DataMode.OBJECT, DataType.OUT);
56: handler.sendClientObject(new Date());
57: handler.setDataMode(DataMode.STRING, DataType.OUT);
58: //will block until the client ObjectOutputStream
59: //has written and flushed the header.
60: //we know our client will send date object
61: //as soon as it recives our date its ok
62: handler.setDataMode(DataMode.OBJECT, DataType.IN);
63: } else {
64: //no built in support
65: ObjectOutputStream oos = new ObjectOutputStream(handler
66: .getOutputStream());
67: oos.writeObject(new Date());
68: oos.flush();
69: oos = null;
70:
71: ObjectInputStream ois = new ObjectInputStream(handler
72: .getInputStream());
73: try {
74: Date gotDate = (Date) ois.readObject();
75: handler.sendSystemMsg("Got date : " + gotDate);
76: } catch (ClassNotFoundException e) {
77: handler.sendSystemMsg("Unknown object got : " + e);
78: }
79: ois = null;
80: }
81: } else {
82: handler.sendSystemMsg("Got cmd : " + command);
83: handler.sendClientMsg("You Sent : " + command);
84: }
85: }
86: }
|