Changeset 124

Show
Ignore:
Timestamp:
03/03/07 23:34:19 (2 years ago)
Author:
kbarnes3
Message:

Made the Bridge automatically reconnect to previously known servers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • dmp/trunk/BadClient/Form1.cs

    r122 r124  
    2929                { 
    3030                    lblStatus.Text = "Status: It connected?"; 
    31                     DataTable theStuff = myBridge.getSongsInCollection(); 
     31                    DataTable theStuff = myBridge.getAvailableSongsInCollection (); 
    3232                    lblStatus.Text = "Status: Found " + theStuff.Rows.Count + " rows!"; 
    3333                    dgvSongs.Rows.Clear(); 
     
    5858        private void button2_Click(object sender, EventArgs e) 
    5959        { 
    60             DataTable theStuff = this.myBridge.getSongsInCollection(); 
     60            DataTable theStuff = this.myBridge.getAvailableSongsInCollection (); 
    6161            lblStatus.Text = "Status: This doesn't work yet!"; 
    6262            foreach (DataRow row in theStuff.Rows) { 
     
    6666 
    6767                private void btnListServers_Click(object sender, EventArgs e) { 
    68                         Dictionary<String, DAAP.Service> theServers = this.myBridge.getServerList(); 
     68                        Dictionary<String, DAAP.Service> theServers = this.myBridge.getZeroconfServerList(); 
    6969                        foreach (KeyValuePair<string, DAAP.Service> pair in theServers) { 
    7070                                treeView1.Nodes.Add(pair.Value.Name); 
  • dmp/trunk/Bridge/Bridge.cs

    r122 r124  
    3636                        this.myServiceLocator = new ServiceLocator(); 
    3737                        this.myServiceLocator.ShowLocalServices = true; 
    38                         //this.myServiceLocator.Start(); Putting this in the UI 
     38                        this.myServiceLocator.Start(); 
     39                        DataTable dbServers = db.getServersInCollection(); 
     40                        System.Threading.Thread.Sleep(2000); 
     41                        Dictionary<String, Service> whatDAAPFound = this.getZeroconfServerList(); 
     42                        foreach (DataRow row in dbServers.Rows) { 
     43                                string serverName = (string) row["name"]; 
     44                                Console.WriteLine("Trying to connect to " + serverName); 
     45                                if (whatDAAPFound.ContainsKey(serverName)) { 
     46                                        Service newServer = whatDAAPFound[serverName]; 
     47                                        this.AddServerConnection(newServer); 
     48                                } 
     49                                else { 
     50                                        Console.WriteLine("Couldn't find " + serverName); 
     51                                } 
     52                        } 
    3953                } 
    4054                /// <summary> 
     
    4256                /// </summary> 
    4357                /// <param name="theHostName">The host name, in some form that can be resolved</param> 
    44                 /// <param name="thePort">The port number, the default is currently 3690</param> 
     58                /// <param name="thePort">The port number, the default is currently 3689</param> 
    4559                /// <returns>The library id, which is referred to in the db and the  
    46                 /// bridge's dictionary of clients</returns> 
     60                /// bridge's dictionary of clients or -1 if the connect fails</returns> 
    4761        public int AddServerConnection(string theHostName, ushort thePort){ 
    48             Client newClient = new Client(theHostName, thePort); 
    49  
    50             try 
    51             { 
    52                 //List<TrackBase> tracks = new List<TrackBase>(); 
    53                 //List<int> deletedTracks = new List<int>(); 
    54                 //List<PlaylistBase> playlists = new List<PlaylistBase>(); 
    55  
    56                 //setup library for this client 
    57                                 //BridgeDatabase.Library lib = new BridgeDatabase.Library(); 
    58                                 //lib.address = newClient.Address.ToString(); 
    59                                 //lib.port = newClient.Port; 
    60                                 //lib.name = newClient.Name; 
    61                                 //lib = db.addLibrary(lib); 
    62                                 if(!clientList.ContainsKey(newClient.Id)) 
    63                     this.clientList.Add(newClient.Id, newClient); 
    64  
    65                 newClient.Login(); // Passwords are for wimps 
    66  
    67                                 return newClient.Id; 
    68             } 
    69             catch (DAAP.LoginException ex) 
    70             { 
    71                                 Console.WriteLine("Bridge: " + ex.Message); 
     62                        try { 
     63                                Client newClient = new Client(theHostName, thePort); 
     64                                return this.LoginAndSaveToDatabase(newClient); 
     65                        } 
     66                        catch { 
    7267                                return -1; 
    73             } 
    74         } 
    75  
    76         /* 
    77          * If anyone knows why I can't do this let me know, thanks, Luni. 
    78          *  
    79          * It complains that returning the type List<DMP.BridgeDatabase.Song> 
    80          * is less "accessible" than the method getAllSongsInCollection itself, 
    81          * which kinda doesn't make sense unless there is some part of namespaces 
    82          * in .Net I do not understand. 
    83          *  
    84          * // Gets all the songs in the *collection* 
    85         public List<DMP.BridgeDatabase.Song> getAllSongsInCollection() 
    86         { 
    87             return this.db.getSongsInCollection(); 
    88         } 
    89          */ 
    90  
    91                 /// <summary> 
    92                 ///  
     68                        } 
     69                } 
     70                /// <summary> 
     71                /// Connects to the given server and eventually will get the songs from it 
     72                /// </summary> 
     73                /// <param name="theService">The serivce found with ZeroConf to connect to</param> 
     74                /// <returns>The library id, which is referred to in the db and the  
     75                /// bridge's dictionary of clients or -1 if the connect fails</returns> 
     76                public int AddServerConnection(Service theService) { 
     77                        try { 
     78                                Client newClient = new Client(theService); 
     79                                return this.LoginAndSaveToDatabase(newClient); 
     80                        } 
     81                        catch { 
     82                                return -1; 
     83                        } 
     84                } 
     85 
     86                /// <summary> 
     87                /// Gets the list of all the songs in the collection that are on available servers 
     88                /// </summary> 
     89                /// <returns>A DataTable with the following columns: 
     90                /// id, libraryId, artist, album, title</returns> 
     91                public DataTable getAvailableSongsInCollection() { 
     92                        int[] libraries = new int[this.clientList.Count]; 
     93                        int libraryCount = 0; 
     94                        foreach (int key in this.clientList.Keys) { 
     95                                libraries[libraryCount] = key; 
     96                                libraryCount++; 
     97                        } 
     98                        DataTable theTable = this.db.getAvailableSongsInCollection(libraries); 
     99                        return theTable; 
     100                } 
     101 
     102                /// <summary> 
     103                /// Gets a list of servers that are discoverable at the time using Zeroconf 
    93104                /// </summary> 
    94105                /// <returns></returns> 
    95                 public Dictionary<String, Service> getServerList() { 
     106                public Dictionary<String, Service> getZeroconfServerList() { 
    96107                        System.Collections.IEnumerable serverListing = this.myServiceLocator.Services; 
    97108                        Dictionary<String, Service> serverDict = new Dictionary<string, Service>(); 
     
    100111                        int duplicateCount; 
    101112                        Service server; 
     113                        Console.WriteLine("Listing servers..."); 
    102114                        foreach (System.Collections.DictionaryEntry entry in serverListing) { 
    103115                                server = (Service) entry.Value; 
    104116                                key = server.Name; 
     117                                Console.WriteLine("Found the server \"" + key + "\"."); 
    105118                                if (serverDict.ContainsKey(key)) { 
    106119                                        duplicateCount = 1; 
     
    116129                                } 
    117130                        } 
     131                        Console.WriteLine("Done listing servers."); 
    118132                        return serverDict; 
    119133                } 
     
    200214            return this.db.isDatabaseUp(); 
    201215        } 
     216 
     217                /// <summary> 
     218                /// Attempts to log in and store the connection info in the database 
     219                /// </summary> 
     220                /// <param name="newClient">The client to log in to</param> 
     221                /// <returns>-1 on failure, the library ID on success</returns> 
     222                private int LoginAndSaveToDatabase(Client newClient) { 
     223                        try { 
     224 
     225                                if (!clientList.ContainsKey(newClient.Id)) 
     226                                        this.clientList.Add(newClient.Id, newClient); 
     227 
     228                                newClient.Login(); // Passwords are for wimps 
     229                                Console.WriteLine("Connected to " + newClient.Name); 
     230 
     231                                return newClient.Id; 
     232                        } 
     233                        catch (DAAP.LoginException ex) { 
     234                                Console.WriteLine("Bridge: " + ex.Message); 
     235                                return -1; 
     236                        } 
     237                } 
    202238        } 
    203239} 
  • dmp/trunk/Mono.Zeroconf/BrowseService.cs

    r118 r124  
    3737    public sealed class BrowseService : Service 
    3838    { 
    39                 private Native.DNSServiceResolveReply resolveReply; 
    4039        private bool is_resolved = false; 
    4140        private bool resolve_pending = false; 
     
    4948        public BrowseService(string name, string replyDomain, string regtype) : base(name, replyDomain, regtype) 
    5049        { 
    51                         resolveReply = OnResolveReply; 
    5250        } 
    53  
    54                 ~BrowseService() 
    55                 { 
    56                         GC.KeepAlive(resolveReply); 
    57                 } 
    5851 
    5952        public void Resolve() 
     
    7770            ServiceRef sd_ref; 
    7871            ServiceError error = Native.DNSServiceResolve(out sd_ref, ServiceFlags.None,  
    79                 InterfaceIndex, Name, RegType, ReplyDomain, resolveReply, IntPtr.Zero); 
     72                InterfaceIndex, Name, RegType, ReplyDomain, OnResolveReply, IntPtr.Zero); 
    8073                 
    8174            if(error != ServiceError.NoError) { 
  • dmp/trunk/daap-sharp/BridgeDatabase.cs

    r110 r124  
    205205            insertTrans.Commit(); 
    206206                        this.databaseConnection.Close(); 
     207                } 
     208 
     209                /// <summary> 
     210                /// Gets the list of all the songs in the collection 
     211                /// </summary> 
     212                /// <param name="libraries">A list of the library id's to get the songs from</param> 
     213                /// <returns>A DataTable with the following columns: 
     214                /// id, libraryId, artist, album, title</returns> 
     215                public DataTable getAvailableSongsInCollection(int[] libraries) { 
     216                        StringBuilder commandText = new StringBuilder("SELECT * FROM songs WHERE "); 
     217                        if (libraries.Length == 0) { 
     218                                commandText.Append("libraryId = -1"); 
     219                        } 
     220                        else { 
     221                                commandText.AppendFormat("libraryId = {0}", libraries[0]); 
     222                                for (int i = 1; i < libraries.Length; i++) { 
     223                                        commandText.AppendFormat("OR libraryId = {1}", libraries[i]); 
     224                                } 
     225                        } 
     226                        SQLiteDataAdapter adapter = new SQLiteDataAdapter(commandText.ToString(),  
     227                                this.databaseConnection); 
     228                        DataTable theTable = new DataTable(); 
     229                        this.databaseConnection.Open(); 
     230                        adapter.Fill(theTable); 
     231                        this.databaseConnection.Close(); 
     232 
     233                        return theTable; 
     234                } 
     235 
     236                /// <summary> 
     237                /// Returns a list of distinct libraries that the bridge has connected to before 
     238                /// </summary> 
     239                /// <returns></returns> 
     240                public DataTable getServersInCollection() { 
     241                        string commandText = "SELECT id, name FROM libraries"; 
     242                        SQLiteDataAdapter adapter = new SQLiteDataAdapter(commandText, this.databaseConnection); 
     243                        DataTable theTable = new DataTable(); 
     244                        this.databaseConnection.Open(); 
     245                        adapter.Fill(theTable); 
     246                        this.databaseConnection.Close(); 
     247 
     248                        return theTable; 
    207249                } 
    208250