Changeset 171
- Timestamp:
- 03/09/07 04:49:42 (2 years ago)
- Files:
-
- dmp/trunk/Bridge/Bridge.cs (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
dmp/trunk/Bridge/Bridge.cs
r169 r171 46 46 private System.Collections.Generic.Dictionary<string, int> serverNameToIdList; 47 47 48 private BridgeDatabase d b;48 private BridgeDatabase database; 49 49 50 50 private bool myServiceLocatorStarted; … … 53 53 /// Discovers servers 54 54 /// </summary> 55 p ublicDAAP.ServiceLocator myServiceLocator;55 private DAAP.ServiceLocator myServiceLocator; 56 56 57 57 /// <summary> … … 61 61 clientList = new Dictionary<int, Client>(); 62 62 this.serverNameToIdList = new Dictionary<string, int>(); 63 d b= new BridgeDatabase();63 database = new BridgeDatabase(); 64 64 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 } 69 71 } 70 72 … … 79 81 { 80 82 this.myServiceLocatorStarted = true; 81 this.myServiceLocator.Start(); 83 lock (this.myServiceLocator) { 84 this.myServiceLocator.Start(); 85 } 82 86 } 83 87 } … … 106 110 /// bridge's dictionary of clients or -1 if the connect fails</returns> 107 111 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 109 117 return -1; 110 118 } … … 125 133 /// bridge's dictionary of clients or -1 if the connect fails</returns> 126 134 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 128 140 return -1; 129 141 } … … 145 157 /// id, libraryId, artist, album, title</returns> 146 158 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 } 148 163 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 } 156 174 return theTable; 157 175 } … … 162 180 /// <returns></returns> 163 181 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 } 165 186 Dictionary<String, Service> serverDict = new Dictionary<string, Service>(); 166 187 String key; … … 197 218 public DataTable getSongsInCollection() 198 219 { 199 DataTable theTable = this.db.getSongsInCollection(); 220 DataTable theTable; 221 lock (this.database) { 222 theTable = this.database.getSongsInCollection(); 223 } 200 224 return theTable; 201 225 } … … 208 232 public Stream getSongStream(int id, out long length) 209 233 { 210 int libraryId = this.db.getRemoteLibraryId(id); 234 int libraryId; 235 lock (this.database) { 236 libraryId = this.database.getRemoteLibraryId(id); 237 } 211 238 //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 } 213 243 214 244 //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 } 217 255 return musicStream; 218 256 //return null; … … 226 264 public Track getTrackInfo(int id) 227 265 { 228 return db.getRemoteTrack(id); 266 Track theTrack; 267 lock (this.database) { 268 theTrack = database.getRemoteTrack(id); 269 } 270 return theTrack; 229 271 } 230 272 … … 236 278 String remoteSongFormat = this.db.getRemoteSongFormat(id); 237 279 * */ 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 } 241 290 return playbackUri.ToString(); 242 291 } … … 265 314 public bool isDatabaseUp() 266 315 { 267 return this.db.isDatabaseUp(); 316 bool theDatabaseIsUp; 317 lock(this.database){ 318 theDatabaseIsUp = this.database.isDatabaseUp(); 319 } 320 return theDatabaseIsUp ; 268 321 } 269 322 … … 275 328 private int LoginAndSaveToDatabase(Client newClient) { 276 329 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 } 287 342 newClient.Login(); // Passwords are for wimps 288 343 Console.WriteLine("Connected to " + newClient.Name); … … 312 367 /// <returns></returns> 313 368 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 } 316 378 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 } 319 385 DataTable newTable = this.getAvailableSongsInCollection(); 320 386 if (this.ServerStatusChanged != null) { … … 337 403 /// <returns>True if it worked, false otherwise</returns> 338 404 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 } 341 414 bool result = this.RemoveServerConnection(libraryId); 342 415 return result; … … 355 428 Service newServer = args.Service; 356 429 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 } 358 434 Console.WriteLine("Library ID for " + newServer.Name + " is " + libraryId); 359 435 if (this.ServerAdded != null) { … … 373 449 private void ZeroConfServerLost(object o, DAAP.ServiceArgs args) { 374 450 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 ) { 376 456 // 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 } 382 469 if (this.ServerRemoved != null) { 383 470 this.ServerRemoved(this, new ServerStatusEventArgs(oldServer.Name, true));
