Merge branch 'master' of git://github.com/ksuther/MenuTunes
[MenuTunes.git] / NetworkController.m
index 70994df..ac10ab6 100755 (executable)
@@ -1,16 +1,3 @@
-/*
- *     MenuTunes
- *  NetworkController
- *    Rendezvous network controller
- *
- *  Original Author : Kent Sutherland <ksuther@ithinksw.com>
- *   Responsibility : Kent Sutherland <ksuther@ithinksw.com>
- *
- *  Copyright (c) 2003 iThink Software.
- *  All Rights Reserved
- *
- */
-
 #import "NetworkController.h"
 #import "MainController.h"
 #import "NetworkObject.h"
 #import "NetworkController.h"
 #import "MainController.h"
 #import "NetworkObject.h"
@@ -33,6 +20,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;
 }
@@ -45,6 +34,8 @@ static NetworkController *sharedController;
     }
     [serverPass release];
     [clientPass release];
     }
     [serverPass release];
     [clientPass release];
+    [serverPort release];
+    [rootObject release];
     [clientProxy release];
     [remoteServices release];
     [browser release];
     [clientProxy release];
     [remoteServices release];
     [browser release];
@@ -69,19 +60,16 @@ static NetworkController *sharedController;
 {
     if (!serverOn && status) {
         NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
 {
     if (!serverOn && status) {
         NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
-        NSPort *serverPort;
         unsigned char buffer;
         NSData *fullPass;
         //Turn on
         NS_DURING
         unsigned char buffer;
         NSData *fullPass;
         //Turn on
         NS_DURING
-            serverPort = [[[NSSocketPort alloc] initWithTCPPort:SERVER_PORT] autorelease];
             serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
                                                      sendPort:serverPort];
             serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
                                                      sendPort:serverPort];
-            clientProxy = [[NetworkObject alloc] init];
-            [serverConnection setRootObject:clientProxy];
+            [serverConnection setRootObject:rootObject];
+            [rootObject makeValid];
             [serverConnection registerName:@"ITMTPlayerHost"];
         NS_HANDLER
             [serverConnection registerName:@"ITMTPlayerHost"];
         NS_HANDLER
-            [clientProxy release];
             [serverConnection setRootObject:nil];
             [serverConnection release];
             [serverPort release];
             [serverConnection setRootObject:nil];
             [serverConnection release];
             [serverPort release];
@@ -97,7 +85,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)];
@@ -110,9 +98,12 @@ static NetworkController *sharedController;
     } else if (serverOn && !status && [serverConnection isValid]) {
         //Turn off
         [service stop];
     } else if (serverOn && !status && [serverConnection isValid]) {
         //Turn off
         [service stop];
-        [clientProxy invalidate];
+        [service release];
+        [rootObject invalidate];
         [serverConnection registerName:nil];
         [serverConnection registerName:nil];
-        [serverConnection setRootObject:nil];
+        [serverConnection invalidate];
+        //[serverConnection setRootObject:nil];
+        //[[serverConnection sendPort] autorelease];
         [serverConnection release];
         ITDebugLog(@"Stopped server.");
         serverOn = NO;
         [serverConnection release];
         ITDebugLog(@"Stopped server.");
         serverOn = NO;
@@ -137,7 +128,7 @@ 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 setReplyTimeout:5];
+        [clientConnection setReplyTimeout:10];
         clientProxy = [[clientConnection rootProxy] retain];
         connectedToServer = YES;
     NS_HANDLER
         clientProxy = [[clientConnection rootProxy] retain];
         connectedToServer = YES;
     NS_HANDLER
@@ -150,7 +141,7 @@ static NetworkController *sharedController;
     if (!clientProxy) {
         ITDebugLog(@"Null proxy! Couldn't connect!");
         [self disconnect];
     if (!clientProxy) {
         ITDebugLog(@"Null proxy! Couldn't connect!");
         [self disconnect];
-        return NO;
+        return 0;
     }
     
     if ([clientProxy requiresPassword]) {
     }
     
     if ([clientProxy requiresPassword]) {
@@ -198,8 +189,8 @@ 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;
+    NSConnection *testConnection = nil;
+    NSSocketPort *testPort = nil;
     NetworkObject *tempProxy;
     BOOL valid;
     ITDebugLog(@"Checking for shared remote at %@.", host);
     NetworkObject *tempProxy;
     BOOL valid;
     ITDebugLog(@"Checking for shared remote at %@.", host);
@@ -215,7 +206,8 @@ static NetworkController *sharedController;
         testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
                                          host:host];
         testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
         testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
                                          host:host];
         testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
-        [testConnection setReplyTimeout:2];
+        [testConnection setReplyTimeout:5];
+        [testConnection setRequestTimeout:5];
         tempProxy = (NetworkObject *)[testConnection rootProxy];
         [tempProxy serverName];
         valid = [tempProxy isValid];
         tempProxy = (NetworkObject *)[testConnection rootProxy];
         [tempProxy serverName];
         valid = [tempProxy isValid];
@@ -226,7 +218,7 @@ static NetworkController *sharedController;
         return NO;
     NS_ENDHANDLER
     
         return NO;
     NS_ENDHANDLER
     
-    if (!clientProxy) {
+    if (!tempProxy) {
         ITDebugLog(@"Null proxy! Couldn't connect!");
         [testConnection release];
         [testPort release];
         ITDebugLog(@"Null proxy! Couldn't connect!");
         [testConnection release];
         [testPort release];
@@ -237,6 +229,18 @@ static NetworkController *sharedController;
     return valid;
 }
 
     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
 {
     return serverOn;
 - (BOOL)isServerOn
 {
     return serverOn;
@@ -272,7 +276,12 @@ static NetworkController *sharedController;
     ITDebugLog(@"Found service named %@.", [aNetService name]);
     [remoteServices addObject:aNetService];
     [aNetService setDelegate:self];
     ITDebugLog(@"Found service named %@.", [aNetService name]);
     [remoteServices addObject:aNetService];
     [aNetService setDelegate:self];
-    [aNetService resolve];
+       //Figure out if it responds to the 10.4 method
+       if ([aNetService respondsToSelector:@selector(resolveWithTimeout:)]) {
+               (void)[aNetService resolveWithTimeout:5.0];
+       } else {
+               [aNetService resolve];
+       }
     if (!moreComing) {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
     }
     if (!moreComing) {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
     }
@@ -291,6 +300,9 @@ static NetworkController *sharedController;
 {
     ITDebugLog(@"Resolved service named %@.", [sender name]);
     [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
 {
     ITDebugLog(@"Resolved service named %@.", [sender name]);
     [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
+    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"useSharedPlayer"] && !connectedToServer) {
+        [[MainController sharedController] checkForRemoteServerAndConnectImmediately:NO];
+    }
     [sender stop];
 }
 
     [sender stop];
 }