Beispiel #1
0
 /**
  * Process Query request
  *
  * @param request
  * @return
  */
 public Response QueryRequestProcess(Request request) {
   ArrayList<String> responseMessage = new ArrayList<String>();
   String msg = request.getMessage().get(0).trim();
   int key = PeerNode.getAscii(msg) % this.getMaxId();
   Hashtable<Integer, String> hashtbl = this.getHashtable();
   if (hashtbl.containsValue(msg)) {
     return new Response(Settings.Version, "QUERY", "0", "200", "OK", responseMessage);
   } else if (getInt(this.getID()) <= key && key < getInt(this.getNextPeerID())
       || getInt(this.getID()) <= key && getInt(this.getID()) > getInt(this.getNextPeerID())
       || getInt(this.getNextPeerID()) > key
           && getInt(this.getID()) > getInt(this.getNextPeerID())) {
     return new Response(Settings.Version, "ADD", "0", "201", "NotPresent", responseMessage);
   } else {
     responseMessage.add(msg);
     return new Response(Settings.Version, "ADD", "0", "400", "NotResponsible", responseMessage);
   }
 }
Beispiel #2
0
 /**
  * Process Add query request
  *
  * @param request
  * @return
  */
 public Response AddQueryRequestProcess(Request request) {
   ArrayList<String> responseMessage = new ArrayList<String>();
   String msg = request.getMessage().get(0).trim();
   int key =
       PeerNode.getAscii(msg)
           % this
               .getMaxId(); // get the Ascii value and convert it to the range of ids by modula
                            // maxId
   Hashtable<Integer, String> hashtbl = this.getHashtable();
   if (hashtbl.containsValue(msg)) {
     return new Response(Settings.Version, "ADD", "0", "202", "duplicate", responseMessage);
   } else if (getInt(this.getID()) <= key && key < getInt(this.getNextPeerID())
       || getInt(this.getID()) <= key && getInt(this.getID()) > getInt(this.getNextPeerID())
       || getInt(this.getNextPeerID()) > key
           && getInt(this.getID()) > getInt(this.getNextPeerID())) {
     hashtbl.put(key, msg);
     return new Response(Settings.Version, "ADD", "0", "200", "ok", responseMessage);
   } else {
     responseMessage.add(msg);
     nextClient(new Request("ADD", Settings.Version, 0, this.getID(), responseMessage).toString());
     return new Response(Settings.Version, "ADD", "0", "400", "NotResponsible", responseMessage);
   }
 }