Added a play count option to track info.
[MenuTunes.git] / NetworkController.m
index fb3976c..b191c6d 100755 (executable)
 
 #import "NetworkController.h"
 #import "MainController.h"
 
 #import "NetworkController.h"
 #import "MainController.h"
+#import "NetworkObject.h"
+#import "PreferencesController.h"
 #import <ITFoundation/ITDebug.h>
 #import <ITFoundation/ITFoundation.h>
 #import <ITFoundation/ITDebug.h>
 #import <ITFoundation/ITFoundation.h>
-#import <ITMTRemote/ITMTRemote.h>
 
 static NetworkController *sharedController;
 
 
 static NetworkController *sharedController;
 
@@ -32,6 +33,8 @@ static NetworkController *sharedController;
         sharedController = self;
         browser = [[NSNetServiceBrowser alloc] init];
         [browser setDelegate:self];
         sharedController = self;
         browser = [[NSNetServiceBrowser alloc] init];
         [browser setDelegate:self];
+        rootObject = [[NetworkObject alloc] init];
+        serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
     }
     return self;
 }
     }
     return self;
 }
@@ -40,11 +43,12 @@ static NetworkController *sharedController;
 {
     [self disconnect];
     if (serverOn) {
 {
     [self disconnect];
     if (serverOn) {
-        [serverConnection invalidate];
         [serverConnection release];
     }
     [serverPass release];
     [clientPass release];
         [serverConnection release];
     }
     [serverPass release];
     [clientPass release];
+    [serverPort release];
+    [rootObject release];
     [clientProxy release];
     [remoteServices release];
     [browser release];
     [clientProxy release];
     [remoteServices release];
     [browser release];
@@ -73,16 +77,17 @@ static NetworkController *sharedController;
         NSData *fullPass;
         //Turn on
         NS_DURING
         NSData *fullPass;
         //Turn on
         NS_DURING
-            serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
             serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
                                                      sendPort:serverPort];
             serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
                                                      sendPort:serverPort];
-            [serverConnection setRootObject:[[MainController sharedController] currentRemote]];
+            [serverConnection setRootObject:rootObject];
+            [rootObject makeValid];
             [serverConnection registerName:@"ITMTPlayerHost"];
             [serverConnection registerName:@"ITMTPlayerHost"];
-            [serverConnection setDelegate:self];
         NS_HANDLER
         NS_HANDLER
+            [serverConnection setRootObject:nil];
             [serverConnection release];
             [serverPort release];
             ITDebugLog(@"Error starting server!");
             [serverConnection release];
             [serverPort release];
             ITDebugLog(@"Error starting server!");
+            return;
         NS_ENDHANDLER
         ITDebugLog(@"Started server.");
         if (!name) {
         NS_ENDHANDLER
         ITDebugLog(@"Started server.");
         if (!name) {
@@ -93,7 +98,7 @@ static NetworkController *sharedController;
                                         name:name
                                         port:SERVER_PORT];
         fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
                                         name:name
                                         port:SERVER_PORT];
         fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
-        if (fullPass) {
+        if ([fullPass length]) {
             [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
             [serverPass release];
             serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
             [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
             [serverPass release];
             serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
@@ -102,23 +107,28 @@ static NetworkController *sharedController;
         }
         [service publish];
         serverOn = YES;
         }
         [service publish];
         serverOn = YES;
+        ITDebugLog(@"Server service published.");
     } else if (serverOn && !status && [serverConnection isValid]) {
         //Turn off
         [service stop];
     } else if (serverOn && !status && [serverConnection isValid]) {
         //Turn off
         [service stop];
+        [service release];
+        [rootObject invalidate];
         [serverConnection registerName:nil];
         [serverConnection registerName:nil];
-        [serverPort invalidate];
         [serverConnection invalidate];
         [serverConnection invalidate];
+        //[serverConnection setRootObject:nil];
+        //[[serverConnection sendPort] autorelease];
         [serverConnection release];
         ITDebugLog(@"Stopped server.");
         serverOn = NO;
     }
 }
 
         [serverConnection release];
         ITDebugLog(@"Stopped server.");
         serverOn = NO;
     }
 }
 
-- (BOOL)connectToHost:(NSString *)host
+- (int)connectToHost:(NSString *)host
 {
     NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
     unsigned char buffer;
     ITDebugLog(@"Connecting to host: %@", host);
 {
     NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
     unsigned char buffer;
     ITDebugLog(@"Connecting to host: %@", host);
+    [remoteHost release];
     remoteHost = [host copy];
     if (fullPass) {
         [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
     remoteHost = [host copy];
     if (fullPass) {
         [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
@@ -131,19 +141,49 @@ static NetworkController *sharedController;
         clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
                                            host:host];
         clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
         clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
                                            host:host];
         clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
-        [clientConnection setDelegate:self];
-        [clientConnection setReplyTimeout:5];
+        [clientConnection setReplyTimeout:10];
         clientProxy = [[clientConnection rootProxy] retain];
         clientProxy = [[clientConnection rootProxy] retain];
+        connectedToServer = YES;
     NS_HANDLER
         [clientConnection release];
         [clientPort release];
         ITDebugLog(@"Connection to host failed: %@", host);
     NS_HANDLER
         [clientConnection release];
         [clientPort release];
         ITDebugLog(@"Connection to host failed: %@", host);
-        return NO;
+        return 0;
     NS_ENDHANDLER
     NS_ENDHANDLER
+    
+    if (!clientProxy) {
+        ITDebugLog(@"Null proxy! Couldn't connect!");
+        [self disconnect];
+        return 0;
+    }
+    
+    if ([clientProxy requiresPassword]) {
+        ITDebugLog(@"Server requires password.");
+        //Check to see if a password is set in defaults
+        if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
+            ITDebugLog(@"Asking for password.");
+            if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
+                ITDebugLog(@"Giving up connection attempt.");
+                [self disconnect];
+                return -1;
+            }
+        }
+        
+        //Send the password
+        ITDebugLog(@"Sending password.");
+        while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
+            ITDebugLog(@"Invalid password!");
+            if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
+                ITDebugLog(@"Giving up connection attempt.");
+                [self disconnect];
+                return -1;
+            }
+        }
+    }
+    
     ITDebugLog(@"Connected to host: %@", host);
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
     ITDebugLog(@"Connected to host: %@", host);
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
-    connectedToServer = YES;
-    return YES;
+    return 1;
 }
 
 - (BOOL)disconnect
 }
 
 - (BOOL)disconnect
