Example #1
0
        public Boolean recieveOkFromRicartAgarwala(String oK, String timestamp)
        {
            LamportClock clock = new LamportClock();

            clock.receiveAction(timestamp);
            NetworkBean     data = new NetworkBean();
            Ricart_Agarwala RA   = new Ricart_Agarwala();

            RA.setNumberOfNodesResponded(RA.getNumberOfNodesResponded() + 1);
            Console.WriteLine("recieved OK message from " + oK + " at Time " + DateTime.Now.ToString("HH:mm:ss:fff") + " \nnumber of ok messages = " + RA.getNumberOfNodesResponded());
            Console.WriteLine(" number of nodes = " + (data.getpId_Address().Count()));

            return(true);
        }
        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();
        }
Example #3
0
        public String RA_sendRequest(String iD, String timeStamp)
        {
            Ricart_Agarwala RA = new Ricart_Agarwala();

            return(RA.RA_RecieveRequest(iD, timeStamp));
        }
Example #4
0
        public void start(int port)
        {
            // for CookComputing.XmlRpcV2
            // RemotingConfiguration.Configure("SumAndDiff.exe.config", false);
            // for CookComputing.XmlRpc

            /*  try { RemotingConfiguration.Configure("C:\\Users\\UMAIR\\Documents\\Visual Studio 2015\\Projects\\Node1\\Node1\\App.config"); }
             * catch(Exception e)
             * {
             *    Console.WriteLine(e.Message);
             * }
             *
             * RemotingConfiguration.RegisterWellKnownServiceType(
             *  typeof(CsServer),
             *  "CsServer.rem",
             *  WellKnownObjectMode.Singleton);
             * Console.WriteLine("Press to shutdown");
             * Console.ReadLine();*/
            // system.("csc /r:system.web.dll /r:CookComputing.XmlRpcV2.dll /target:library CsServer.cs ");
            HttpListener listener = new HttpListener();
            string       ip       = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();

            String url = "http://" + ip + ":" + port + "/";

            Console.WriteLine(url);
            String      iD   = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            NetworkBean data = new NetworkBean();
            CentralisedMutualExclusion cme = new CentralisedMutualExclusion();
            Ricart_Agarwala            RA  = new Ricart_Agarwala();
            LamportClock clock             = new LamportClock();

            data.setAddress(url);
            //data.setpId(Long.parseLong(iD));
            data.setpId(iD);
            data.setPort(port);
            data.setMasterAddress(url);
            data.setMasterPid(iD);
            cme.setCRInUse(false);
            RA.setRequestCR(false);
            RA.setUsingCR(false);
            RA.setNumberOfNodesResponded(0);
            RA.setMyTimeStamp("0");
            data.setDistributedStringVariable("");
            data.setStartCME(false);
            data.setStartRicartAgarwala(false);
            data.setMessage("");
            clock.setC(1);
            data.setNoOfNodesFinishedOperation(0);
            data.setRnd();
            //Console.WriteLine(url+iD);
            listener.Prefixes.Add(url);
            try { listener.Start(); }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            while (true)
            {
                HttpListenerContext context = listener.GetContext();


                XmlRpcListenerService svc = new CsServer();

                svc.ProcessRequest(context);
                Thread.Sleep(1);
            }

            Console.WriteLine("server started");
        }