//this will call by local to send add request to others and will wait to get back all permissions //and watch the currentAddRequest at the top of the queue in a while loop //Actually in a while loop it will check the list of its tail and //if all of them are ok it will check the top of the queue //So in this way client thread will become lock here until start adding process public void sendAddPermissionRequest(HpnXmlRpcClient hpnXmlRpcClient)//client agent for sending message to other hosts { Console.WriteLine("* Mutual Exclusion Algorithm <<R&A>> has started."); currentAddRequest = new RequestObject(); //Make a new request with [LC = LC + 1]{means increased clock before send request} for getting add permission addRequestsQueue.add(currentAddRequest); //put the request in your own queue hpnXmlRpcClient.sendAddMutualExclusionRequest(currentAddRequest); //send the new add request to all other hosts Console.WriteLine("* Now we will wait for OK responses from other hosts!"); bool flag = true; while (flag) { lock (this) { flag = currentAddRequest.isWaiting(); } }//sit back and wait for OK from all processes Console.WriteLine("* Now we will have this permission to go to the critical section."); //After this function you can go to the critical section }
public void sendReleaseMessage(HpnXmlRpcClient hpnXmlRpcClient)//client agent for sending message to other hosts { Console.WriteLine("* Now we have come out from the critical section."); //After the critical section this function will call by this machine //Upon exiting the critical section, //remove request from the queue and Console.WriteLine("* Sending OK message to all requests that are available in the Priority Queue has started."); RequestObject obj = addRequestsQueue.remove(); while (obj != null) { //send release message to every process that are remaining in the queue //send OK to all processes in queue and empty queue if (obj.isExternalRequester()) { hpnXmlRpcClient.sendAddCriticalSectionReleased(obj); //send the new add request released } obj = addRequestsQueue.remove(); } currentAddRequest = null; Console.WriteLine("* Mutual Exclusion Algorithm has finished."); }
static void Main(string[] args) { calendar = new Calendar(filepath); hostsList = new HostsList(); try { Console.WriteLine(" <<< Welcome to HPN Calendar System >>> "); Console.WriteLine(" <<< Version TUMS 1.0 >>> "); Console.WriteLine("____________________________________________________"); Console.WriteLine("In which port number you want to run this host?"); Console.WriteLine("The port number must be between 1025 & 65535."); Console.WriteLine("Default port number is {0}. Enter 0 to use default.", DefaultPort.portNumber); Console.WriteLine("Enter -1 to exit program."); Console.Write("Please enter the port number : "); int port = Reader.nextInt(); while ((port < 1025 || port > 65535) && port > 0) { Console.WriteLine("The port number that you have entered is not valid."); Console.Write("Please enter the port number : "); port = Reader.nextInt(); } if (port < 0) { Console.WriteLine("The HPN Calendar System has stoped by user."); Environment.Exit(0); } else if (port == 0) { port = DefaultPort.portNumber; } Console.WriteLine("The port number has assigned to : " + port); server = new HpnXmlRpcServer(port); server.Add("Calendar", calendar); server.Add("CalendarNetwork", hostsList); server.startServing(); //Start the xml-rpc server for test. Console.WriteLine("The XML-RPC server has checked : Ok."); Console.WriteLine("The host has run on this address : http://" + LocalNet.IpAddress + ":" + port + "/"); //the local host must be add to the host list as a host. //but not here, when we make the client instance so we will pass the port number //and then in the hpn.cs.xml.client.HpnXmlRpcClient class in its contractor will add the local host and its port to the host list while (true) { Console.Write("Do you want to create a new Calendar Network? [Y/N] : "); char response = Reader.nextChar(); if (response == 'n' || response == 'N') { server.signOff(); //Sign off the server to show the joining menu Console.WriteLine("The host is working in its offline mode, to connect to an existing network please use the following command list."); break; } else if (response == 'y' || response == 'Y') { break; } else { Console.WriteLine("The character that you have entered ['" + response + "'] is not correct. You can just enter a character from the set {'n','N','y','Y'}."); } } //The port of this machine must be send to register the local host as the first host in the host list //And the calendar must be send to be able to get the list of appointments in offline mode because in offline mode //the local server is signed off and can't response to the local client requests. client = HpnXmlRpcClient.getHpnXmlRpcClient(port, calendar); while (true) { client.controlPanel(); } } catch (Exception e) { Console.WriteLine(e.Message); } //End of try }//End of main