1 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 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 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 handler.setDataMode(DataMode.OBJECT, DataType.OUT); 55 handler.sendClientObject(new Date ()); 56 handler.setDataMode(DataMode.STRING, DataType.OUT); 57 handler.setDataMode(DataMode.OBJECT, DataType.IN); 62 } else { 63 ObjectOutputStream oos = 65 new ObjectOutputStream(handler.getOutputStream()); 66 oos.writeObject(new Date ()); 67 oos.flush(); 68 oos = null; 69 70 ObjectInputStream ois = 71 new ObjectInputStream(handler.getInputStream()); 72 try { 73 Date gotDate = (Date ) ois.readObject(); 74 handler.sendSystemMsg("Got date : " + gotDate); 75 } catch (ClassNotFoundException 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 |