@@ -154,7 +194,6 @@ static NetworkController *sharedController;
     remoteHost = nil;
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [clientProxy release];
     remoteHost = nil;
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [clientProxy release];
-    [clientConnection invalidate];
     [clientConnection release];
     return YES;
 }
     [clientConnection release];
     return YES;
 }
@@ -163,9 +202,10 @@ static NetworkController *sharedController;
 {
     NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
     unsigned char buffer;
 {
     NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
     unsigned char buffer;
-    NSConnection *testConnection;
-    NSSocketPort *testPort;
-    NSDistantObject *tempProxy;
+    NSConnection *testConnection = nil;
+    NSSocketPort *testPort = nil;
+    NetworkObject *tempProxy;
+    BOOL valid;
     ITDebugLog(@"Checking for shared remote at %@.", host);
     if (fullPass) {
         [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
     ITDebugLog(@"Checking for shared remote at %@.", host);
     if (fullPass) {
         [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
@@ -177,23 +217,41 @@ static NetworkController *sharedController;
     
     NS_DURING
         testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
     
     NS_DURING
         testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
-                                           host:host];
+                                         host:host];
         testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
         testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
-        [testConnection setReplyTimeout:2];
-        tempProxy = [testConnection rootProxy];
-        [testConnection setDelegate:self];
-        [tempProxy sharedRemoteName];
+        [testConnection setReplyTimeout:5];
+        [testConnection setRequestTimeout:5];
+        tempProxy = (NetworkObject *)[testConnection rootProxy];
+        [tempProxy serverName];
+        valid = [tempProxy isValid];
     NS_HANDLER
         ITDebugLog(@"Connection to host failed: %@", host);
     NS_HANDLER
         ITDebugLog(@"Connection to host failed: %@", host);
-        [testConnection invalidate];
         [testConnection release];
         [testPort release];
         return NO;
     NS_ENDHANDLER
         [testConnection release];
         [testPort release];
         return NO;
     NS_ENDHANDLER
-    [testConnection invalidate];
+    
+    if (!tempProxy) {
+        ITDebugLog(@"Null proxy! Couldn't connect!");
+        [testConnection release];
+        [testPort release];
+        return NO;
+    }
     [testConnection release];
     [testPort release];
     [testConnection release];
     [testPort release];
-    return YES;
+    return valid;
+}
+
+- (void)resetServerName
+{
+    if ([self isServerOn]) {
+        [service stop];
+        [service release];
+        service = [[NSNetService alloc] initWithDomain:@""
+                                        type:@"_mttp._tcp."
+                                        name:[[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"]
+                                        port:SERVER_PORT];
+    }
 }
 
 - (BOOL)isServerOn
 }
 
 - (BOOL)isServerOn
@@ -216,9 +274,9 @@ static NetworkController *sharedController;
     return remoteHost;
 }
 
     return remoteHost;
 }
 
-- (ITMTRemote *)sharedRemote
+- (NetworkObject *)networkObject
 {
 {
-    return (ITMTRemote *)clientProxy;
+    return clientProxy;
 }
 
 - (NSArray *)remoteServices
 }
 
 - (NSArray *)remoteServices
@@ -226,21 +284,6 @@ static NetworkController *sharedController;
     return remoteServices;
 }
 
     return remoteServices;
 }
 
-/*- (BOOL)authenticateComponents:(NSArray*)components withData:(NSData *)authenticationData
-{
-    return YES;
-    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableSharingPassword"] || [authenticationData isEqualToData:serverPass]) {
-        return YES;
-    } else {
-        return NO;
-    }
-}
-
-- (NSData *)authenticationDataForComponents:(NSArray *)components
-{
-    return clientPass;
-}*/
-
 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
 {
     ITDebugLog(@"Found service named %@.", [aNetService name]);
 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
 {
     ITDebugLog(@"Found service named %@.", [aNetService name]);
@@ -264,8 +307,10 @@ static NetworkController *sharedController;
 - (void)netServiceDidResolveAddress:(NSNetService *)sender
 {
     ITDebugLog(@"Resolved service named %@.", [sender name]);
 - (void)netServiceDidResolveAddress:(NSNetService *)sender
 {
     ITDebugLog(@"Resolved service named %@.", [sender name]);
-    NSLog(@"Resolved service named %@.", [sender name]);
     [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
     [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
+    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"useSharedPlayer"] && !connectedToServer) {
+        [[MainController sharedController] checkForRemoteServerAndConnectImmediately:NO];
+    }
     [sender stop];
 }
 
     [sender stop];
 }