Skip to content
servasat edited this page Jun 27, 2011 · 14 revisions

Commands are the standardized way in which jAuction Client and jAuction Server communicate.

Server

For the jAuction Server Commands are implemented by extending the abstract class ServerCommand which implements the Interface ServerCommandRules.

ServerCommandRules

public interface ServerCommandRules {
  public String name();
  public String responseName();
  public boolean parseJson(JSONObject data);
  public JSONObject requestSpecification();
  public JSONObject responseSpecification();
}

ServerCommand

public abstract class ServerCommand implements ServerCommandRules, Runnable {	
      ServerCommand(Connection con){
        this.con = con;
      }
      protected JSONObject specificationMapper(String type, HashMap data){
        ...
      }
      public String responseName(){
        return this.name();
      }
      public Connection con;
      protected boolean authenticate(String auth_key){
        if (this.con.user != null && this.con.user.getAuthKey().equals(auth_key)){
          	return true;
        }else{
          this.con.notify("wrong auth_key");
          return false;
        }
      }
}

To Create a new Command Object we use the ServerCommandFactory

public class ServerCommandFactory {
    private Hashtable<String, String> serverCommands = new Hashtable<String, String>();
    
    public Hashtable<String, String> getServerCommands(){
    	return this.serverCommands;
    }
    
    public ServerCommandFactory(){
        serverCommands.put("login", "Login");
        ...
        serverCommands.put("quit", "Quit");
    }
    public ServerCommand getCommand(String name, Connection con){
      ServerCommand sc = null;
      try {
        if(serverCommands.containsKey(name)){
          Class cla = (Class) Class.forName("server.ServerCommands."+serverCommands.get(name));
          try {
            sc = (ServerCommand) cla.getDeclaredConstructor(con.getClass()).newInstance(con);
          }catch(Exception e){
            System.out.println("Dynamic ServerCommand loading failed");
            e.printStackTrace();
          }
        }else{
          System.out.println("Unknown Command "+name);
        }
      }catch(ClassNotFoundException e){
        System.out.println("Class not found");
        e.printStackTrace();
      }
      return sc;
    }
}
Clone this wiki locally