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"];
76 serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
77 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
79 [serverConnection setRootObject:[[NetworkObject alloc] init]];
80 [serverConnection registerName:@"ITMTPlayerHost"];
82 [[serverConnection rootObject] release];
83 [serverConnection release];
85 ITDebugLog(@"Error starting server!");
87 ITDebugLog(@"Started server.");
89 name = @"MenuTunes Shared Player";
91 service = [[NSNetService alloc] initWithDomain:@""
95 fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
97 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
99 serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
105 } else if (serverOn && !status && [serverConnection isValid]) {
108 [serverConnection registerName:nil];
109 [[serverConnection rootObject] release];
110 [serverConnection release];
111 ITDebugLog(@"Stopped server.");
116 - (int)connectToHost:(NSString *)host
118 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
119 unsigned char buffer;
120 ITDebugLog(@"Connecting to host: %@", host);
121 [remoteHost release];
122 remoteHost = [host copy];
124 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
125 [clientPass release];
126 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
131 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
133 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
134 [clientConnection setReplyTimeout:5];
135 clientProxy = [[clientConnection rootProxy] retain];
137 [clientConnection release];
138 [clientPort release];
139 ITDebugLog(@"Connection to host failed: %@", host);
144 ITDebugLog(@"Null proxy! Couldn't connect!");
149 if ([clientProxy requiresPassword]) {
150 ITDebugLog(@"Server requires password.");
151 //Check to see if a password is set in defaults
152 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
153 ITDebugLog(@"Asking for password.");
154 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
155 ITDebugLog(@"Giving up connection attempt.");
162 ITDebugLog(@"Sending password.");
163 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
164 ITDebugLog(@"Invalid password!");
165 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
166 ITDebugLog(@"Giving up connection attempt.");
173 ITDebugLog(@"Connected to host: %@", host);
174 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
175 connectedToServer = YES;
181 ITDebugLog(@"Disconnecting from host.");
182 connectedToServer = NO;
183 [remoteHost release];
185 [[NSNotificationCenter defaultCenter] removeObserver:self];
186 [clientProxy release];
187 [clientConnection release];
191 - (BOOL)checkForServerAtHost:(NSString *)host
193 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
194 unsigned char buffer;
195 NSConnection *testConnection;
196 NSSocketPort *testPort;
197 NetworkObject *tempProxy;
198 ITDebugLog(@"Checking for shared remote at %@.", host);
200 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
201 [clientPass release];
202 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
208 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
210 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
211 [testConnection setReplyTimeout:2];
212 tempProxy = (NetworkObject *)[testConnection rootProxy];
213 [tempProxy serverName];
215 ITDebugLog(@"Connection to host failed: %@", host);
216 [testConnection release];
222 ITDebugLog(@"Null proxy! Couldn't connect!");
223 [testConnection release];
227 [testConnection release];
237 - (BOOL)isClientConnected
239 return clientConnected;
242 - (BOOL)isConnectedToServer
244 return connectedToServer;
247 - (NSString *)remoteHost
252 - (NetworkObject *)networkObject
257 - (NSArray *)remoteServices
259 return remoteServices;
262 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
264 ITDebugLog(@"Found service named %@.", [aNetService name]);
265 [remoteServices addObject:aNetService];
266 [aNetService setDelegate:self];
267 [aNetService resolve];
269 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
273 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
275 ITDebugLog(@"Removed service named %@.", [aNetService name]);
276 [remoteServices removeObject:aNetService];
278 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
282 - (void)netServiceDidResolveAddress:(NSNetService *)sender
284 ITDebugLog(@"Resolved service named %@.", [sender name]);
285 // NSLog(@"Resolved service named %@.", [sender name]);
286 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
290 - (void)netServiceWillResolve:(NSNetService *)sender
292 ITDebugLog(@"Resolving service named %@.", [sender name]);
295 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
297 ITDebugLog(@"Error resolving service %@.", errorDict);