/// <summary>
 /// Search for something on all hubs
 /// this will send a search request to every
 /// connected hub
 /// </summary>
 /// <param name="sp">all search parameters in one single parameter</param>
 public void Search(Hub.SearchParameters sp)
 {
     search_results.SearchTerm = sp.search_string;
     lock (connected_hubs_lock)
     {
         foreach (Hub hub in connected_hubs)
         {//search on all connected hubs
             // add filter hubs possibilities
             hub.Search(sp);
         }
     }
 }
 /// <summary>
 /// Read values of a single hub
 /// </summary>
 /// <param name="node">xml node to read from</param>
 /// <returns>reserved</returns>
 private bool ReadHub(XmlNode node)
 {
     //Console.WriteLine("Reading Hub information");
     Hub hub = new Hub();
     foreach (XmlAttribute attr in node.Attributes)
     {
         try
         {
             //Console.WriteLine("attr:" + attr.Name + " - " + attr.Value);
             if (attr.Name.Equals("Name")) hub.Name = attr.Value;
             if (attr.Name.Equals("Address")) hub.Address = attr.Value;
             if (attr.Name.Equals("Description")) hub.Description = attr.Value;
             if (attr.Name.Equals("Country")) hub.Country = attr.Value;
             if (attr.Name.Equals("IP")) hub.IP = attr.Value;
             if (attr.Name.Equals("Users") && !string.IsNullOrEmpty(attr.Value)) hub.Users = long.Parse(attr.Value);
             if (attr.Name.Equals("Shared") && !string.IsNullOrEmpty(attr.Value)) hub.Shared = long.Parse(attr.Value);
             if (attr.Name.Equals("Minshare") && !string.IsNullOrEmpty(attr.Value)) hub.MinShare = long.Parse(attr.Value);
             if (attr.Name.Equals("Minslots") && !string.IsNullOrEmpty(attr.Value)) hub.MinSlots = int.Parse(attr.Value);
             if (attr.Name.Equals("Maxhubs") && !string.IsNullOrEmpty(attr.Value)) hub.MaxHubs = int.Parse(attr.Value);
             if (attr.Name.Equals("Maxusers") && !string.IsNullOrEmpty(attr.Value)) hub.MaxUsers = long.Parse(attr.Value);
             if (attr.Name.Equals("Port") && !string.IsNullOrEmpty(attr.Value)) hub.Port = int.Parse(attr.Value);
         }
         catch (Exception e)
         {
             Console.WriteLine("Exception reading Hub-Values: "+e.Message);
             return (false);
         }
     }
     bool unique = true;
     foreach (Hub search in hubs)
     {
         if (search.Address == hub.Address)
         {
             //Console.WriteLine("duplicate hub entry found: "+hub.Name);
             unique = false;
         }
     }
     if(unique)
         hubs.Add(hub);
     return (true);
 }
 /// <summary>
 /// Start getting a file list from a user
 /// </summary>
 /// <param name="hub">the hub which the user is connected to</param>
 /// <param name="username">the user from which the file list should be downloaded from</param>
 public void GetFileList(Hub hub, string username)
 {
     download_queue.AddFileList(hub, username);
     hub.SendConnectToMe(username); //signal download to hub to start it
 }
 /// <summary>
 /// Search for something on all hubs
 /// this will send a search request to every
 /// connected hub
 /// </summary>
 /// <param name="search_string">the term you want to search for</param>
 /// <param name="size_restricted">TRUE if you want to restrict your search to a specific size range</param>
 /// <param name="is_max_size">TRUE if you want to size restrict your search to a max size</param>
 /// <param name="size">the size you want to use in your size resstriction,only used if size_restricted is set to TRUE</param>
 /// <param name="file_type">the specific filetype to search for ,default will be ANY</param>
 public void Search(string search_string, bool size_restricted, bool is_max_size, int size, Hub.SearchFileType file_type)
 {
     search_results.SearchTerm = search_string;
     lock (connected_hubs_lock)
     {
         foreach (Hub hub in connected_hubs)
         {//search on all connected hubs
             // add filter hubs possibilities
             hub.Search(search_string, size_restricted, is_max_size, size, file_type);
         }
     }
 }
        /// <summary>
        /// Connect to a hub
        /// this will initialize a hub with 
        /// some client values like our nickname etc
        /// add some event handlers
        /// and start connecting to it
        /// </summary>
        /// <param name="me">the hub you want to connect to</param>
        public void ConnectHub(Hub me)
        {
            me.Nick = nick;
            if (!string.IsNullOrEmpty(local_peer.ExternalIP)) me.MyIP = local_peer.ExternalIP;
            else me.MyIP = local_peer.IP;
            me.MyTcpPort = local_peer.TcpPort;
            me.MyUdpPort = local_peer.UdpPort;
            me.MyEmail = email;
            me.MyDescription = description;
            me.MyVersion = version;
            me.MyTagVersion = tag_version;
            me.MyShareSize = share_size;
            me.MyConnectionMode = connection_mode;
            me.MyConnectionSpeed = connection_speed;
            me.MyName = name;

            if (!me.IsGrabbedByClient)
            {
                me.SearchReceived += delegate(Hub search_hub, Hub.SearchParameters search)
                {
                    if (search.HasTTH)
                    {
                        Sharing.SharingEntry entry = shares.GetShareByTTH(search.tth);
                        if (entry != null)
                        {
                            if (search.mode == Hub.ConnectionMode.Passive)
                                search_hub.SearchReply(entry.Filename,entry.Filesize, search);
                            else local_peer.SearchReply(entry.Filename,entry.Filesize,search_hub, search);
                        }
                    }
                    else
                    {
                        //TODO add old fashioned search here
                    }

                };
                me.SearchResultReceived += delegate(Hub search_result_hub, SearchResults.SearchResult result)
                    {
                        InterpretReceivedSearchResult(result);
                    };
                me.PasswordRequested += delegate(Hub password_requested)
                {
                    //TODO add a password for hubs db
                    // and first check that db before and send a found password
                    //automagically and silent
                    if (HubPasswordRequested != null)
                        return(HubPasswordRequested(password_requested));
                    return (null);
                };
                me.MainChatLineReceived += delegate(Hub main_chat_hub, Hub.ChatLine main_chat_line)
                {
                    if (HubMainChatReceived != null)
                        HubMainChatReceived(main_chat_hub, main_chat_line);
                };
                me.PrivateChatLineReceived += delegate(Hub private_chat_hub, Hub.ChatLine private_chat_line)
                {
                    if (HubPrivateChatReceived != null)
                        HubPrivateChatReceived(private_chat_hub, private_chat_line);
                };
                me.MoveForced += delegate(Hub src_hub, Hub dst_hub)
                {
                    if (HubMoveForced != null)
                        HubMoveForced(src_hub, dst_hub);
                };
                me.ConnectToMeReceived += delegate(Hub hub, Peer connect_to_me_client)
                {
                    //free slots check maybe needed
                    SetupPeerEventHandler(connect_to_me_client);
                    connect_to_me_client.Connected += delegate(Peer connect_to_me_connected_client)
                    {
                        if (PeerConnected != null)
                            PeerConnected(connect_to_me_connected_client);
                        connect_to_me_connected_client.StartHandShake();
                        lock (peers_lock)
                        {
                            peers.Add(connect_to_me_connected_client);
                        }
                    };
                    connect_to_me_client.Connect();

                };
                me.Disconnected += delegate(Hub hub)
                {
                    UpdateSourcesByHub(hub, false);
                    lock (connected_hubs_lock)
                    {
                        if (connected_hubs.Contains(hub))
                            connected_hubs.Remove(hub);
                    }
                    if (HubDisconnected != null)
                        HubDisconnected(hub);
                };
                me.Connected += delegate(Hub hub)
                {
                    lock (connected_hubs_lock)
                    {
                        if (!connected_hubs.Contains(hub))
                            connected_hubs.Add(hub);
                    }
                    if (HubConnected != null)
                        HubConnected(hub);
                };
                me.UnableToConnect += delegate(Hub hub)
                {
                    UpdateSourcesByHub(hub, false);
                    /*lock (connected_hubs_lock) TODO check if commenting this out hurts our code---> :-)
                    {
                        if (connected_hubs.Contains(hub))
                            connected_hubs.Remove(hub);
                    }*/
                    if (HubUnableToConnect != null)
                        HubUnableToConnect(hub);
                };
                me.LoggedIn += delegate(Hub hub)
                {
                    if (HubLoggedIn != null)
                        HubLoggedIn(hub);
                };
                me.UserJoined += delegate(Hub hub, string username)
                {
                    UpdateSourcesByUsername(username, hub, true);
                    if (HubUserJoined != null)
                        HubUserJoined(hub, username);
                };
                me.UserQuit += delegate(Hub hub, string username)
                {
                    UpdateSourcesByUsername(username, hub, false);
                    if (HubUserQuit != null)
                        HubUserQuit(hub, username);
                };

                me.IsGrabbedByClient = true;
            }
            me.Connect();
        }
 /// <summary>
 /// Disconnect a hub
 /// this will also remove all event handlers from that hub
 /// (TODO this behaviour will cause problems in multi client scenarios,find a workaround)
 /// </summary>
 /// <param name="me">the hub you want to disconnect from</param>
 public void DisconnectHub(Hub me)
 {
     lock (connected_hubs_lock)
     {
         connected_hubs.Remove(me);
     }
     me.Disconnect();
     me.Ungrab(); //hub event handlers should be ungrabbed
 }
 //TODO if a user is connected to more than one hub we run into inconsistencies when the user disconnects the
 //actually used hub.. we could still send him messages over another hub but for the client the source is
 //just offline
 //solution would be maybe a list of hubs instead of just one , one feature for later
 /// <summary>
 /// Update Source Online Status for a specific User on a certain Hub
 /// this will update the online status of the user 
 /// and try to find the find the affected queue entry source ,if there is any
 /// </summary>
 /// <param name="username">the user whos status changed</param>
 /// <param name="source_hub">the hub the user's status changed on</param>
 /// <param name="is_online">TRUE if the user went ONLINE</param>
 private void UpdateSourcesByUsername(string username, Hub source_hub, bool is_online)
 {
     download_queue.UpdateSourcesByUsername(username, source_hub, is_online);
 }
 /// <summary>
 /// Update Source Online Status for a specific Hub
 /// this will update the online status of the whole user list of the specified hub
 /// and try to find the find the affected queue entry sources ,if any
 /// </summary>
 /// <param name="me">the hub which status has changed</param>
 /// <param name="is_online">TRUE if you want to set the sources to ONLINE</param>
 private void UpdateSourcesByHub(Hub me, bool is_online)
 {
     foreach (string username in me.UserList)
         UpdateSourcesByUsername(username, me, is_online);
 }
