/** * Process Pull query request * * @param request * @return */ public Response PullQueryRequestProcess(Request request) { String[] msg = request.getMessage().get(0).trim().split("\\s+"); this.setNextPeerHostName(msg[0]); this.setNextPeerPort(msg[1]); this.setNextPeerID(request.getPeerID()); ArrayList<String> responseMessage = new ArrayList<String>(); responseMessage.add("blah"); return new Response(Settings.Version, "PULL", "1", "200", "ok", responseMessage); }
/** * This is the RequestProcessor thread that continuously processes Client Requests from the * RequestProcessor Request Queue. */ public void run() { done = false; while (!done) { if (!requests.isEmpty()) { Request r = requests.remove(); System.out.println("Processing Request: " + r.getMessage()); protocol.processRequest(r); } else { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
/** * 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); } }
/** * 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); } }