Added checking to prevent cascading threads and server checks.
[MenuTunes.git] / MainController.m
index 8abe8de..65c718c 100755 (executable)
@@ -89,7 +89,7 @@ static MainController *sharedController;
     if ([df boolForKey:@"enableSharing"]) {
         [self setServerStatus:YES];
     } else if ([df boolForKey:@"useSharedPlayer"]) {
-        [self checkForRemoteServer];
+        [self checkForRemoteServerAndConnectImmediately:YES];
     }
     
     //Setup for notification of the remote player launching or quitting
@@ -1131,9 +1131,22 @@ static MainController *sharedController;
 }
 
 - (void)checkForRemoteServer
+{
+    [self checkForRemoteServerAndConnectImmediately:NO];
+}
+
+- (void)checkForRemoteServerAndConnectImmediately:(BOOL)connectImmediately
 {
     ITDebugLog(@"Checking for remote server.");
-    [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:nil];
+    if (!_checkingForServer) {
+        if (!_serverCheckLock) {
+            _serverCheckLock = [[NSLock alloc] init];
+        }
+        [_serverCheckLock lock];
+        _checkingForServer = YES;
+        [_serverCheckLock unlock];
+        [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:[NSNumber numberWithBool:connectImmediately]];
+    }
 }
 
 - (void)runRemoteServerCheck:(id)sender
@@ -1142,11 +1155,18 @@ static MainController *sharedController;
     ITDebugLog(@"Remote server check running.");
     if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
         ITDebugLog(@"Remote server found.");
-        [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO];
+        if ([sender boolValue]) {
+            [self performSelectorOnMainThread:@selector(connectToServer) withObject:nil waitUntilDone:NO];
+        } else {
+            [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO];
+        }
     } else {
         ITDebugLog(@"Remote server not found.");
         [self performSelectorOnMainThread:@selector(remoteServerNotFound:) withObject:nil waitUntilDone:NO];
     }
+    [_serverCheckLock lock];
+    _checkingForServer = NO;
+    [_serverCheckLock unlock];
     [pool release];
 }
 
@@ -1159,7 +1179,9 @@ static MainController *sharedController;
 
 - (void)remoteServerNotFound:(id)sender
 {
-    [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
+    if (![[NetworkController sharedController] isConnectedToServer]) {
+        [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
+    }
 }
 
 - (void)networkError:(NSException *)exception
@@ -1170,7 +1192,7 @@ static MainController *sharedController;
         [[StatusWindowController sharedController] showNetworkErrorQueryWindow];
         if ([self disconnectFromServer]) {
             [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
-            [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:YES];
+            [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
         } else {
             ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
         }
@@ -1179,9 +1201,10 @@ static MainController *sharedController;
 
 - (void)reconnect
 {
-    if ([self connectToServer] == 0) {
-        [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:YES];
-    }
+    /*if ([self connectToServer] == 0) {
+        [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
+    }*/
+    [self checkForRemoteServerAndConnectImmediately:YES];
     [[StatusWindow sharedWindow] setLocked:NO];
     [[StatusWindow sharedWindow] vanish:self];
     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
@@ -1276,6 +1299,7 @@ static MainController *sharedController;
     [statusWindowController release];
     [menuController release];
     [networkController release];
+    [_serverCheckLock release];
     [super dealloc];
 }