4 * Rendezvous network controller
6 * Original Author : Kent Sutherland <ksuther@ithinksw.com>
7 * Responsibility : Kent Sutherland <ksuther@ithinksw.com>
9 * Copyright (c) 2003 iThink Software.
14 #import "NetworkController.h"
15 #import "MainController.h"
16 #import "NetworkObject.h"
17 #import <ITFoundation/ITDebug.h>
18 #import <ITFoundation/ITFoundation.h>
20 static NetworkController *sharedController;
22 @implementation NetworkController
24 + (NetworkController *)sharedController
26 return sharedController;
31 if ( (self = [super init]) ) {
32 sharedController = self;
33 browser = [[NSNetServiceBrowser alloc] init];
34 [browser setDelegate:self];
43 [serverConnection release];
47 [clientProxy release];
48 [remoteServices release];
55 - (void)startRemoteServerSearch
57 [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
58 [remoteServices release];
59 remoteServices = [[NSMutableArray alloc] init];
62 - (void)stopRemoteServerSearch
67 - (void)setServerStatus:(BOOL)status
69 if (!serverOn && status) {
70 NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
75 serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
76 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
78 [serverConnection setRootObject:[[NetworkObject alloc] init]];
79 [serverConnection registerName:@"ITMTPlayerHost"];
81 [[serverConnection rootObject] release];
82 [serverConnection release];
84 ITDebugLog(@"Error starting server!");
86 ITDebugLog(@"Started server.");
88 name = @"MenuTunes Shared Player";
90 service = [[NSNetService alloc] initWithDomain:@""
94 fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
96 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
98 serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
104 } else if (serverOn && !status && [serverConnection isValid]) {
107 [serverConnection registerName:nil];
108 [[serverConnection rootObject] release];
109 [serverConnection release];
110 ITDebugLog(@"Stopped server.");
115 - (BOOL)connectToHost:(NSString *)host
117 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
118 unsigned char buffer;
119 ITDebugLog(@"Connecting to host: %@", host);
120 [remoteHost release];
121 remoteHost = [host copy];
123 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
124 [clientPass release];
125 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
130 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
132 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
133 [clientConnection setReplyTimeout:5];
134 clientProxy = [[clientConnection rootProxy] retain];
136 [clientConnection release];
137 [clientPort release];
138 ITDebugLog(@"Connection to host failed: %@", host);
143 ITDebugLog(@"Null proxy! Couldn't connect!");
148 if ([clientProxy requiresPassword]) {
149 ITDebugLog(@"Sending password.");
150 if (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
151 ITDebugLog(@"Invalid password!");
153 if ( NSRunCriticalAlertPanel(@"Invalid Password", @"The MenuTunes server you attempted to connect to rejected your password. Would you like to try to reconnect?.", @"Yes", @"No", nil) == NSOKButton ) {
154 return [self connectToHost:host];
161 ITDebugLog(@"Connected to host: %@", host);
162 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
163 connectedToServer = YES;
169 ITDebugLog(@"Disconnecting from host.");
170 connectedToServer = NO;
171 [remoteHost release];
173 [[NSNotificationCenter defaultCenter] removeObserver:self];
174 [clientProxy release];
175 [clientConnection release];
179 - (BOOL)checkForServerAtHost:(NSString *)host
181 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
182 unsigned char buffer;
183 NSConnection *testConnection;
184 NSSocketPort *testPort;
185 NetworkObject *tempProxy;
186 ITDebugLog(@"Checking for shared remote at %@.", host);
188 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
189 [clientPass release];
190 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
196 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
198 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
199 [testConnection setReplyTimeout:2];
200 tempProxy = (NetworkObject *)[testConnection rootProxy];
201 [tempProxy serverName];
203 ITDebugLog(@"Connection to host failed: %@", host);
204 [testConnection release];
210 ITDebugLog(@"Null proxy! Couldn't connect!");
211 [testConnection release];
215 [testConnection release];
225 - (BOOL)isClientConnected
227 return clientConnected;
230 - (BOOL)isConnectedToServer
232 return connectedToServer;
235 - (NSString *)remoteHost
240 - (NetworkObject *)networkObject
245 - (NSArray *)remoteServices
247 return remoteServices;
250 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
252 ITDebugLog(@"Found service named %@.", [aNetService name]);
253 [remoteServices addObject:aNetService];
254 [aNetService setDelegate:self];
255 [aNetService resolve];
257 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
261 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
263 ITDebugLog(@"Removed service named %@.", [aNetService name]);
264 [remoteServices removeObject:aNetService];
266 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
270 - (void)netServiceDidResolveAddress:(NSNetService *)sender
272 ITDebugLog(@"Resolved service named %@.", [sender name]);
273 NSLog(@"Resolved service named %@.", [sender name]);
274 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
278 - (void)netServiceWillResolve:(NSNetService *)sender
280 ITDebugLog(@"Resolving service named %@.", [sender name]);
283 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
285 ITDebugLog(@"Error resolving service %@.", errorDict);