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];
82 [serverConnection registerName:@"ITMTPlayerHost"];
84 [clientProxy 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 [clientProxy invalidate];
114 [serverConnection registerName:nil];
115 [serverConnection setRootObject:nil];
116 [serverConnection release];
117 ITDebugLog(@"Stopped server.");
122 - (int)connectToHost:(NSString *)host
124 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
125 unsigned char buffer;
126 ITDebugLog(@"Connecting to host: %@", host);
127 [remoteHost release];
128 remoteHost = [host copy];
130 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
131 [clientPass release];
132 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
137 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
139 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
140 [clientConnection setReplyTimeout:5];
141 clientProxy = [[clientConnection rootProxy] retain];
142 connectedToServer = YES;
144 [clientConnection release];
145 [clientPort release];
146 ITDebugLog(@"Connection to host failed: %@", host);
151 ITDebugLog(@"Null proxy! Couldn't connect!");
156 if ([clientProxy requiresPassword]) {
157 ITDebugLog(@"Server requires password.");
158 //Check to see if a password is set in defaults
159 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
160 ITDebugLog(@"Asking for password.");
161 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
162 ITDebugLog(@"Giving up connection attempt.");
169 ITDebugLog(@"Sending password.");
170 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
171 ITDebugLog(@"Invalid password!");
172 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
173 ITDebugLog(@"Giving up connection attempt.");
180 ITDebugLog(@"Connected to host: %@", host);
181 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
187 ITDebugLog(@"Disconnecting from host.");
188 connectedToServer = NO;
189 [remoteHost release];
191 [[NSNotificationCenter defaultCenter] removeObserver:self];
192 [clientProxy release];
193 [clientConnection release];
197 - (BOOL)checkForServerAtHost:(NSString *)host
199 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
200 unsigned char buffer;
201 NSConnection *testConnection;
202 NSSocketPort *testPort;
203 NetworkObject *tempProxy;
205 ITDebugLog(@"Checking for shared remote at %@.", host);
207 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
208 [clientPass release];
209 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
215 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
217 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
218 [testConnection setReplyTimeout:2];
219 tempProxy = (NetworkObject *)[testConnection rootProxy];
220 [tempProxy serverName];
221 valid = [tempProxy isValid];
223 ITDebugLog(@"Connection to host failed: %@", host);
224 [testConnection release];
230 ITDebugLog(@"Null proxy! Couldn't connect!");
231 [testConnection release];
235 [testConnection release];
245 - (BOOL)isClientConnected
247 return clientConnected;
250 - (BOOL)isConnectedToServer
252 return connectedToServer;
255 - (NSString *)remoteHost
260 - (NetworkObject *)networkObject
265 - (NSArray *)remoteServices
267 return remoteServices;
270 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
272 ITDebugLog(@"Found service named %@.", [aNetService name]);
273 [remoteServices addObject:aNetService];
274 [aNetService setDelegate:self];
275 [aNetService resolve];
277 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
281 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
283 ITDebugLog(@"Removed service named %@.", [aNetService name]);
284 [remoteServices removeObject:aNetService];
286 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
290 - (void)netServiceDidResolveAddress:(NSNetService *)sender
292 ITDebugLog(@"Resolved service named %@.", [sender name]);
293 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
297 - (void)netServiceWillResolve:(NSNetService *)sender
299 ITDebugLog(@"Resolving service named %@.", [sender name]);
302 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
304 ITDebugLog(@"Error resolving service %@.", errorDict);