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 "PreferencesController.h"
18 #import <ITFoundation/ITDebug.h>
19 #import <ITFoundation/ITFoundation.h>
21 static NetworkController *sharedController;
23 @implementation NetworkController
25 + (NetworkController *)sharedController
27 return sharedController;
32 if ( (self = [super init]) ) {
33 sharedController = self;
34 browser = [[NSNetServiceBrowser alloc] init];
35 [browser setDelegate:self];
36 rootObject = [[NetworkObject alloc] init];
37 serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
46 [serverConnection release];
52 [clientProxy release];
53 [remoteServices release];
60 - (void)startRemoteServerSearch
62 [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
63 [remoteServices release];
64 remoteServices = [[NSMutableArray alloc] init];
67 - (void)stopRemoteServerSearch
72 - (void)setServerStatus:(BOOL)status
74 if (!serverOn && status) {
75 NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
80 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
82 [serverConnection setRootObject:rootObject];
83 [rootObject makeValid];
84 [serverConnection registerName:@"ITMTPlayerHost"];
86 [serverConnection setRootObject:nil];
87 [serverConnection release];
89 ITDebugLog(@"Error starting server!");
92 ITDebugLog(@"Started server.");
94 name = @"MenuTunes Shared Player";
96 service = [[NSNetService alloc] initWithDomain:@""
100 fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
102 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
103 [serverPass release];
104 serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
110 ITDebugLog(@"Server service published.");
111 } else if (serverOn && !status && [serverConnection isValid]) {
114 [rootObject invalidate];
115 [serverConnection registerName:nil];
116 [serverConnection invalidate];
117 //[serverConnection setRootObject:nil];
118 //[[serverConnection sendPort] autorelease];
119 [serverConnection release];
120 ITDebugLog(@"Stopped server.");
125 - (int)connectToHost:(NSString *)host
127 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
128 unsigned char buffer;
129 ITDebugLog(@"Connecting to host: %@", host);
130 [remoteHost release];
131 remoteHost = [host copy];
133 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
134 [clientPass release];
135 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
140 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
142 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
143 [clientConnection setReplyTimeout:5];
144 clientProxy = [[clientConnection rootProxy] retain];
145 connectedToServer = YES;
147 [clientConnection release];
148 [clientPort release];
149 ITDebugLog(@"Connection to host failed: %@", host);
154 ITDebugLog(@"Null proxy! Couldn't connect!");
159 if ([clientProxy requiresPassword]) {
160 ITDebugLog(@"Server requires password.");
161 //Check to see if a password is set in defaults
162 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
163 ITDebugLog(@"Asking for password.");
164 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
165 ITDebugLog(@"Giving up connection attempt.");
172 ITDebugLog(@"Sending password.");
173 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
174 ITDebugLog(@"Invalid password!");
175 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
176 ITDebugLog(@"Giving up connection attempt.");
183 ITDebugLog(@"Connected to host: %@", host);
184 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
190 ITDebugLog(@"Disconnecting from host.");
191 connectedToServer = NO;
192 [remoteHost release];
194 [[NSNotificationCenter defaultCenter] removeObserver:self];
195 [clientProxy release];
196 [clientConnection release];
200 - (BOOL)checkForServerAtHost:(NSString *)host
202 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
203 unsigned char buffer;
204 NSConnection *testConnection;
205 NSSocketPort *testPort;
206 NetworkObject *tempProxy;
208 ITDebugLog(@"Checking for shared remote at %@.", host);
210 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
211 [clientPass release];
212 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
218 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
220 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
221 [testConnection setReplyTimeout:2];
222 tempProxy = (NetworkObject *)[testConnection rootProxy];
223 [tempProxy serverName];
224 valid = [tempProxy isValid];
226 ITDebugLog(@"Connection to host failed: %@", host);
227 [testConnection release];
233 ITDebugLog(@"Null proxy! Couldn't connect!");
234 [testConnection release];
238 [testConnection release];
248 - (BOOL)isClientConnected
250 return clientConnected;
253 - (BOOL)isConnectedToServer
255 return connectedToServer;
258 - (NSString *)remoteHost
263 - (NetworkObject *)networkObject
268 - (NSArray *)remoteServices
270 return remoteServices;
273 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
275 ITDebugLog(@"Found service named %@.", [aNetService name]);
276 [remoteServices addObject:aNetService];
277 [aNetService setDelegate:self];
278 [aNetService resolve];
280 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
284 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
286 ITDebugLog(@"Removed service named %@.", [aNetService name]);
287 [remoteServices removeObject:aNetService];
289 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
293 - (void)netServiceDidResolveAddress:(NSNetService *)sender
295 ITDebugLog(@"Resolved service named %@.", [sender name]);
296 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
300 - (void)netServiceWillResolve:(NSNetService *)sender
302 ITDebugLog(@"Resolving service named %@.", [sender name]);
305 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
307 ITDebugLog(@"Error resolving service %@.", errorDict);