public bool add(RequestObject obj) { return(this.queue.Add(obj)); }
//this will call by others //@Override public String requestAddPermission(String requesterId, int requesterLogicalClock, String requesterHostUrl, int requesterHostPort) { //the clock will manage here too //this will call by others to send [req<id,clock>] to this host //By receiving a req message we will send a true that mean we receive your message //if we want to accept the request at time we will send a OK instead //we must implement this pseudocode // ********************************************* // if (not accessing resource and do not want to access it) // send OK // else if (currently using resource) // queue request // else if (want to access resource too) // if (timestamp of request is smaller) // send OK // else //own timestamp is smaller // queue request // ********************************************* try{ if (!ServerStatus.getServerStatus()) { return(null); } long id = Convert.ToInt64(requesterId); HostUrl hostUrl = new HostUrl(requesterHostUrl, requesterHostPort); RequestObject requestObject = new RequestObject(id, requesterLogicalClock, hostUrl); //the clock will correct in the constructor of ExternalLampartClock class based on following rule //LC = Max(LC, LCsender) + 1 String response = null; if (currentAddRequest == null) //if not accessing resource and do not want to access it --> send OK { ExtendedLamportClockObject ELC = new ExtendedLamportClockObject(); //for increasing clock before send response = "true" + "\n"; //Send 'OK' response += " ID:&@[" + ELC.getIdString() + "]#! "; response += " LC:&@[" + ELC.getLogicalClock() + "]#! "; return(response); } else if (!currentAddRequest.isWaiting()) //if currently using resource { addRequestsQueue.add(requestObject); response = "false" + "\n"; //Send 'NOKEY'//means 'you have to wait for me' return(response); } else //if want to access resource too [(currentAddRequest != null) && (currentAddRequest.isWaiting() == true)] { if (requestObject.getELCO().compare(currentAddRequest.getELCO()) < 0) //if timestamp of requester is smaller { ExtendedLamportClockObject ELC = new ExtendedLamportClockObject(); //for increasing clock before send response = "true" + "\n"; //Send 'OK' response += " ID:&@[" + ELC.getIdString() + "]#! "; response += " LC:&@[" + ELC.getLogicalClock() + "]#! "; return(response); } else //if own timestamp is smaller { addRequestsQueue.add(requestObject); response = "false" + "\n"; //Send 'OK'//means 'you have to wait for me' return(response); } } }catch (Exception) { return(null); } }