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());
            }
        }
예제 #2
0
        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());
        }
        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);
            }
        }
예제 #4
0
        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());
        }