private void register(string _input)
        {
            token.WaitOne();
            string[] _tokens = _input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            if (_tokens.Count <string>() < 2)
            {
                Console.Out.WriteLine("Illegal arguments");
                Console.Out.WriteLine("register [ip]: Register the host with the given IP address as remote host.\r\n");
                return;
            }
            string _ip = _tokens[1];

            Client newClient = new Client(_ip);

            try
            {
                newClient.makeFunction("Say", new string[] { "Peer of address: " + getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() + " registered you" });
            }
            catch
            {
                Console.Out.WriteLine("Wrong address given. Couldn't connect.");
                return;
            }

            if (EventManager.singleMachineDebug)
            {
                //For debugging on a single machine
                newClient.makeFunction("AddNewPeer", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
            }
            else
            {
                newClient.makeFunction("AddNewPeer", new string[] { getLocalIP() });
            }
        }
        /// <summary>
        /// Method delete the given node from the list of remote hosts
        /// </summary>
        /// <param name="_input">The input string</param>
        private void drop(string _input)
        {
            string[] _tokens = _input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            if (_tokens.Count <string>() < 2)
            {
                Console.Out.WriteLine("Illegal arguments");
                Console.Out.WriteLine("drop [ip]: Remove the host with the given IP address from the list of remote hosts. \r\n");
                return;
            }
            string _ip = _tokens[1];

            if (!Client.clientsMap.Keys.Contains(_ip))
            {
                Console.Out.WriteLine("Peer under given address is not connected with you.");
                return;
            }

            Client.clientsMap[_ip].makeFunction("DropAll", null);
            Client tempClient = Client.clientsMap[_ip];

            _drop(_ip);
            tempClient.makeFunction("SetNext", new string[] { "" });
            tempClient.makeFunction("SetPrev", new string[] { "" });
            tempClient.makeFunction("SetToken", null);
            foreach (Client c in Client.clientsMap.Values)
            {
                if (c.address != _ip)
                {
                    c.makeFunction("Drop", new string[] { _ip });
                }
            }
        }
        public int _addNewPeer(string newPeerAddress)
        {
            waitsForToken = true;
            token.WaitOne();
            waitsForToken = false;

            Client newPeer = new Client(newPeerAddress);

            foreach (string address in Client.clientsMap.Keys)
            {
                newPeer.makeFunction("Register", new string[] { address });
                Client.clientsMap[address].makeFunction("Register", new string[] { newPeerAddress });
            }
            Client.clientsMap.Add(newPeerAddress, newPeer);
            if (EventManager.singleMachineDebug)
            {
                //For testing on a single machine
                newPeer.makeFunction("Register", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
                newPeer.makeFunction("SetPrev", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
            }
            else
            {
                newPeer.makeFunction("Register", new string[] { getLocalIP() });
                newPeer.makeFunction("SetPrev", new string[] { getLocalIP() });
            }

            if (next != null)
            {
                next.makeFunction("SetPrev", new string[] { newPeerAddress });
                newPeer.makeFunction("SetNext", new string[] { next.address });
            }
            else
            {
                if (EventManager.singleMachineDebug)
                {
                    //For debugging on a single machine
                    newPeer.makeFunction("SetNext", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
                }
                else
                {
                    newPeer.makeFunction("SetNext", new string[] { getLocalIP() });
                }
                prev = newPeer;
            }
            next = newPeer;

            DirectoryInfo di = new DirectoryInfo(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "..\\..\\..\\events"));

            foreach (FileInfo fi in di.GetFiles())
            {
                StreamReader _sr = new StreamReader(File.Open(fi.FullName, FileMode.Open, FileAccess.Read));
                string       _s  = _sr.ReadLine();
                _sr.Close();
                string[] _tokens = _s.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                newPeer.makeFunction("Add", _tokens);
            }

            forwardToken();
            return(0);
        }
 /// <summary>
 /// Used to set the token on the current peer
 /// </summary>
 public void setToken()
 {
     if (waitsForToken)
     {
         token.Release();
     }
     else if (next != null)
     {
         next.makeFunction("SetToken", null);
     }
     else
     {
         token.Release();
     }
 }
        private void register(string _input)
        {
            token.WaitOne();
            string[] _tokens = _input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            if (_tokens.Count<string>() < 2)
            {
                Console.Out.WriteLine("Illegal arguments");
                Console.Out.WriteLine("register [ip]: Register the host with the given IP address as remote host.\r\n");
                return;
            }
            string _ip = _tokens[1];

            Client newClient = new Client(_ip);

            try
            {
                newClient.makeFunction("Say", new string[] { "Peer of address: " + getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() + " registered you" });
            }
            catch
            {
                Console.Out.WriteLine("Wrong address given. Couldn't connect.");
                return;
            }

            if (EventManager.singleMachineDebug)
                //For debugging on a single machine
                newClient.makeFunction("AddNewPeer", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
            else
                newClient.makeFunction("AddNewPeer", new string[] { getLocalIP() });
        }
        public int _addNewPeer(string newPeerAddress)
        {
            waitsForToken = true;
            token.WaitOne();
            waitsForToken = false;

            Client newPeer = new Client(newPeerAddress);
            foreach (string address in Client.clientsMap.Keys)
            {
                newPeer.makeFunction("Register", new string[] { address });
                Client.clientsMap[address].makeFunction("Register", new string[] { newPeerAddress });
            }
            Client.clientsMap.Add(newPeerAddress, newPeer);
            if (EventManager.singleMachineDebug)
            {
                //For testing on a single machine
                newPeer.makeFunction("Register", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
                newPeer.makeFunction("SetPrev", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
            }
            else
            {
                newPeer.makeFunction("Register", new string[] { getLocalIP() });
                newPeer.makeFunction("SetPrev", new string[] { getLocalIP() });
            }

            if (next != null)
            {
                next.makeFunction("SetPrev", new string[] { newPeerAddress });
                newPeer.makeFunction("SetNext", new string[] { next.address });
            }
            else
            {
                if (EventManager.singleMachineDebug)
                    //For debugging on a single machine
                    newPeer.makeFunction("SetNext", new string[] { getLocalIP() + ":8000/xmlrpc" + EventManager.servNum.ToString() });
                else
                    newPeer.makeFunction("SetNext", new string[] { getLocalIP() });
                prev = newPeer;
            }
            next = newPeer;

            DirectoryInfo di = new DirectoryInfo(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "..\\..\\..\\events"));
            foreach (FileInfo fi in di.GetFiles())
            {
                StreamReader _sr = new StreamReader(File.Open(fi.FullName, FileMode.Open, FileAccess.Read));
                string _s = _sr.ReadLine();
                _sr.Close();
                string[] _tokens = _s.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                newPeer.makeFunction("Add", _tokens);
            }

            forwardToken();
            return 0;
        }