getClientByID in spidermonkey Server class

Hey there,



I just talked this through with Lars and he agrees to put it in. I setup 2 methods which make it possible to get the Client by the clientID from the Server.



To do this, the Client now has a custom equals method which compares Clients by clientID and the Server has a new method:



public Client getClientByID(int clientID)



Through here you can get the exact Client you want from the list. I sent him the exact code already and he was planning on putting it in SVN some time today I remember correctly.



This should save all of use SpiderMonkey users a lot of looping through the entire list of Clients each time we want to send a message.



Mark

Ok, long overdue…



Here’s the actual code changes needed for this new feature to work:



Client class:

[java]

/**

  • {@inheritDoc}

    */

    public boolean equals(Object obj) {

    if (obj == null || !(obj instanceof Client || obj instanceof Integer)) {

    return false;

    } else if (obj instanceof Client){

    return ((Client)obj).getClientID() == getClientID();

    } else if (obj instanceof Integer) {

    return ((Integer)obj).intValue() == getClientID();

    } else {

    return false;

    }

    }

    [/java]



    Server class:

    [java]

    /**
  • Get a specific client based on the provided clientID.
  • @param clientID The clientID identifying the client requested.
  • @return The located client or null if the client was not on the list.

    */

    public Client getClientByID(int clientID) {

    Client c = null;

    if (getConnectors().contains(Integer.valueOf(clientID))) {

    c = getConnectors().get(getConnectors().indexOf(Integer.valueOf(clientID)));

    }

    return c;

    }

    [/java]



    By adding these two method to the appropriate classes, you get the server.getClientByID method which allows you to get a specific client straight from the server. This eliminates the need to either keep your own list of Client objects or to traverse the entire collection each time you want to find a specific Client.



    These two methods have already been approved and should be in soon(ish) now.



    Mark