コード例 #1
0
        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;
        }
コード例 #2
0
        //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 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();
        }