static void Main(string[] args)
        {
            var proxy = XmlRpcProxyGen.Create<IConnectionProxy>();
            var port = NetworkHelper.FindFreePort();
            var ipAddress = NetworkHelper.FindIp().ToString();
            var nodeInfo = new NodeInfo(ipAddress, port);
            var electAlg = new Bully(nodeInfo, proxy);
            var ricartSyncAlgorithm = new RicartSyncAlgorithm(nodeInfo, proxy);
            var centralizedSyncAlgorithm = new CentralizedSyncAlgorithm(nodeInfo, proxy);

            var client = new Node(nodeInfo, proxy, electAlg, ricartSyncAlgorithm.Client, centralizedSyncAlgorithm.Client);
            var server = new Server(port, client, ricartSyncAlgorithm.Server, centralizedSyncAlgorithm.Server);
            var host = new Host(client, server);

            Console.WriteLine("Client IP: " + client.NodeInfo.GetIpAndPort());
            Console.WriteLine("Client ID: " + client.NodeInfo.Id);

            while (true)
            {
                Console.Write("["+client.NodeInfo.GetIpAndPort()+"] ");
                Console.WriteLine("The service is ready, please write commands: ");
                var command = Console.ReadLine();
                try
                {
                    client.ProcessCommand(command);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }
        }
 public int Compare(NodeInfo host2)
 {
     if (_id < host2.Id)
         return -1;
     if (_id == host2.Id)
         return 0;
     return 1;
 }
 /// <summary>
 /// constructor
 /// </summary>
 public Node(NodeInfo nodeInfo, IConnectionProxy proxy, IElectionAlgorithm electionAlgorithm
     , IRicartSyncAlgorithmClient ricartSyncAlgorithmClient, ICentralizedSyncAlgorithmClient centralizedSyncAlgorithmClient)
 {
     _proxy = proxy;
     _nodeInfo = nodeInfo;
     _electionAlgorithm = electionAlgorithm;
     _ricartSyncAlgClient = ricartSyncAlgorithmClient;
     _centralizedSyncAlgClient = centralizedSyncAlgorithmClient;
 }
        /// <summary>
        /// Constructor
        /// </summary>
        public CentralizedSyncAlgorithm(NodeInfo nodeInfo, IConnectionProxy proxy)
        {
            // State and Queue is only for Master Node
            State = AccessState.Released;
            Proxy = proxy;
            LocalNodeInfo = nodeInfo;

            Client = new CentralizedSyncAlgorithmClient(this);
            Server = new CentralizedSyncAlgorithmServer(this);
        }
 /// <summary>
 /// Send election message to target host
 /// </summary>
 public void SendElectionMsg(NodeInfo target, IConnectionProxy proxy)
 {
     //var proxy1 = XmlRpcProxyGen.Create<IConnectionProxy>();
     proxy.Url = target.GetFullUrl();
     if (proxy.ReceiveElectionMsg(_node.Id.ToString()))
     {
         // when target response
         _isThisNodeLost.Set();  // declare this node is lost
     }
 }
        /// <summary>
        /// constructor
        /// </summary>
        public RicartSyncAlgorithm(NodeInfo nodeInfo, IConnectionProxy proxy)
        {
            State = AccessState.Released;
            LocalId = nodeInfo.Id;
            Proxy = proxy;
            LocalNodeInfo = nodeInfo;
            _clock = new ExtendedLamportClock(LocalId);

            Client = new RicartSyncAlgorithmClient(this);
            Server = new RicartSyncAlgorithmServer(this);
        }
        /// <summary>
        /// Send Sync Req to Master node
        /// </summary>
        public void SendSyncRequestToMaster_CT(NodeInfo masterNode)
        {
            IsAllowed.Reset();
            _module.Proxy.Url = masterNode.GetFullUrl();
            // to evade time out - using thread and wait for an acceptance trigger.
            var send = new Thread(
                        () => SendSyncMsg(_module.Proxy, masterNode));
            send.Start();

            // wait until the master allow to accept to the resource
            IsAllowed.WaitOne();
        }
        public virtual void Init()
        {
            //if (initialized) Assert.Fail("fixture setup called multiple times");
            if (initialized) return;

            initialized = true;

            var hostLookup = new Dictionary<String, Host>();
            var mockProxy = new ConnectionProxyMock(hostLookup);

            for (var i = 0; i < HostNumber; i++)
            {
                //var proxy = XmlRpcProxyGen.Create<IConnectionProxy>();

                var port = NetworkHelper.FindFreePort();
                var ipAddress = NetworkHelper.FindIp().ToString();
                var nodeInfo = new NodeInfo(ipAddress, port);
                var electAlg = new Bully(nodeInfo, mockProxy);
                var ricartSyncAlgorithm = new RicartSyncAlgorithm(nodeInfo, mockProxy);
                var centralizedSyncAlgorithm = new CentralizedSyncAlgorithm(nodeInfo, mockProxy);

                var client = new Node(nodeInfo, mockProxy, electAlg, ricartSyncAlgorithm.Client, centralizedSyncAlgorithm.Client);
                var server = new Server(port, client, ricartSyncAlgorithm.Server, centralizedSyncAlgorithm.Server);
                var host = new Host(client, server);
                hostLookup.Add(nodeInfo.GetFullUrl(), host);

                Hosts.Add(host);
            }

            //var proxy2 = XmlRpcProxyGen.Create<IConnectionProxy>();
            var port2 = NetworkHelper.FindFreePort();
            var ipAddress2 = "255.255.255.255";
            var nodeInfo2 = new NodeInfo(ipAddress2, port2);
            var electAlg2 = new Bully(nodeInfo2, mockProxy);
            var ricartSyncAlgorithm2 = new RicartSyncAlgorithm(nodeInfo2, mockProxy);
            var centralizedSyncAlgorithm2 = new CentralizedSyncAlgorithm(nodeInfo2, mockProxy);

            var masterclient = new Node(nodeInfo2, mockProxy, electAlg2, ricartSyncAlgorithm2.Client, centralizedSyncAlgorithm2.Client);
            MasterHost = new Host(masterclient,
                new Server(port2, masterclient, ricartSyncAlgorithm2.Server, centralizedSyncAlgorithm2.Server));
            hostLookup.Add(nodeInfo2.GetFullUrl(), MasterHost);
        }
        /// <summary>
        /// Start election of master node
        /// </summary>
        public void ElectMasterNode()
        {
            if (_hostLookup.Count == 0)
            {
                Console.WriteLine("This client is not in network");
                return;
            }

            // master node reset
            _masterNode = null;

            _electionAlgorithm.StartBullyElection(_hostLookup);
        }
        /// <summary>
        /// Add new host to dictionary
        /// </summary>
        public NodeInfo AddNewHost(string ipAndPort)
        {
            var nodeInfo = new NodeInfo(ipAndPort);
            _hostLookup.Add(ipAndPort, new NodeInfo(ipAndPort));

            return nodeInfo;
        }
        private void ResetVariables()
        {
            _appendedStringSet = new HashSet<string>();
            _masterNode = null;
            Resource = string.Empty;

            _electionAlgorithm.BullyReset();
            _centralizedSyncAlgClient.CentralizedReset();
            _ricartSyncAlgClient.RicartReset();
        }
        /// <summary>
        /// Setting master node. And starting algorithm if needed
        /// </summary>
        public void SetMasterNode(String ipAndPortMaster)
        {
            _masterNode = new NodeInfo(ipAndPortMaster);

            Console.WriteLine("## Master[" + _masterNode.GetIpAndPort() + "] is elected.");

            _electionAlgorithm.FinishElection();
            _isElectionFinished.Set();
        }
        /// <summary>
        /// Command: Join
        /// connect to other network
        /// </summary>
        public void Join(string ipAndPort)
        {
            Console.WriteLine("# Join operation with address " + ipAndPort);
            var toJoinInfo = new NodeInfo(ipAndPort);
            if (toJoinInfo.IsSameHost(_nodeInfo))
            {
                throw new ArgumentException("Cannot join yourself");
            }

            _proxy.Url = toJoinInfo.GetFullUrl();

            // Receive list from receiver
            var listsOfHosts = _proxy.GetHosts(_nodeInfo.GetIpAndPort());

            // Add list to dictionary and send the information of own node to all other hosts
            foreach (var host in listsOfHosts)
            {
                var toSendHost = AddNewHost(host.ToString());
                _proxy.Url = toSendHost.GetFullUrl();
                _proxy.AddNewHost(_nodeInfo.GetIpAndPort());
            }

            // Add ipAndPort of receiver
            AddNewHost(ipAndPort);

            Console.WriteLine("# Network connection success.");
        }
 public bool IsSameHost(NodeInfo host)
 {
     return Id == host.Id;
 }
 /// <summary>
 /// When this node is the winner(master), it send final election msg to other machines
 /// </summary>
 public void SendElectionFinalMsg(NodeInfo target)
 {
     //var proxy1 = XmlRpcProxyGen.Create<IConnectionProxy>();
     _proxy.Url = target.GetFullUrl();
     _proxy.SetMasterNode(_node.GetIpAndPort());
 }
 public Bully(NodeInfo node, IConnectionProxy proxy)
 {
     _node = node;
     _proxy = proxy;
     _isExecuted = false;
 }
        /// <summary>
        /// Send Req message to a node
        /// </summary>
        private void SendSyncMsg(IConnectionProxy proxy1, NodeInfo toNode)
        {
            var logicClockTs = _module.Clock.SendEventHandle();

            _module.AddToAcceptList(toNode.GetIpAndPort());
            var urlToSend = toNode.GetFullUrl();

            //LogHelper.WriteStatus("CLIENT: SEND REQ FROM: [" + _module1.LocalNodeInfo.GetIpAndPort() + "] TO: [" +
            //                      proxy1.Url + "]");
            //.WriteLine("CLIENT: SEND REQ FROM: " + _module1.LocalNodeInfo.GetIpAndPort() + " TO: " + _module1.Proxy1.Url);
            lock (Shared.SendLock)
            {
                proxy1.Url = urlToSend;
                proxy1.GetSyncRequest_RA(logicClockTs.ToString(), _module.LocalNodeInfo.Id.ToString(), _module.LocalNodeInfo.GetIpAndPort());
            }
        }
 /// <summary>
 /// Send Sync Req to Master node
 /// </summary>
 private void SendSyncMsg(IConnectionProxy proxy, NodeInfo toNode)
 {
     proxy.Url = toNode.GetFullUrl();
     _module.Proxy.GetSyncRequest_CT(_module.LocalNodeInfo.Id.ToString(), _module.LocalNodeInfo.GetIpAndPort());
 }
 public bool IsSameHost(NodeInfo host)
 {
     return(Id == host.Id);
 }