More work done to networking. Bugfixes out the wazoo, but there's still
[MenuTunes.git] / MainController.m
index f47c01b..a9e67e9 100755 (executable)
@@ -2,6 +2,7 @@
 #import "MenuController.h"
 #import "PreferencesController.h"
 #import "NetworkController.h"
 #import "MenuController.h"
 #import "PreferencesController.h"
 #import "NetworkController.h"
+#import "NetworkObject.h"
 #import <ITKit/ITHotKeyCenter.h>
 #import <ITKit/ITHotKey.h>
 #import <ITKit/ITKeyCombo.h>
 #import <ITKit/ITHotKeyCenter.h>
 #import <ITKit/ITHotKey.h>
 #import <ITKit/ITKeyCombo.h>
@@ -61,8 +62,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
@@ -942,36 +945,84 @@ static MainController *sharedController;
 
 - (BOOL)connectToServer
 {
 
 - (BOOL)connectToServer
 {
+    int result;
+    ITDebugLog(@"Attempting to connect to shared remote.");
+    result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
     //Connect
     //Connect
-    if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) {
-        currentRemote = [networkController sharedRemote];
-        [refreshTimer invalidate];
+    if (result == 1) {
+        currentRemote = [[[networkController networkObject] remote] retain];
+        [self timerUpdate];
+        //[refreshTimer invalidate];
+        ITDebugLog(@"Connection successful.");
         return YES;
         return YES;
-    } else {
+    } else if (result == 0) {
+        ITDebugLog(@"Connection failed.");
+        currentRemote = [remoteArray objectAtIndex:0];
+        return NO;
+    } else if (result == -1) {
+        ITDebugLog(@"Connection failed.");
         currentRemote = [remoteArray objectAtIndex:0];
         return NO;
         currentRemote = [remoteArray objectAtIndex:0];
         return NO;
+        //Do something about the password being invalid
     }
 }
 
 - (BOOL)disconnectFromServer
 {
     }
 }
 
 - (BOOL)disconnectFromServer
 {
+    ITDebugLog(@"Disconnecting from shared remote.");
     //Disconnect
     //Disconnect
+    [currentRemote release];
     currentRemote = [remoteArray objectAtIndex:0];
     [networkController disconnect];
     [self timerUpdate];
     return YES;
 }
 
     currentRemote = [remoteArray objectAtIndex:0];
     [networkController disconnect];
     [self timerUpdate];
     return YES;
 }
 
+- (void)checkForRemoteServer:(NSTimer *)timer
+{
+    ITDebugLog(@"Checking for remote server.");
+    if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
+        ITDebugLog(@"Remote server found.");
+        [timer invalidate];
+        if (![networkController isConnectedToServer]) {
+            [[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);
-    if ([networkController isConnectedToServer] && [self disconnectFromServer]) {
-    } else {
-        ITDebugLog(@"CRITICAL ERROR DISCONNECTING!");
+    NSLog(@"%@", [exception reason]);
+    if ([[exception name] isEqualToString:NSPortTimeoutException] && [networkController isConnectedToServer]) {
+        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 ([self disconnectFromServer]) {
+            [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
+        } else {
+            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