KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dateserver > CommandHandler


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
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 JavaDoc;
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     public void closingConnection(ClientHandler handler)
39         throws IOException {
40         handler.sendSystemMsg("Connection closed : " +
41             handler.getSocket().getInetAddress());
42     }
43
44     public void handleCommand(ClientHandler handler, String JavaDoc command)
45             throws SocketTimeoutException, IOException {
46         
47         if(command.toLowerCase().equals("quit")) {
48             handler.sendClientMsg("Bye ;-)");
49             handler.closeConnection();
50         } else if(command.toLowerCase().equals("exchange date")) {
51             float version = QuickServer.getVersionNo();
52             if(version >= 1.2) {
53                 //we can use built in support
54
handler.setDataMode(DataMode.OBJECT, DataType.OUT);
55                 handler.sendClientObject(new Date JavaDoc());
56                 handler.setDataMode(DataMode.STRING, DataType.OUT);
57                 //will block until the client ObjectOutputStream
58
//has written and flushed the header.
59
//we know our client will send date object
60
//as soon as it recives our date its ok
61
handler.setDataMode(DataMode.OBJECT, DataType.IN);
62             } else {
63                 //no built in support
64
ObjectOutputStream oos =
65                     new ObjectOutputStream(handler.getOutputStream());
66                 oos.writeObject(new Date JavaDoc());
67                 oos.flush();
68                 oos = null;
69                 
70                 ObjectInputStream ois =
71                     new ObjectInputStream(handler.getInputStream());
72                 try {
73                     Date JavaDoc gotDate = (Date JavaDoc) ois.readObject();
74                     handler.sendSystemMsg("Got date : " + gotDate);
75                 } catch (ClassNotFoundException JavaDoc e) {
76                     handler.sendSystemMsg("Unknown object got : "+e);
77                 }
78                 ois = null;
79             }
80         } else {
81             handler.sendSystemMsg("Got cmd : " + command);
82             handler.sendClientMsg("You Sent : " + command);
83         }
84     }
85 }
86
Popular Tags