Changeset 136

Show
Ignore:
Timestamp:
03/06/07 20:48:00 (2 years ago)
Author:
kbarnes3
Message:

Added an event to fire when a server is added. Untested, so sorry if it breaks.

Files:

Legend:

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

    r134 r136  
    1515        public class Bridge 
    1616        { 
     17                public event EventHandler<ServerAddedEventArgs > ServerAdded; 
     18 
    1719                /// <summary> 
    1820                /// The dictionary of DAAP clients, since I believe each instance only connects to one server 
     
    2022                /// </summary> 
    2123                private System.Collections.Generic.Dictionary<int, Client> clientList; 
     24 
     25                /// <summary> 
     26                /// This dictionary maps a server name to the library ID found in the Bridge Database 
     27                /// </summary> 
     28                private System.Collections.Generic.Dictionary<string, int> serverNameToIdList; 
    2229 
    2330        private BridgeDatabase db; 
     
    3845            this.myServiceLocator = new ServiceLocator(); 
    3946            this.myServiceLocator.ShowLocalServices = true; 
     47                        this.myServiceLocator.Found += ZeroConfServerFound; 
    4048            this.myServiceLocatorStarted = false; 
    4149                } 
     
    156164                } 
    157165 
    158         public void AddFoundHandler(ServiceHandler fun){ 
    159             myServiceLocator.Found += fun; 
    160         } 
    161  
    162166                /// <summary> 
    163167                /// Gets the list of all the songs in the collection 
     
    250254                    Console.WriteLine("Added libraryid to client list: " + newClient.Id); 
    251255                    this.clientList.Add(newClient.Id, newClient); 
     256                                        this.serverNameToIdList.Add(newClient.Name, newClient.Id); 
    252257                } 
    253258                else 
     
    264269                        } 
    265270                } 
     271 
     272                /// <summary> 
     273                /// Runs when ZeroConf finds a new server 
     274                /// </summary> 
     275                /// <param name="o"></param> 
     276                /// <param name="args"></param> 
     277                private void ZeroConfServerFound(object o, DAAP.ServiceArgs args) { 
     278                        Service newServer = args.Service; 
     279                        int libraryId = this.db.getLibraryIdForName(newServer.Name); 
     280                        if (libraryId > -1) { //Reconnect to this server 
     281                                int addResult = this.AddServerConnection(newServer); 
     282                                if (addResult > -1) { 
     283                                         
     284                                        this.ServerAdded(this, new ServerAddedEventArgs(newServer.Name, true)); 
     285                                } 
     286                        } 
     287                        else { 
     288                                this.ServerAdded(this, new ServerAddedEventArgs(newServer.Name, false)); 
     289                        } 
     290                } 
    266291        } 
     292 
    267293} 
  • dmp/trunk/Bridge/Bridge.csproj

    r87 r136  
    4848    <Compile Include="Bridge.cs" /> 
    4949    <Compile Include="Properties\AssemblyInfo.cs" /> 
     50    <Compile Include="ServerAddedEventArgs.cs" /> 
    5051  </ItemGroup> 
    5152  <ItemGroup> 
  • dmp/trunk/daap-sharp/BridgeDatabase.cs

    r134 r136  
    238238 
    239239                        return theTable; 
     240                } 
     241 
     242                /// <summary> 
     243                /// Finds the library ID matching the name, if one exists 
     244                /// </summary> 
     245                /// <param name="libName">The name of the server to find an ID for</param> 
     246                /// <returns>The libary ID if it's in the database, -1 otherwise</returns> 
     247                public int getLibraryIdForName(string libName) { 
     248                        string commandText = "SELECT id FROM libraries WHERE name = @name"; 
     249                        SQLiteDataAdapter adapter = new SQLiteDataAdapter(commandText, this.databaseConnection); 
     250                        adapter.SelectCommand.Parameters.Add("name", DbType.String).Value = libName; 
     251                        DataTable tempTable = new DataTable(); 
     252                        this.databaseConnection.Open(); 
     253                        adapter.Fill(tempTable); 
     254                        this.databaseConnection.Close(); 
     255                        if (tempTable.Rows.Count <= 0) { 
     256                                return -1; 
     257                        } 
     258                        else { 
     259                                int libraryId = (int)tempTable.Rows[0]["id"]; 
     260                                return libraryId; 
     261                        } 
    240262                } 
    241263