Adding a new proxy object for the networking. It's broken right now, but
[MenuTunes.git] / MainController.m
index bbb0cec..92f3ee6 100755 (executable)
@@ -61,8 +61,10 @@ static MainController *sharedController;
     networkController = [[NetworkController alloc] init];
     if ([df boolForKey:@"enableSharing"]) {
         [self setServerStatus:YES];
     networkController = [[NetworkController alloc] init];
     if ([df boolForKey:@"enableSharing"]) {
         [self setServerStatus:YES];
-    } else if ([df boolForKey:@"useSharedPlayer"] && [df boolForKey:@"alwaysUseSharedPlayer"]) {
-        [self connectToServer];
+    } else if ([df boolForKey:@"useSharedPlayer"]) {
+        if (![self connectToServer]) {
+            [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
+        }
     }
     
     //Setup for notification of the remote player launching or quitting
     }
     
     //Setup for notification of the remote player launching or quitting
@@ -294,14 +296,14 @@ static MainController *sharedController;
         timerUpdating = YES;
         
         NS_DURING
         timerUpdating = YES;
         
         NS_DURING
-        latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
-        [menuController rebuildSubmenus];
-
-        if ( [df boolForKey:@"showSongInfoOnChange"] ) {
-            [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
-        }
-        
-        [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
+            latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
+            [menuController rebuildSubmenus];
+    
+            if ( [df boolForKey:@"showSongInfoOnChange"] ) {
+                [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
+            }
+            
+            [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
         NS_HANDLER
             [self networkError:localException];
         NS_ENDHANDLER
         NS_HANDLER
             [self networkError:localException];
         NS_ENDHANDLER
@@ -942,12 +944,15 @@ static MainController *sharedController;
 
 - (BOOL)connectToServer
 {
 
 - (BOOL)connectToServer
 {
+    ITDebugLog(@"Attempting to connect to shared remote.");
     //Connect
     if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) {
     //Connect
     if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) {
-        currentRemote = [networkController sharedRemote];
+        currentRemote = [[networkController networkObject] remote];
         [refreshTimer invalidate];
         [refreshTimer invalidate];
+        ITDebugLog(@"Connection successful.");
         return YES;
     } else {
         return YES;
     } else {
+        ITDebugLog(@"Connection failed.");
         currentRemote = [remoteArray objectAtIndex:0];
         return NO;
     }
         currentRemote = [remoteArray objectAtIndex:0];
         return NO;
     }
@@ -955,6 +960,7 @@ static MainController *sharedController;
 
 - (BOOL)disconnectFromServer
 {
 
 - (BOOL)disconnectFromServer
 {
+    ITDebugLog(@"Disconnecting from shared remote.");
     //Disconnect
     currentRemote = [remoteArray objectAtIndex:0];
     [networkController disconnect];
     //Disconnect
     currentRemote = [remoteArray objectAtIndex:0];
     [networkController disconnect];
@@ -962,16 +968,49 @@ static MainController *sharedController;
     return YES;
 }
 
     return YES;
 }
 
+- (void)checkForRemoteServer:(NSTimer *)timer
+{
+    ITDebugLog(@"Checking for remote server.");
+    if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
+        ITDebugLog(@"Remote server found.");
+        [timer invalidate];
+        [[StatusWindowController sharedController] showReconnectQueryWindow];
+    } else {
+        ITDebugLog(@"Remote server not found.");
+    }
+}
+
 - (void)networkError:(NSException *)exception
 {
     ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
 - (void)networkError:(NSException *)exception
 {
     ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
-    NSRunAlertPanel(@"Remote MenuTunes Disconnected", @"The MenuTunes server you were connected to stopped responding or quit. MenuTunes will revert back to the local player.", @"OK", nil, nil);
+    NSLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
+    NSRunCriticalAlertPanel(@"Remote MenuTunes Disconnected", @"The MenuTunes server you were connected to stopped responding or quit. MenuTunes will revert back to the local player.", @"OK", nil, nil);
     if ([networkController isConnectedToServer] && [self disconnectFromServer]) {
     if ([networkController isConnectedToServer] && [self disconnectFromServer]) {
+        if ([[exception name] isEqualToString:NSPortTimeoutException]) {
+            [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
+        }
     } else {
     } else {
-        ITDebugLog(@"CRITICAL ERROR DISCONNECTING!");
+        ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
     }
 }
 
     }
 }
 
+- (void)reconnect
+{
+    if (![self connectToServer]) {
+        [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
+    }
+    [[StatusWindow sharedWindow] setLocked:NO];
+    [[StatusWindow sharedWindow] vanish:self];
+    [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
+}
+
+- (void)cancelReconnect
+{
+    [[StatusWindow sharedWindow] setLocked:NO];
+    [[StatusWindow sharedWindow] vanish:self];
+    [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
+}
+
 /*************************************************************************/
 #pragma mark -
 #pragma mark WORKSPACE NOTIFICATION HANDLERS
 /*************************************************************************/
 #pragma mark -
 #pragma mark WORKSPACE NOTIFICATION HANDLERS