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 [clientProxy release];
116 ITDebugLog(@"Stopped server.");
121 - (int)connectToHost:(NSString *)host
123 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
124 unsigned char buffer;
125 ITDebugLog(@"Connecting to host: %@", host);
126 [remoteHost release];
127 remoteHost = [host copy];
129 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
130 [clientPass release];
131 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
136 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
138 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
139 [clientConnection setReplyTimeout:5];
140 clientProxy = [[clientConnection rootProxy] retain];
141 connectedToServer = YES;
143 [clientConnection release];
144 [clientPort release];
145 ITDebugLog(@"Connection to host failed: %@", host);
150 ITDebugLog(@"Null proxy! Couldn't connect!");
155 if ([clientProxy requiresPassword]) {
156 ITDebugLog(@"Server requires password.");
157 //Check to see if a password is set in defaults
158 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
159 ITDebugLog(@"Asking for password.");
160 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
161 ITDebugLog(@"Giving up connection attempt.");
168 ITDebugLog(@"Sending password.");
169 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
170 ITDebugLog(@"Invalid password!");
171 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
172 ITDebugLog(@"Giving up connection attempt.");
179 ITDebugLog(@"Connected to host: %@", host);
180 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
186 ITDebugLog(@"Disconnecting from host.");
187 connectedToServer = NO;
188 [remoteHost release];
190 [[NSNotificationCenter defaultCenter] removeObserver:self];
191 [clientProxy release];
192 [clientConnection release];
196 - (BOOL)checkForServerAtHost:(NSString *)host
198 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
199 unsigned char buffer;
200 NSConnection *testConnection;
201 NSSocketPort *testPort;
202 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];
220 ITDebugLog(@"Connection to host failed: %@", host);
221 [testConnection release];
227 ITDebugLog(@"Null proxy! Couldn't connect!");
228 [testConnection release];
232 [testConnection release];
242 - (BOOL)isClientConnected
244 return clientConnected;
247 - (BOOL)isConnectedToServer
249 return connectedToServer;
252 - (NSString *)remoteHost
257 - (NetworkObject *)networkObject
262 - (NSArray *)remoteServices
264 return remoteServices;
267 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
269 ITDebugLog(@"Found service named %@.", [aNetService name]);
270 [remoteServices addObject:aNetService];
271 [aNetService setDelegate:self];
272 [aNetService resolve];
274 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
278 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
280 ITDebugLog(@"Removed service named %@.", [aNetService name]);
281 [remoteServices removeObject:aNetService];
283 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
287 - (void)netServiceDidResolveAddress:(NSNetService *)sender
289 ITDebugLog(@"Resolved service named %@.", [sender name]);
290 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
294 - (void)netServiceWillResolve:(NSNetService *)sender
296 ITDebugLog(@"Resolving service named %@.", [sender name]);
299 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
301 ITDebugLog(@"Error resolving service %@.", errorDict);