Exemplo n.º 1
0
        public void AddContact(string nickname, string ipAddress)
        {
            Contact contact = new Contact(ipAddress);
            contact.Nick = nickname;

            if (this.FindNickname(nickname) != null)
                contacts.Add(nickname, contact);
            else
            {
                contacts.Remove(nickname);
                contacts.Add(nickname, contact);
            }
        }
Exemplo n.º 2
0
        private bool NickManage()
        {
            System.Console.WriteLine("Enter nick to look: ");
             			string nick = System.Console.ReadLine();

             			string ip = life.FindIpAddress(nick);

             			if (ip == null)
             			{
             				System.Console.WriteLine("Contact not found");
             				System.Console.WriteLine("Add {0} as a new contact? [y/N]",nick);
             				if (System.Console.ReadLine() == "y")
             				{
                    System.Console.Write("Enter the IP of {0}:", nick);
                    ip = System.Console.ReadLine();

                    life.AddContact(nick,ip);
                }
                else return false;
            }

            contact = life.SearchContact(nick);

            return true;
        }
Exemplo n.º 3
0
        private void listNicks()
        {
            System.Console.WriteLine("==============================================");
            System.Console.WriteLine("====  Your  Contacts :");

            IDictionaryEnumerator enumerator;

            try
            {
                enumerator = life.Contacts.GetEnumerator();
            }
            catch (Exception ex)
            {
                return;
            }

            while (enumerator.MoveNext())
            {
                System.Console.Write("====    Nick: ");
                System.Console.Write(enumerator.Key);
                System.Console.Write("    IP: ");
                System.Console.Write(((Contact)enumerator.Value).IP);
                System.Console.Write("    Status: ");
                Contact contact = new Contact(enumerator.Value.ToString());

                System.Console.WriteLine(PeerNetwork.RetrieveStatus(contact));
            }

            System.Console.WriteLine("================= End List ===================");
            System.Console.WriteLine("==============================================");
        }
Exemplo n.º 4
0
        public static Hashtable GetRemoteContacts(string ip)
        {
            TcpClient cliente;
            int port = 2006;
            Hashtable contact = new Hashtable();

                cliente = new TcpClient();
                cliente.SendTimeout = 3000;
                cliente.ReceiveTimeout = 3000;
                cliente.Connect(ip, port);
                NetworkStream  output = cliente.GetStream();

                BinaryReader reader = new BinaryReader( output );

                string contactsReturn = reader.ReadString();

                //System.Console.WriteLine(contactsReturn);

                string[] parts = contactsReturn.Split(new Char[]  {';'});

                for(int i = 0;i < (parts.Length-1); i += 4)
                {
                    Contact newContact = new Contact();
                    newContact.Nick = parts[i];
                    newContact.Name = parts[i+1];
                    newContact.IP = parts[i+2];
                    newContact.Trust = Convert.ToInt32(parts[i+3]);
                    contact.Add(parts[i],newContact);
                }

                cliente.Close();

            return contact;
        }
Exemplo n.º 5
0
        public void SearchContactsStatus()
        {
            while(true)
            {
                IDictionaryEnumerator enumerator = life.Contacts.GetEnumerator();

                while (enumerator.MoveNext())
                {

                    contact = (Contact) enumerator.Value;
                    string status = contact.Status;

                    TcpClient cliente;

                    Thread thrCliente = new Thread(new ThreadStart(Connect));
                    thrCliente.Start();
                    Thread.Sleep(1500);
                    thrCliente.Abort();

                    if(status != contact.Status)
                    {
                        Console.WriteLine("\nContact " + enumerator.Key.ToString() + " is now " + contact.Status);
                    }
                }
                Thread.Sleep(3000);
            }
        }
Exemplo n.º 6
0
        public static String RetrieveStatus(Contact contact)
        {
            TcpClient cliente;

            PeerNetwork com = new PeerNetwork(null);
            com.contact = contact;
            Thread thrCliente = new Thread(new ThreadStart(com.Connect));
            thrCliente.Start();
            Thread.Sleep(1500);
            thrCliente.Abort();
            return (com.contact.Name + " " + com.contact.Status);
        }