Esempio n. 9
0
 /// <summary>
 /// Copy the hub's values into a new hub instance and return it
 /// this will not duplicate a connection / socket - only values
 /// </summary>
 /// <returns>a new hub instance containing all values of the original hub</returns>
 public Hub Copy()
 {
     Hub ret = new Hub();
     ret.address = this.address;
     ret.auto_reconnect = this.auto_reconnect;
     ret.country = this.country;
     ret.description = this.description;
     ret.ip = this.ip;
     ret.is_connecting = false;
     ret.is_connected = false;
     ret.is_extended_protocol = false;
     ret.is_grabbed = this.is_grabbed;
     ret.is_logged_in = false;
     ret.Connected = this.Connected;
     ret.Disconnected = this.Disconnected;
     ret.UnableToConnect = this.UnableToConnect;
     ret.LoggedIn = this.LoggedIn;
     ret.SearchResultReceived = this.SearchResultReceived;
     ret.UserJoined = this.UserJoined;
     ret.UserQuit = this.UserQuit;
     ret.MoveForced = this.MoveForced;
     ret.max_hubs = 0;
     ret.max_users = 0;
     ret.min_share = 0;
     ret.min_slots = 0;
     ret.my_connection_mode = this.my_connection_mode;
     ret.my_connection_speed = this.my_connection_speed;
     ret.my_email = this.my_email;
     ret.my_ip = this.my_ip;
     ret.my_name = this.my_name;
     ret.my_share_size = this.my_share_size;
     ret.my_tag_version = this.my_tag_version;
     ret.my_tcp_port = this.my_tcp_port;
     ret.my_udp_port = this.my_udp_port;
     ret.my_version = this.my_version;
     ret.name = this.name;
     ret.nick = this.nick;
     ret.op_list = new List<string>();
     ret.port = this.port;
     ret.shared = 0;
     ret.topic = this.topic;
     ret.user_list = new List<string>();
     ret.UserList = new List<string>();
     ret.users = 0;
     return (ret);
 }
