public void ruN(int index1) { StringBuilder result = new StringBuilder(""); List<Client> clientList = Server.getInstance().getShallowCopyClients(); Client[] clients = clientList.ToArray(); Client[] clientsOtherThanThis = Server.getInstance() .getClientsArrayOtherThan(clients[index1]); //set the state of this client to wanted, because //it wants to access critical section clients[index1].setWanted(true); //and before submitting request, the client must update its //logical clock, so //update clock, which will increment default timestamp //basically Ricart&Agrawala is using extended Lamport clock //in extended lamport clock, for each send event, the node //has to update its clock (increment timestamp) clients[index1].updateClock(); Client clienT = new Client(Server.getInstance().getSocketAddress(), Utils.getDefaultSocketAddress()); object reply = null; node = clienT.nodeList.Values.Last(); foreach( Client client in clientsOtherThanThis) { bool isRequestSendingOK = false; try { Console.WriteLine("Client " + clients[index1].getIP() + " with timeStamp " + clients[index1].getClock() + " sending \"request\" to client " + client.getIP() + "\n"); result.Append("Client " + clients[index1].getIP() + " with timeStamp " + clients[index1].getClock() + " sending \"request\" to client " + client.getIP() + "\n"); reply = node.sendmessage("request", clients[index1].getIP(), clients[index1].getClock(), client.getIP()); //if we have reached here request sending has been successful isRequestSendingOK = true; } catch(Exception e) { Console.WriteLine("Exception while executing the " + "RequestProcessor.sendMessage method for" + " each client other than this client"); result.Append("Exception while executing the " + "RequestProcessor.sendMessage method for" + " each client other than this client\n"); } if (isRequestSendingOK) { //cast reply to string string replyString = (string)reply; if (replyString.Contains(",")) { //now split and get OK sender's timestamp string[] splitComponents = replyString.Split(','); if (splitComponents[0].Equals("OK", StringComparison.InvariantCultureIgnoreCase)) { //update the logical clock, when received OK //from the other node, because it is receive event clients[index1].updateClock(int.Parse(splitComponents[1])); //increment the OK count for this client clients[index1].incrementOKCount(); } } } } //so, number of OKCount is the same as number of clients //other than this. In other words, if received "OK" from //all clients that this client has sent the request to while (clients[index1].getOKCount() != clientsOtherThanThis.Length) { //wait } if (clients[index1].getOKCount() == clientsOtherThanThis.Length) { //now this client can enter the critical section //set the using flag for this client clients[index1].setUsing(true); Server.getInstance().assignToCriticalSection(clients[index1].getIP()); // get the random English word string randomEnglishWord = Server.getInstance().generateRandomEnglishWord(); // method to write this generated word to // client's // buffer to check it later whether or not // written // word exists in the resulting master string clients[index1].rememberWord(randomEnglishWord); // now enter the critical section where the word // will be written // to the coordinator's master string // boolean variable to hold whether // criticalSection entrance was OK bool isCriticalSectionSuccess = false; try { // params will contain the client's ip who // will enter // the critical section and and the // randomEnglishWord reply = node.enterCS(clients[index1].getIP(), randomEnglishWord); // reply could be like // "Node with ip has written some word" Console.WriteLine((string)reply); result.Append((string)reply); //if we have reached here critical section completed successfully isCriticalSectionSuccess = true; } catch (Exception e) { Console.WriteLine("Exception while calling method" + " RequestProcessor.enterCS\n"); } if (isCriticalSectionSuccess) { // update clock when sending OK clients[index1].updateClock(); int oksSent = 0; int numberOfNodesOkToBeSent = clients[index1].getRequestQueue().Count(); //get the queue, send OK to all processes in own queue //and empty queue. foreach (string IP in clients[index1].getRequestQueue()) { try { result.Append("Client " + clients[index1].getIP() + " with timeStamp " + clients[index1].getClock() + " sending \"OK\" to client " + IP + "\n"); node.sendmessage("OK", clients[index1].getIP(), clients[index1].getClock(), IP); oksSent++; } catch (Exception e) { Console.WriteLine("Exception while executing the " + "RequestProcessor.sendMessage method for" + " each client other than this client"); } } //if all oks sent successfully if (oksSent == numberOfNodesOkToBeSent) { Server.getInstance().removeFromCriticalSection(clients[index1].getIP()); //now empty queue, set using and wanted flags to false //and resetOKCount to 0. clients[index1].emptyRequestQueue(); clients[index1].setUsing(false); clients[index1].setWanted(false); clients[index1].resetOKCount(); } } } }
public string receiveMessage(string message, string requesterIP) { // if the received message is "request" if (message.Equals("request", StringComparison.InvariantCultureIgnoreCase)) { //if critical section is empty, access has to be granted if (Server.getInstance().isCriticalSectionEmpty()) { // so send "GRANTED" Server.getInstance().assignToCriticalSection(requesterIP); return "GRANTED"; } else // someone is in the critical section { // queue the request, return DENIED Server.getInstance().queueRequest(requesterIP); return "DENIED"; } } else if (message.Equals("release", StringComparison.InvariantCultureIgnoreCase)) { // requester is done with critical section // remove the requesterIP from critical section Server.getInstance().removeFromCriticalSection(requesterIP); // if queue is not empty, dequeue first and reply "GRANTED" if (!Server.getInstance().isQueueEmpty()) { string newRequesterIP = Server.getInstance().dequeue(); //assign critical section new enterer ip Server.getInstance().assignToCriticalSection(newRequesterIP); //print out who is the new enterer Console.WriteLine("New Critical Section Enterer IP: " + newRequesterIP); Client client = Server.getInstance().getClientByIP(newRequesterIP); // get the random English word string randomEnglishWord = Server.getInstance() .generateRandomEnglishWord(); // method to write this generated word to // client's // buffer to check it later whether or not // written // word exists in the resulting master string client.rememberWord(randomEnglishWord); try { node = client.nodeList.Values.Last(); object reply = node.enterCS(client.getIP(),randomEnglishWord); Console.WriteLine((string)reply); } catch (Exception e) { Console.WriteLine("Exception while calling method" + " RequestProcessor.enterCS\n"); } //to prevent deadlock, because the same thread can //access the sendMessage again ThreadStart threadDelegate = new ThreadStart(run); Thread tempThread = new Thread(threadDelegate); tempThread.Start(); return "GRANTED"; } else // queue is already empty, so reply GRANTED { return "GRANTED"; } } else { return "GRANTED"; } }
public void ruN(int index1) { StringBuilder result = new StringBuilder(""); List <Client> clientList = Server.getInstance().getShallowCopyClients(); Client[] clients = clientList.ToArray(); Client[] clientsOtherThanThis = Server.getInstance() .getClientsArrayOtherThan(clients[index1]); //set the state of this client to wanted, because //it wants to access critical section clients[index1].setWanted(true); //and before submitting request, the client must update its //logical clock, so //update clock, which will increment default timestamp //basically Ricart&Agrawala is using extended Lamport clock //in extended lamport clock, for each send event, the node //has to update its clock (increment timestamp) clients[index1].updateClock(); Client clienT = new Client(Server.getInstance().getSocketAddress(), Utils.getDefaultSocketAddress()); object reply = null; node = clienT.nodeList.Values.Last(); foreach (Client client in clientsOtherThanThis) { bool isRequestSendingOK = false; try { Console.WriteLine("Client " + clients[index1].getIP() + " with timeStamp " + clients[index1].getClock() + " sending \"request\" to client " + client.getIP() + "\n"); result.Append("Client " + clients[index1].getIP() + " with timeStamp " + clients[index1].getClock() + " sending \"request\" to client " + client.getIP() + "\n"); reply = node.sendmessage("request", clients[index1].getIP(), clients[index1].getClock(), client.getIP()); //if we have reached here request sending has been successful isRequestSendingOK = true; } catch (Exception e) { Console.WriteLine("Exception while executing the " + "RequestProcessor.sendMessage method for" + " each client other than this client"); result.Append("Exception while executing the " + "RequestProcessor.sendMessage method for" + " each client other than this client\n"); } if (isRequestSendingOK) { //cast reply to string string replyString = (string)reply; if (replyString.Contains(",")) { //now split and get OK sender's timestamp string[] splitComponents = replyString.Split(','); if (splitComponents[0].Equals("OK", StringComparison.InvariantCultureIgnoreCase)) { //update the logical clock, when received OK //from the other node, because it is receive event clients[index1].updateClock(int.Parse(splitComponents[1])); //increment the OK count for this client clients[index1].incrementOKCount(); } } } } //so, number of OKCount is the same as number of clients //other than this. In other words, if received "OK" from //all clients that this client has sent the request to while (clients[index1].getOKCount() != clientsOtherThanThis.Length) { //wait } if (clients[index1].getOKCount() == clientsOtherThanThis.Length) { //now this client can enter the critical section //set the using flag for this client clients[index1].setUsing(true); Server.getInstance().assignToCriticalSection(clients[index1].getIP()); // get the random English word string randomEnglishWord = Server.getInstance().generateRandomEnglishWord(); // method to write this generated word to // client's // buffer to check it later whether or not // written // word exists in the resulting master string clients[index1].rememberWord(randomEnglishWord); // now enter the critical section where the word // will be written // to the coordinator's master string // boolean variable to hold whether // criticalSection entrance was OK bool isCriticalSectionSuccess = false; try { // params will contain the client's ip who // will enter // the critical section and and the // randomEnglishWord reply = node.enterCS(clients[index1].getIP(), randomEnglishWord); // reply could be like // "Node with ip has written some word" Console.WriteLine((string)reply); result.Append((string)reply); //if we have reached here critical section completed successfully isCriticalSectionSuccess = true; } catch (Exception e) { Console.WriteLine("Exception while calling method" + " RequestProcessor.enterCS\n"); } if (isCriticalSectionSuccess) { // update clock when sending OK clients[index1].updateClock(); int oksSent = 0; int numberOfNodesOkToBeSent = clients[index1].getRequestQueue().Count(); //get the queue, send OK to all processes in own queue //and empty queue. foreach (string IP in clients[index1].getRequestQueue()) { try { result.Append("Client " + clients[index1].getIP() + " with timeStamp " + clients[index1].getClock() + " sending \"OK\" to client " + IP + "\n"); node.sendmessage("OK", clients[index1].getIP(), clients[index1].getClock(), IP); oksSent++; } catch (Exception e) { Console.WriteLine("Exception while executing the " + "RequestProcessor.sendMessage method for" + " each client other than this client"); } } //if all oks sent successfully if (oksSent == numberOfNodesOkToBeSent) { Server.getInstance().removeFromCriticalSection(clients[index1].getIP()); //now empty queue, set using and wanted flags to false //and resetOKCount to 0. clients[index1].emptyRequestQueue(); clients[index1].setUsing(false); clients[index1].setWanted(false); clients[index1].resetOKCount(); } } } }
public string receiveMessage(string message, string requesterIP) { // if the received message is "request" if (message.Equals("request", StringComparison.InvariantCultureIgnoreCase)) { //if critical section is empty, access has to be granted if (Server.getInstance().isCriticalSectionEmpty()) { // so send "GRANTED" Server.getInstance().assignToCriticalSection(requesterIP); return("GRANTED"); } else // someone is in the critical section { // queue the request, return DENIED Server.getInstance().queueRequest(requesterIP); return("DENIED"); } } else if (message.Equals("release", StringComparison.InvariantCultureIgnoreCase)) { // requester is done with critical section // remove the requesterIP from critical section Server.getInstance().removeFromCriticalSection(requesterIP); // if queue is not empty, dequeue first and reply "GRANTED" if (!Server.getInstance().isQueueEmpty()) { string newRequesterIP = Server.getInstance().dequeue(); //assign critical section new enterer ip Server.getInstance().assignToCriticalSection(newRequesterIP); //print out who is the new enterer Console.WriteLine("New Critical Section Enterer IP: " + newRequesterIP); Client client = Server.getInstance().getClientByIP(newRequesterIP); // get the random English word string randomEnglishWord = Server.getInstance() .generateRandomEnglishWord(); // method to write this generated word to // client's // buffer to check it later whether or not // written // word exists in the resulting master string client.rememberWord(randomEnglishWord); try { node = client.nodeList.Values.Last(); object reply = node.enterCS(client.getIP(), randomEnglishWord); Console.WriteLine((string)reply); } catch (Exception e) { Console.WriteLine("Exception while calling method" + " RequestProcessor.enterCS\n"); } //to prevent deadlock, because the same thread can //access the sendMessage again ThreadStart threadDelegate = new ThreadStart(run); Thread tempThread = new Thread(threadDelegate); tempThread.Start(); return("GRANTED"); } else // queue is already empty, so reply GRANTED { return("GRANTED"); } } else { return("GRANTED"); } }