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 - (int)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!");
157 ITDebugLog(@"Connected to host: %@", host);
158 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
159 connectedToServer = 1;
165 ITDebugLog(@"Disconnecting from host.");
166 connectedToServer = NO;
167 [remoteHost release];
169 [[NSNotificationCenter defaultCenter] removeObserver:self];
170 [clientProxy release];
171 [clientConnection release];
175 - (BOOL)checkForServerAtHost:(NSString *)host
177 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
178 unsigned char buffer;
179 NSConnection *testConnection;
180 NSSocketPort *testPort;
181 NetworkObject *tempProxy;
182 ITDebugLog(@"Checking for shared remote at %@.", host);
184 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
185 [clientPass release];
186 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
192 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
194 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
195 [testConnection setReplyTimeout:2];
196 tempProxy = (NetworkObject *)[testConnection rootProxy];
197 [tempProxy serverName];
199 ITDebugLog(@"Connection to host failed: %@", host);
200 [testConnection release];
206 ITDebugLog(@"Null proxy! Couldn't connect!");
207 [testConnection release];
211 [testConnection release];
221 - (BOOL)isClientConnected
223 return clientConnected;
226 - (BOOL)isConnectedToServer
228 return connectedToServer;
231 - (NSString *)remoteHost
236 - (NetworkObject *)networkObject
241 - (NSArray *)remoteServices
243 return remoteServices;
246 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
248 ITDebugLog(@"Found service named %@.", [aNetService name]);
249 [remoteServices addObject:aNetService];
250 [aNetService setDelegate:self];
251 [aNetService resolve];
253 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
257 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
259 ITDebugLog(@"Removed service named %@.", [aNetService name]);
260 [remoteServices removeObject:aNetService];
262 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
266 - (void)netServiceDidResolveAddress:(NSNetService *)sender
268 ITDebugLog(@"Resolved service named %@.", [sender name]);
269 NSLog(@"Resolved service named %@.", [sender name]);
270 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
274 - (void)netServiceWillResolve:(NSNetService *)sender
276 ITDebugLog(@"Resolving service named %@.", [sender name]);
279 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
281 ITDebugLog(@"Error resolving service %@.", errorDict);