Esempio n. 10
0
 public void TestLocalHubConnect()
 {
     Console.WriteLine("Test to connect to a local hub (remember to start some hub before).");
     bool wait = true;
     Hub hub = new Hub();
     hub.Address = "localhost";
     hub.Connected += delegate(Hub connected)
     {
         Console.WriteLine("Hub Connected");
         //Assert.IsTrue(!string.IsNullOrEmpty(external_ip), "no ip address fetched");
         //wait = false;
     };
     hub.LoggedIn += delegate(Hub logged_in)
     {
         Console.WriteLine("Hub Logged in");
         wait = false;
     };
     hub.Disconnected += delegate(Hub disconnected)
     {
         if (wait)
         {
             Console.WriteLine("Test failed : Hub disconnected.");
             Assert.Fail("Test failed : Hub disconnected.");
         }
     };
     hub.UnableToConnect += delegate(Hub error)
     {
         Console.WriteLine("Test failed : Unable to connect to");
     };
     hub.IsGrabbedByClient = true;
     hub.Connect();
     Console.WriteLine("Waiting for hub events.");
     DateTime start = DateTime.Now;
     while (wait)
     {
         if (DateTime.Now - start > new TimeSpan(0, 0, 10))
         {
             Console.WriteLine("");
             Console.WriteLine("Operation took too long");
             wait = false;
             Assert.Fail("Operation took too long");
         }
         Console.Write(".");
         Thread.Sleep(250);
     }
     hub.Disconnect();
     Console.WriteLine("Local Hub Connect Test successful.");
 }
        /// <summary>
        /// Reply to a search from a user via udp
        /// (active connection of peer user required)
        /// </summary>
        /// <param name="result_name">the filename of the share found</param>
        /// <param name="filesize">the filesize of the share</param>
        /// <param name="hub">the hub the user is connected to</param>
        /// <param name="search">a whole lot of parameters of the search initiated by a remote user (including ip and port,which we will need here)</param>
        public void SearchReply(string result_name, long filesize, Hub hub, Hub.SearchParameters search)
        {
            try
            {
            string temp_hub = hub.Name;
            if (search.HasTTH) temp_hub = "TTH:" + search.tth;
            string reply = "$SR " + hub.Nick + " " + result_name + (char)0x05 + filesize + " 1/1" + (char)0x05 + temp_hub + " (" + hub.IP + ":" + hub.Port + ")|";
            Console.WriteLine("Replying to active search: " + reply);
            IPEndPoint udp_reply_endpoint = new IPEndPoint(IPAddress.Parse(search.ip), search.port);
            //EndPoint temp_receive_from_endpoint = (EndPoint)receive_from_endpoint;

            byte[] send_bytes = System.Text.Encoding.Default.GetBytes(reply);
            udp_socket.BeginSendTo(send_bytes, 0, send_bytes.Length, SocketFlags.None,udp_reply_endpoint, new AsyncCallback(SearchReplyCallback), udp_socket);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Exception during sending of SearchReply to: "+search.ip+":"+search.port+" : "+ex.Message);
            }
        }