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];
44 [serverConnection release];
48 [clientProxy release];
49 [remoteServices release];
56 - (void)startRemoteServerSearch
58 [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
59 [remoteServices release];
60 remoteServices = [[NSMutableArray alloc] init];
63 - (void)stopRemoteServerSearch
68 - (void)setServerStatus:(BOOL)status
70 if (!serverOn && status) {
71 NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
77 serverPort = [[[NSSocketPort alloc] initWithTCPPort:SERVER_PORT] autorelease];
78 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
80 clientProxy = [[NetworkObject alloc] init];
81 [serverConnection setRootObject:[clientProxy autorelease]];
82 [serverConnection registerName:@"ITMTPlayerHost"];
84 [[serverConnection rootObject] release];
85 [serverConnection setRootObject:nil];
86 [serverConnection release];
88 ITDebugLog(@"Error starting server!");
91 ITDebugLog(@"Started server.");
93 name = @"MenuTunes Shared Player";
95 service = [[NSNetService alloc] initWithDomain:@""
99 fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
101 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
102 [serverPass release];
103 serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
109 ITDebugLog(@"Server service published.");
110 } else if (serverOn && !status && [serverConnection isValid]) {
113 [serverConnection registerName:nil];
114 [serverConnection release];
115 ITDebugLog(@"Stopped server.");
120 - (int)connectToHost:(NSString *)host
122 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
123 unsigned char buffer;
124 ITDebugLog(@"Connecting to host: %@", host);
125 [remoteHost release];
126 remoteHost = [host copy];
128 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
129 [clientPass release];
130 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
135 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
137 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
138 [clientConnection setReplyTimeout:5];
139 clientProxy = [[clientConnection rootProxy] retain];
140 connectedToServer = YES;
142 [clientConnection release];
143 [clientPort release];
144 ITDebugLog(@"Connection to host failed: %@", host);
149 ITDebugLog(@"Null proxy! Couldn't connect!");
154 if ([clientProxy requiresPassword]) {
155 ITDebugLog(@"Server requires password.");
156 //Check to see if a password is set in defaults
157 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
158 ITDebugLog(@"Asking for password.");
159 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
160 ITDebugLog(@"Giving up connection attempt.");
167 ITDebugLog(@"Sending password.");
168 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
169 ITDebugLog(@"Invalid password!");
170 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
171 ITDebugLog(@"Giving up connection attempt.");
178 ITDebugLog(@"Connected to host: %@", host);
179 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
185 ITDebugLog(@"Disconnecting from host.");
186 connectedToServer = NO;
187 [remoteHost release];
189 [[NSNotificationCenter defaultCenter] removeObserver:self];
190 [clientProxy release];
191 [clientConnection release];
195 - (BOOL)checkForServerAtHost:(NSString *)host
197 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
198 unsigned char buffer;
199 NSConnection *testConnection;
200 NSSocketPort *testPort;
201 NetworkObject *tempProxy;
203 ITDebugLog(@"Checking for shared remote at %@.", host);
205 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
206 [clientPass release];
207 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
213 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
215 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
216 [testConnection setReplyTimeout:2];
217 tempProxy = (NetworkObject *)[testConnection rootProxy];
218 [tempProxy serverName];
219 valid = [tempProxy isValid];
221 ITDebugLog(@"Connection to host failed: %@", host);
222 [testConnection release];
228 ITDebugLog(@"Null proxy! Couldn't connect!");
229 [testConnection release];
233 [testConnection release];
243 - (BOOL)isClientConnected
245 return clientConnected;
248 - (BOOL)isConnectedToServer
250 return connectedToServer;
253 - (NSString *)remoteHost
258 - (NetworkObject *)networkObject
263 - (NSArray *)remoteServices
265 return remoteServices;
268 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
270 ITDebugLog(@"Found service named %@.", [aNetService name]);
271 [remoteServices addObject:aNetService];
272 [aNetService setDelegate:self];
273 [aNetService resolve];
275 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
279 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
281 ITDebugLog(@"Removed service named %@.", [aNetService name]);
282 [remoteServices removeObject:aNetService];
284 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
288 - (void)netServiceDidResolveAddress:(NSNetService *)sender
290 ITDebugLog(@"Resolved service named %@.", [sender name]);
291 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
295 - (void)netServiceWillResolve:(NSNetService *)sender
297 ITDebugLog(@"Resolving service named %@.", [sender name]);
300 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
302 ITDebugLog(@"Error resolving service %@.", errorDict);