public String joinNetwork(String iD, String address) { NetworkBean data = new NetworkBean(); CsClient csclient = new CsClient(); Dictionary <String, String> returnAddressIdMap = new Dictionary <String, String>(); foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { returnAddressIdMap.Add(entry.Key, entry.Value); } // call function to send the new address to all nodes try { csclient.addReturnAdressToList(address); csclient.propagateNewNodeAddress(returnAddressIdMap, iD, address); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } returnAddressIdMap.Add(data.getpId(), data.getAddress()); data.addValuesToDictionary(iD, address); try { csclient.setMasterNode(address); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } return(data.getpId()); }
//determine the critical section entry public String RA_RecieveRequest(String iD, String timeStamp) { LamportClock clock = new LamportClock(); clock.receiveAction(timeStamp); NetworkBean data = new NetworkBean(); Console.WriteLine("recieved RA operation request from " + iD + "at time " + DateTime.Now.ToString("HH:mm:ss:fff")); if (usingCR) { Console.WriteLine("Already using CR.. Defer the request " + iD); deferRequest(iD); return("Deferred_" + clock.sendAction()); } //myTimeStamp if (requestCR) { // clock.receiveAction(timeStamp); //timeStamp = clock.getValue().ToString(); if ((clock.getRequestTimeStamp().ToString()).CompareTo(timeStamp) > 0) { Console.WriteLine("already requested CR..my request Time stamp=" + clock.getRequestTimeStamp() + " " + iD + " request timestamp =" + timeStamp); Console.WriteLine("Send OK Message"); return("OK_" + clock.sendAction()); } if ((clock.getRequestTimeStamp().ToString()).CompareTo(timeStamp) < 0) { Console.WriteLine("already requested CR...my request Time stamp= " + clock.getRequestTimeStamp() + " the node " + iD + " request timestamp is =" + timeStamp); Console.WriteLine("Deferr the request"); deferRequest(iD); return("Deferred_" + clock.sendAction()); } if ((clock.getRequestTimeStamp().ToString()).CompareTo(timeStamp) == 0) { if (iD.CompareTo(data.getpId()) > 0) { Console.WriteLine("already requested CR...my request Time stamp= " + clock.getRequestTimeStamp() + " the node " + iD + " request timestamp is =" + timeStamp); Console.WriteLine("Both clocktimes are same...my process id is" + data.getpId() + " the node is is " + iD + " My id is lesser"); Console.WriteLine("Deferr the request"); deferRequest(iD); return("Deferred_" + clock.sendAction()); } if (iD.CompareTo(data.getpId()) < 0) { Console.WriteLine("already requested CR...my request Time stamp= " + clock.getRequestTimeStamp() + " the node " + iD + " request timestamp is =" + timeStamp); Console.WriteLine("Both clocktimes are same...my process id is" + data.getpId() + " the node is is " + iD + " My id is higher"); Console.WriteLine("send ok message"); return("OK_" + clock.sendAction()); } } } Console.WriteLine("Not Using or requesting CR... Send OK Message"); return("OK_" + clock.sendAction()); }
public void bullyAlgorithm() { NetworkBean data = new NetworkBean(); Boolean noMasterFound = true; // ServerOperations serveroperation = new ServerOperations(); String response = ""; foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { String sendAddress = entry.Value; long pId = Int64.Parse(entry.Key); long currentNodepId = Int64.Parse(data.getpId()); if (pId > currentNodepId) { try { elctMasterNode proxy = XmlRpcProxyGen.Create <elctMasterNode>(); proxy.Url = sendAddress; response = proxy.electMasterNode(data.getpId()); Console.WriteLine(data.getpId() + " Sends the bully election message to :" + sendAddress); Console.WriteLine("response is :" + response); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("no response"); // removeNode(sendAddress); data.getpId_Address().Remove(sendAddress); } } if (response.Length > 0) { noMasterFound = false; break; } } // if no master is found then this node is the new master if (noMasterFound) { Console.WriteLine("No master found I am the new master"); data.setMasterPid(data.getpId()); data.setMasterAddress(data.getAddress()); propagateNewMasterNode(data.getpId(), data.getAddress()); } }
public void checkFinalString() { var stopwatch = new Stopwatch(); stopwatch.Start(); while (stopwatch.Elapsed < TimeSpan.FromSeconds(20)) { } String message = ""; NetworkBean data = new NetworkBean(); CsClient client = new CsClient(); ServerOperations serveroperaation = new ServerOperations(); Console.WriteLine("**************Reading The final String*******************"); try { distribRead proxy = XmlRpcProxyGen.Create <distribRead>(); proxy.Url = data.getMasterAddress(); message = proxy.distributedRead(data.getpId()); Console.WriteLine("the final string is \n"); Console.WriteLine(message + "\n"); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("Could not send the message succesfully.. node denied connection"); Console.WriteLine("No response from the master node"); Console.WriteLine("Starting election to elect new master node ( Bully algorith)"); client.removeNode(data.getMasterPid()); data.getpId_Address().Remove(data.getMasterPid()); client.bullyAlgorithm(); Console.WriteLine(e.StackTrace); } Boolean wordFlag = true; foreach (String word in data.getAddedWords()) { if (message.Contains(word)) { Console.WriteLine(word + " is present in the final string"); } else { Console.WriteLine(word + " is not present in the final string"); wordFlag = false; } } if (wordFlag) { Console.WriteLine("all the words are added to the string in master node"); } //end// data.getAddedWords().Clear(); data.setNoOfNodesFinishedOperation(0); data.setStartRicartAgarwala(false); data.setStartCME(false); }
public void releaseDeferedRequests() { //remove the Ids from queue //send OK message to the Ids //queue.Count != 0 while (ricartAgarwalaQueue.Count != 0) { String iD = ricartAgarwalaQueue.Dequeue(); NetworkBean data = new NetworkBean(); String address = data.getpId_Address()[iD]; ServerOperations serveroperaation = new ServerOperations(); LamportClock clock = new LamportClock(); try { rcvOkFromRA proxy = XmlRpcProxyGen.Create <rcvOkFromRA>(); proxy.Url = address; proxy.recieveOkFromRicartAgarwala(data.getpId(), clock.sendAction().ToString()); Console.WriteLine("Releasing the deferred request from " + iD + " from queue "); Console.WriteLine("OK message sent at " + DateTime.Now.ToString("HH:mm:ss:fff")); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("Could not send the message succesfully.. node denied connection"); Console.WriteLine(e.StackTrace); } } numberOfNodesResponded = 0; usingCR = false; }
public void join(String sendAddress) { // NetworkBean data = new NetworkBean(); joinNetw proxy = XmlRpcProxyGen.Create <joinNetw>(); proxy.Url = sendAddress; try { String recievedId = proxy.joinNetwork(data.getpId(), data.getAddress()); data.addValuesToDictionary(recievedId, sendAddress); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } }
public void resetDistributedStringVariable() { NetworkBean data = new NetworkBean(); ServerOperations serveroperation = new ServerOperations(); try { resetDistributedMasterStr proxy = XmlRpcProxyGen.Create <resetDistributedMasterStr>(); proxy.Url = data.getMasterAddress(); proxy.resetDistributedMasterString(data.getpId()); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("No response from the master node"); Console.WriteLine("Starting election to elect new master node ( Bully algorith)"); //remove node from network removeNode(data.getMasterPid()); data.getpId_Address().Remove(data.getMasterPid()); bullyAlgorithm(); // Console.WriteLine(e.StackTrace); } }
public void startDistributedCME() { try { Thread.Sleep(100); } catch (Exception e1) { // TODO Auto-generated catch block Console.WriteLine(e1.StackTrace); } CsClient client = new CsClient(); NetworkBean data = new NetworkBean(); //run the loop for 20 seconds Console.WriteLine("Distributed operation has started with Centralized Mutual Exclusion"); Console.WriteLine("*********************ID = " + data.getpId() + "*********************"); if (data.getpId().Equals(data.getMasterPid())) { Console.WriteLine("************************I am the Master Node********************"); } String response; String message = ""; ServerOperations serveroperaation = new ServerOperations(); var stopwatch = new Stopwatch(); stopwatch.Start(); while (stopwatch.Elapsed < TimeSpan.FromSeconds(20)) { //a) Wait a random amount of time (1-4 seconds) Random rnd = new Random(); int waitTime = data.getRnd(); Thread.Sleep(waitTime); //b) Read the string variable from the master node //c) Append some random english word to this string //d) Write the updated string to the master node // request for critical section to perform these steps try {//request //get response // if OK do operation //release message reqCMEDistributiveOperation proxy = XmlRpcProxyGen.Create <reqCMEDistributiveOperation>(); proxy.Url = data.getMasterAddress(); Console.WriteLine("Request Sent to Master for Distributed Operation at " + DateTime.Now.ToString("HH:mm:ss:fff")); Console.WriteLine("waiting for the response from Master"); response = proxy.requestCMEDistributiveOperation(data.getpId()); if (response.Equals("OK")) { Console.WriteLine("Recived OK message from Master at " + DateTime.Now.ToString("HH:mm:ss:fff")); distribRead proxyread = XmlRpcProxyGen.Create <distribRead>(); proxyread.Url = data.getMasterAddress(); Console.WriteLine("Reading the variable String from master node"); message = proxyread.distributedRead(data.getpId()); Console.WriteLine("The string is " + message); /* try * { * * Thread.Sleep(rnd.Next(1000, 3000)); * //System.out.println((long)(Math.random() * 4000+1000)); * } * catch (Exception e) * { * // TODO Auto-generated catch block * Console.WriteLine(e.StackTrace); * }*/ String randomWord = WordList.words[rnd.Next(0, 12)]; Console.WriteLine("Appending '" + randomWord + "' to the string " + message); distribWrite proxyWrite = XmlRpcProxyGen.Create <distribWrite>(); proxyWrite.Url = data.getMasterAddress(); proxyWrite.distributedWrite(data.getpId(), randomWord); data.getAddedWords().Add(randomWord); Console.WriteLine(randomWord + " appended to string"); Console.WriteLine("distributed Operatiion completed at " + DateTime.Now.ToString("HH:mm:ss:fff")); Console.WriteLine("Releasing the CR"); CMErelResource proxyRelease = XmlRpcProxyGen.Create <CMErelResource>(); proxyRelease.Url = data.getMasterAddress(); proxyRelease.CMEreleaseResource(data.getpId()); } } catch (Exception e) { // TODO Auto-generated catch block // Console.WriteLine(e.StackTrace); Console.WriteLine(e.Message); Console.WriteLine("Could not send the message succesfully.. node denied connection"); Console.WriteLine("No response from the master node"); Console.WriteLine("Starting election to elect new master node ( Bully algorith)"); client.removeNode(data.getMasterPid()); data.getpId_Address().Remove(data.getMasterPid()); client.bullyAlgorithm(); // Console.WriteLine(e.StackTrace); } } //c) After the process has ended all the nodes read the final string from the master node and //write it to the screen. Moreover they check if all the words they added to the string are //present in the final string. The result of this check is also written to the screen. // propagate that it is done with the process. Console.WriteLine("done with distributed process"); //loop foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { String sendAddress = entry.Value; String iD = entry.Key; try { distdProcessDone proxy = XmlRpcProxyGen.Create <distdProcessDone>(); proxy.Url = sendAddress; proxy.distributedProcessDone(iD); // Console.WriteLine("sending message I am New Master to :" + sendAddress); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("Could not send the message succesfully.. node denied connection"); // Console.WriteLine(e.StackTrace); Console.WriteLine("remove node from network"); client.removeNode(iD); data.getpId_Address().Remove(sendAddress); if (data.getMasterPid().Equals(iD)) { client.bullyAlgorithm(); } } } // int number = data.getNoOfNodesFinishedOperation(); while (data.getpId_Address().Count() > data.getNoOfNodesFinishedOperation()) { // number = data.getNoOfNodesFinishedOperation(); try { Thread.Sleep(100); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } } Console.WriteLine("out loop"); checkFinalString(); }
public void startDistributedRicartAndAgarwala() { try { Thread.Sleep(1); } catch (Exception e1) { // TODO Auto-generated catch block Console.WriteLine(e1.StackTrace); } CsClient client = new CsClient(); NetworkBean data = new NetworkBean(); Console.WriteLine("Distributed operation has started with Ricart and Agarwala algorithm"); Console.WriteLine("*********************ID = " + data.getpId() + "*********************"); if (data.getpId().Equals(data.getMasterPid())) { Console.WriteLine("****************************I am the Master Node*****************"); } Ricart_Agarwala RA = new Ricart_Agarwala(); String response; String message = ""; ServerOperations serveroperaation = new ServerOperations(); LamportClock clock = new LamportClock(); var stopwatch = new Stopwatch(); stopwatch.Start(); while (stopwatch.Elapsed < TimeSpan.FromSeconds(20)) { //a) Wait a random amount of time (1-4 seconds) Random rnd = new Random(); int waitTime = data.getRnd(); Thread.Sleep(waitTime); Console.WriteLine(waitTime); //b) Read the string variable from the master node //c) Append some random english word to this string //d) Write the updated string to the master node // request for critical section to perform these steps // RA.setMyTimeStamp((clock.getValue() + 1).ToString()); clock.tick(); clock.setRequestTimeStamp(clock.getValue() + 1); foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { String sendAddress = entry.Value; String iD = entry.Key; try { RA_sendReq proxy = XmlRpcProxyGen.Create <RA_sendReq>(); proxy.Url = sendAddress; RA.setRequestCR(true); clock.sendAction(); //("sending request message to "+entry.getKey()+"at local time :"+dateFormat.format(new Date())); Console.WriteLine("sending request message to " + iD + "at local time :" + DateTime.Now.ToString("HH:mm:ss:fff")); response = proxy.RA_sendRequest(data.getpId(), (clock.getValue()).ToString()); //Console.WriteLine("sending message I am New Master to :" + sendAddress); if (response.Contains("OK")) { String[] parts = response.Split('_'); clock.receiveAction(parts[1]); Console.WriteLine("Recieved OK message from " + iD + "at local time :" + DateTime.Now.ToString("HH:mm:ss:fff")); RA.setNumberOfNodesResponded(RA.getNumberOfNodesResponded() + 1); } else { String[] parts = response.Split('_'); clock.receiveAction(parts[1]); } } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("Exception from the node/nremove node from network"); client.removeNode(iD); data.getpId_Address().Remove(sendAddress); if (iD.Equals(data.getMasterPid())) { client.bullyAlgorithm(); } RA.setNumberOfNodesResponded(RA.getNumberOfNodesResponded() + 1); // Console.WriteLine(e.StackTrace); } }//for each Console.WriteLine("Number of OK messages recieved =" + RA.getNumberOfNodesResponded()); Console.WriteLine("Number of nodes in network =" + data.getpId_Address().Count()); if (RA.getNumberOfNodesResponded() != data.getpId_Address().Count()) { Console.WriteLine("Waiting for other nodes to send OK message..."); } // System.out.println(data.getpId_Address().size()+""+RA.getNumberOfNodesResponded()); int numberOfOkMsgs = RA.getNumberOfNodesResponded(); while (data.getpId_Address().Count() != RA.getNumberOfNodesResponded()) { numberOfOkMsgs = RA.getNumberOfNodesResponded(); //System.out.println(numberOfOkMsgs); try { Thread.Sleep(100); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } //keep waiting //break from the loop only when all the nodes have sent OK message } Console.WriteLine("Recieved OK message from all the nodes"); Console.WriteLine("Critical region in my control"); Console.WriteLine("Distributed process started at " + DateTime.Now.ToString("HH:mm:ss:fff")); RA.setRequestCR(false); RA.setUsingCR(true); try { distribRead proxy = XmlRpcProxyGen.Create <distribRead>(); proxy.Url = data.getMasterAddress(); Console.WriteLine("Reading the variable String from master node"); message = proxy.distributedRead(data.getpId()); Console.WriteLine("The string is " + message); String randomWord = WordList.words[rnd.Next(0, WordList.words.Length)]; Console.WriteLine("Appending '" + randomWord + "' to the string " + message); distribWrite proxyWrite = XmlRpcProxyGen.Create <distribWrite>(); proxyWrite.Url = data.getMasterAddress(); proxyWrite.distributedWrite(data.getpId(), randomWord); data.getAddedWords().Add(randomWord); /* try * { * * Thread.Sleep(rnd.Next(1000, 3000)); * //System.out.println((long)(Math.random() * 4000+1000)); * } * catch (Exception e) * { * // TODO Auto-generated catch block * Console.WriteLine(e.StackTrace); * }*/ } catch (Exception e) { // Console.WriteLine(e.StackTrace); Console.WriteLine("No response from the master node"); Console.WriteLine("Starting election to elect new master node ( Bully algorith)"); client.removeNode(data.getMasterAddress()); data.getpId_Address().Remove(data.getMasterPid()); client.bullyAlgorithm(); } Console.WriteLine("Distributed operation done at time " + DateTime.Now.ToString("HH:mm:ss:fff")); //release all the requests from queue Console.WriteLine("Exiting critical Section"); Console.WriteLine("Releasing all deffered requests"); RA.releaseDeferedRequests(); } //c) After the process has ended all the nodes read the final string from the master node and //write it to the screen. Moreover they check if all the words they added to the string are //present in the final string. The result of this check is also written to the screen. // propagate that it is done with the process. Console.WriteLine("done with distributed process"); clock.tick(); //loop foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { String sendAddress = entry.Value; String iD = entry.Key; try { distdProcessDone proxy = XmlRpcProxyGen.Create <distdProcessDone>(); proxy.Url = sendAddress; proxy.distributedProcessDone(iD); // Console.WriteLine("sending message I am New Master to :" + sendAddress); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("Could not send the message succesfully.. node denied connection"); // Console.WriteLine(e.StackTrace); } RA.setMyTimeStamp("0"); } //int number = data.getNoOfNodesFinishedOperation(); while (data.getpId_Address().Count() > data.getNoOfNodesFinishedOperation()) { // number = data.getNoOfNodesFinishedOperation(); try { Thread.Sleep(100); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } } Console.WriteLine("out loop"); checkFinalString(); }
static void Main(string[] args) { int choice = 1; Console.WriteLine("Please enter the port number to start listening----"); int port = Int32.Parse(Console.ReadLine()); ServerOperations serveroperation = new ServerOperations(); Thread thread = new Thread(() => serveroperation.start(port)); thread.Start(); CsClient csclient = new CsClient(); // csclient.testing1(); while (choice == 1) { Console.WriteLine("1.join \n2.view address list\n3.Trigger Bully election \n4.Reset central distributed variable"); Console.WriteLine("5.view master node\n6.Start Distributive Operation with Centralised mutual exclusion\n7.Start Distributive Operation with Ricart and Agarwala Algorithm\n8.signoff \n9.exit"); int read = Int32.Parse(Console.ReadLine()); switch (read) { case 1: { Console.WriteLine("Please enter the Address to join "); String sendAddress = Console.ReadLine(); try { csclient.join(sendAddress); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine(e.StackTrace); } break; } case 2: { NetworkBean data = new NetworkBean(); data.diaplayIdAddress(); Console.WriteLine("own address"); Console.WriteLine(data.getpId() + ":" + data.getAddress()); break; } case 3: { csclient.bullyAlgorithm(); break; } case 4: { csclient.resetDistributedStringVariable(); break; } case 5: { NetworkBean data = new NetworkBean(); data.diaplayIdAddress(); Console.WriteLine("master"); Console.WriteLine(data.getMasterPid() + ":" + data.getMasterAddress()); break; } case 6: { csclient.startDistributedOpeartion("CME"); break; } case 7: { csclient.startDistributedOpeartion("RA"); break; } case 8: { csclient.signOff(); break; } case 9: { csclient.signOff(); System.Environment.Exit(1); //choice = 0; break; } default: Console.WriteLine("Invalid input"); break; } } //csclient.testing(); Console.Read(); }
public void signOff() { NetworkBean data = new NetworkBean(); foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { String sendAddress = entry.Value; String iD = entry.Key; try { nodeSignoff proxy = XmlRpcProxyGen.Create <nodeSignoff>(); proxy.Url = sendAddress; proxy.nodeSignOff(data.getpId()); Console.WriteLine("sending message Signing off to :" + sendAddress); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("Could not send the message succesfully.. node denied connection"); removeNode(iD); // Console.WriteLine(e.StackTrace); } } if (data.getMasterPid().Equals(data.getpId())) { foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { addr = entry.Value; ide = entry.Key; } foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { try { // NetworkBean data = new NetworkBean(); ServerOperations serveroperaation = new ServerOperations(); setMasterAddrId proxy = XmlRpcProxyGen.Create <setMasterAddrId>(); proxy.Url = entry.Value; proxy.setMasterAddressId(ide, addr); } catch (Exception e) { // Console.WriteLine(e.StackTrace); } } String response = ""; foreach (KeyValuePair <string, string> entry in data.getpId_Address()) { String sendAddress = entry.Value; try { callBullyAfterSignoff proxy = XmlRpcProxyGen.Create <callBullyAfterSignoff>(); proxy.Url = sendAddress; response = proxy.callBullyAfterSignOff(data.getpId()); Console.WriteLine(data.getpId() + " Sends the bully election message to :" + sendAddress); Console.WriteLine("response is :" + response); } catch (Exception e) { // TODO Auto-generated catch block Console.WriteLine("no response"); // Console.WriteLine(e.StackTrace); } if (response.Length > 0) { break; } } } data.getpId_Address().Clear(); data.setMasterAddress(data.getAddress()); data.setMasterPid(data.getpId()); }