Changeset 171

Show
Ignore:
Timestamp:
03/09/07 04:49:42 (2 years ago)
Author:
kbarnes3
Message:

Added some locks to hopefully eliminate some concurrency issues.

Files:

Legend:

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

    r169 r171  
    4646                private System.Collections.Generic.Dictionary<string, int> serverNameToIdList; 
    4747 
    48         private BridgeDatabase db
     48        private BridgeDatabase database
    4949 
    5050        private bool myServiceLocatorStarted; 
     
    5353                /// Discovers servers 
    5454                /// </summary> 
    55                 public DAAP.ServiceLocator myServiceLocator; 
     55                private DAAP.ServiceLocator myServiceLocator; 
    5656 
    5757                /// <summary> 
     
    6161            clientList = new Dictionary<int, Client>(); 
    6262                        this.serverNameToIdList = new Dictionary<string, int>(); 
    63             db = new BridgeDatabase(); 
     63            database = new BridgeDatabase(); 
    6464            this.myServiceLocator = new ServiceLocator(); 
    65             this.myServiceLocator.ShowLocalServices = true; 
    66                         this.myServiceLocator.Found += ZeroConfServerFound; 
    67                         this.myServiceLocator.Removed += ZeroConfServerLost; 
    68             this.myServiceLocatorStarted = false; 
     65                        lock (this.myServiceLocator) { 
     66                                this.myServiceLocator.ShowLocalServices = true; 
     67                                this.myServiceLocator.Found += ZeroConfServerFound; 
     68                                this.myServiceLocator.Removed += ZeroConfServerLost; 
     69                                this.myServiceLocatorStarted = false; 
     70                        } 
    6971                } 
    7072 
     
    7981            { 
    8082                this.myServiceLocatorStarted = true; 
    81                 this.myServiceLocator.Start(); 
     83                                lock (this.myServiceLocator) { 
     84                                        this.myServiceLocator.Start(); 
     85                                } 
    8286            } 
    8387        } 
     
    106110                /// bridge's dictionary of clients or -1 if the connect fails</returns> 
    107111                public int AddServerConnection(Service theService) { 
    108                         if (this.serverNameToIdList.ContainsKey(theService.Name)) { // Already connected 
     112                        bool containsKey; 
     113                        lock(this.serverNameToIdList){ 
     114                                containsKey = this.serverNameToIdList.ContainsKey(theService.Name); 
     115                        } 
     116                        if (containsKey) { // Already connected 
    109117                                return -1; 
    110118                        } 
     
    125133                /// bridge's dictionary of clients or -1 if the connect fails</returns> 
    126134                public int AddServerConnection(string zeroconfName) { 
    127                         if (this.serverNameToIdList.ContainsKey(zeroconfName)) { // Already connected 
     135                        bool containsKey; 
     136                        lock(this.serverNameToIdList){ 
     137                                containsKey = this.serverNameToIdList.ContainsKey(zeroconfName); 
     138                        } 
     139                        if (containsKey) { // Already connected 
    128140                                return -1; 
    129141                        } 
     
    145157                /// id, libraryId, artist, album, title</returns> 
    146158                public DataTable getAvailableSongsInCollection() { 
    147                         int[] libraries = new int[this.clientList.Count]; 
     159                        int[] libraries; 
     160                        lock (this.clientList) { 
     161                                libraries = new int[this.clientList.Count]; 
     162                        } 
    148163                        int libraryCount = 0; 
    149                         foreach (int key in this.clientList.Keys) { 
    150                                 libraries[libraryCount] = key; 
    151                                 libraryCount++; 
    152                         } 
    153  
    154  
    155                         DataTable theTable = this.db.getAvailableSongsInCollection(libraries); 
     164                        lock (this.clientList) { 
     165                                foreach (int key in this.clientList.Keys) { 
     166                                        libraries[libraryCount] = key; 
     167                                        libraryCount++; 
     168                                } 
     169                        } 
     170                        DataTable theTable; 
     171                        lock (this.database) { 
     172                                theTable = this.database.getAvailableSongsInCollection(libraries); 
     173                        } 
    156174                        return theTable; 
    157175                } 
     
    162180                /// <returns></returns> 
    163181                public Dictionary<String, Service> getZeroconfServerList() { 
    164                         System.Collections.IEnumerable serverListing = this.myServiceLocator.Services; 
     182                        System.Collections.IEnumerable serverListing; 
     183                        lock (this.myServiceLocator) { 
     184                                serverListing = this.myServiceLocator.Services; 
     185                        } 
    165186                        Dictionary<String, Service> serverDict = new Dictionary<string, Service>(); 
    166187                        String key; 
     
    197218                public DataTable getSongsInCollection() 
    198219                { 
    199                         DataTable theTable = this.db.getSongsInCollection(); 
     220                        DataTable theTable; 
     221                        lock (this.database) { 
     222                                theTable = this.database.getSongsInCollection(); 
     223                        } 
    200224                        return theTable; 
    201225                } 
     
    208232        public Stream getSongStream(int id, out long length) 
    209233        { 
    210             int libraryId = this.db.getRemoteLibraryId(id); 
     234                        int libraryId; 
     235                        lock (this.database) { 
     236                                libraryId = this.database.getRemoteLibraryId(id); 
     237                        } 
    211238            //int remoteSongId = this.db.getRemoteSongId(id); 
    212             long songSize = this.db.getRemoteSongSize(id); 
     239                        long songSize; 
     240                        lock (this.database) { 
     241                                songSize = this.database.getRemoteSongSize(id); 
     242                        } 
    213243 
    214244            //Track song = clientList[libraryId].Database.LookupTrackById(remoteSongId); 
    215                         Track song = db.getRemoteTrack(id); 
    216             Stream musicStream = clientList[libraryId].Database.StreamTrack(song, out length); 
     245                        Track song; 
     246                        lock (this.database) { 
     247                                song = database.getRemoteTrack(id); 
     248                        } 
     249                        Stream musicStream; 
     250                        lock (this.clientList) { 
     251                                lock (this.database) { 
     252                                        musicStream = clientList[libraryId].Database.StreamTrack(song, out length); 
     253                                } 
     254                        } 
    217255                        return musicStream; 
    218256            //return null; 
     
    226264                public Track getTrackInfo(int id) 
    227265                { 
    228                         return db.getRemoteTrack(id); 
     266                        Track theTrack; 
     267                        lock (this.database) { 
     268                                theTrack = database.getRemoteTrack(id); 
     269                        } 
     270                        return theTrack; 
    229271                } 
    230272 
     
    236278            String remoteSongFormat = this.db.getRemoteSongFormat(id); 
    237279             * */ 
    238             int libraryId = this.db.getRemoteLibraryId(id); 
    239             Track playTrack = db.getRemoteTrack(id); 
    240             UriBuilder playbackUri = clientList[libraryId].Database.StreamTrackURI(playTrack); 
     280                        int libraryId; 
     281                        Track playTrack; 
     282                        lock (this.database) { 
     283                                libraryId = this.database.getRemoteLibraryId(id); 
     284                                playTrack = database.getRemoteTrack(id); 
     285                        } 
     286                        UriBuilder playbackUri; 
     287                        lock (this.clientList) { 
     288                                        playbackUri = clientList[libraryId].Database.StreamTrackURI(playTrack); 
     289                        } 
    241290            return playbackUri.ToString(); 
    242291        } 
     
    265314        public bool isDatabaseUp() 
    266315        { 
    267             return this.db.isDatabaseUp(); 
     316                        bool theDatabaseIsUp; 
     317                        lock(this.database){ 
     318                                theDatabaseIsUp = this.database.isDatabaseUp(); 
     319                        } 
     320            return theDatabaseIsUp ; 
    268321        } 
    269322 
     
    275328                private int LoginAndSaveToDatabase(Client newClient) { 
    276329                        try { 
    277  
    278                 if (!clientList.ContainsKey(newClient.Id)) 
    279                 { 
    280                     Console.WriteLine("Added libraryid to client list: " + newClient.Id); 
    281                     this.clientList.Add(newClient.Id, newClient); 
    282                                         this.serverNameToIdList.Add(newClient.Name, newClient.Id); 
    283                 } 
    284                 else 
    285                     Console.WriteLine("Following libraryId already in clientlist: " + newClient.Id); 
    286  
     330                                lock (this.clientList) { 
     331                                        if (!clientList.ContainsKey(newClient.Id)) { 
     332                                                Console.WriteLine("Added libraryid to client list: " + newClient.Id); 
     333                                                this.clientList.Add(newClient.Id, newClient); 
     334                                                lock (this.serverNameToIdList) { 
     335                                                        this.serverNameToIdList.Add(newClient.Name, newClient.Id); 
     336                                                } 
     337                                        } 
     338                                        else { 
     339                                                Console.WriteLine("Following libraryId already in clientlist: " + newClient.Id); 
     340                                        } 
     341                                } 
    287342                                newClient.Login(); // Passwords are for wimps 
    288343                                Console.WriteLine("Connected to " + newClient.Name); 
     
    312367                /// <returns></returns> 
    313368                public bool RemoveServerConnection(int libraryId) { 
    314                         if (this.clientList.ContainsKey(libraryId)) { 
    315                                 Client oldClient = this.clientList[libraryId]; 
     369                        bool containsKey; 
     370                        lock (this.clientList) { 
     371                                containsKey = (this.clientList.ContainsKey(libraryId)); 
     372                        } 
     373                        if (containsKey) { 
     374                                Client oldClient; 
     375                                lock (this.clientList) { 
     376                                        oldClient = this.clientList[libraryId]; 
     377                                } 
    316378                                oldClient.Logout(); 
    317                                 this.clientList.Remove(libraryId); 
    318                                 this.db.RemoveServer(libraryId); 
     379                                lock (this.clientList) { 
     380                                        this.clientList.Remove(libraryId); 
     381                                } 
     382                                lock (this.database) { 
     383                                        this.database.RemoveServer(libraryId); 
     384                                } 
    319385                                DataTable newTable = this.getAvailableSongsInCollection(); 
    320386                                if (this.ServerStatusChanged != null) { 
     
    337403                /// <returns>True if it worked, false otherwise</returns> 
    338404                public bool RemoveServerConnection(string zeroconfName) { 
    339                         if (this.serverNameToIdList.ContainsKey(zeroconfName)) { 
    340                                 int libraryId = this.serverNameToIdList[zeroconfName]; 
     405                        bool containsKey; 
     406                        lock(this.serverNameToIdList){ 
     407                                containsKey = this.serverNameToIdList.ContainsKey(zeroconfName); 
     408                        } 
     409                        if (containsKey) { 
     410                                int libraryId; 
     411                                lock (this.serverNameToIdList) { 
     412                                        libraryId = this.serverNameToIdList[zeroconfName]; 
     413                                } 
    341414                                bool result = this.RemoveServerConnection(libraryId); 
    342415                                return result; 
     
    355428                        Service newServer = args.Service; 
    356429                        Console.WriteLine("ZeroConf found " + newServer.Name); 
    357                         int libraryId = this.db.getLibraryIdForName(newServer.Name); 
     430                        int libraryId; 
     431                        lock (this.database) { 
     432                                libraryId = this.database.getLibraryIdForName(newServer.Name); 
     433                        } 
    358434                        Console.WriteLine("Library ID for " + newServer.Name + " is " + libraryId); 
    359435                        if (this.ServerAdded != null) { 
     
    373449                private void ZeroConfServerLost(object o, DAAP.ServiceArgs args) { 
    374450                        Service oldServer = args.Service; 
    375                         if (this.serverNameToIdList.ContainsKey(oldServer.Name)) { 
     451                        bool containsKey; 
     452                        lock (this.serverNameToIdList) { 
     453                                containsKey = this.serverNameToIdList.ContainsKey(oldServer.Name); 
     454                        } 
     455                        if (containsKey ) { 
    376456                                // It was connected, drop it 
    377                                 int libraryId = this.serverNameToIdList[oldServer.Name]; 
    378                                 if (this.clientList.ContainsKey(libraryId)) { 
    379                                         this.clientList.Remove(libraryId); 
    380                                 } 
    381                                 this.serverNameToIdList.Remove(oldServer.Name); 
     457                                int libraryId; 
     458                                lock(this.serverNameToIdList){ 
     459                                        libraryId = this.serverNameToIdList[oldServer.Name]; 
     460                                } 
     461                                lock (this.clientList) { 
     462                                        if (this.clientList.ContainsKey(libraryId)) { 
     463                                                this.clientList.Remove(libraryId); 
     464                                        } 
     465                                } 
     466                                lock(this.serverNameToIdList){ 
     467                                        this.serverNameToIdList.Remove(oldServer.Name); 
     468                                } 
    382469                                if (this.ServerRemoved != null) { 
    383470                                        this.ServerRemoved(this, new ServerStatusEventArgs(oldServer.Name, true));