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!");
88 ITDebugLog(@"Started server.");
90 name = @"MenuTunes Shared Player";
92 service = [[NSNetService alloc] initWithDomain:@""
96 fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
98 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
100 serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
106 ITDebugLog(@"Server service published.");
107 } else if (serverOn && !status && [serverConnection isValid]) {
110 [serverConnection registerName:nil];
111 [[serverConnection rootObject] release];
112 [serverConnection release];
113 ITDebugLog(@"Stopped server.");
118 - (int)connectToHost:(NSString *)host
120 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
121 unsigned char buffer;
122 ITDebugLog(@"Connecting to host: %@", host);
123 [remoteHost release];
124 remoteHost = [host copy];
126 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
127 [clientPass release];
128 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
133 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
135 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
136 [clientConnection setReplyTimeout:5];
137 clientProxy = [[clientConnection rootProxy] retain];
138 connectedToServer = YES;
140 [clientConnection release];
141 [clientPort release];
142 ITDebugLog(@"Connection to host failed: %@", host);
147 ITDebugLog(@"Null proxy! Couldn't connect!");
152 if ([clientProxy requiresPassword]) {
153 ITDebugLog(@"Server requires password.");
154 //Check to see if a password is set in defaults
155 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
156 ITDebugLog(@"Asking for password.");
157 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
158 ITDebugLog(@"Giving up connection attempt.");
165 ITDebugLog(@"Sending password.");
166 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
167 ITDebugLog(@"Invalid password!");
168 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
169 ITDebugLog(@"Giving up connection attempt.");
176 ITDebugLog(@"Connected to host: %@", host);
177 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
183 ITDebugLog(@"Disconnecting from host.");
184 connectedToServer = NO;
185 [remoteHost release];
187 [[NSNotificationCenter defaultCenter] removeObserver:self];
188 [clientProxy release];
189 [clientConnection release];
193 - (BOOL)checkForServerAtHost:(NSString *)host
195 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
196 unsigned char buffer;
197 NSConnection *testConnection;
198 NSSocketPort *testPort;
199 NetworkObject *tempProxy;
200 ITDebugLog(@"Checking for shared remote at %@.", host);
202 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
203 [clientPass release];
204 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
210 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
212 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
213 [testConnection setReplyTimeout:2];
214 tempProxy = (NetworkObject *)[testConnection rootProxy];
215 [tempProxy serverName];
217 ITDebugLog(@"Connection to host failed: %@", host);
218 [testConnection release];
224 ITDebugLog(@"Null proxy! Couldn't connect!");
225 [testConnection release];
229 [testConnection release];
239 - (BOOL)isClientConnected
241 return clientConnected;
244 - (BOOL)isConnectedToServer
246 return connectedToServer;
249 - (NSString *)remoteHost
254 - (NetworkObject *)networkObject
259 - (NSArray *)remoteServices
261 return remoteServices;
264 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
266 ITDebugLog(@"Found service named %@.", [aNetService name]);
267 [remoteServices addObject:aNetService];
268 [aNetService setDelegate:self];
269 [aNetService resolve];
271 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
275 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
277 ITDebugLog(@"Removed service named %@.", [aNetService name]);
278 [remoteServices removeObject:aNetService];
280 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
284 - (void)netServiceDidResolveAddress:(NSNetService *)sender
286 ITDebugLog(@"Resolved service named %@.", [sender name]);
287 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
291 - (void)netServiceWillResolve:(NSNetService *)sender
293 ITDebugLog(@"Resolving service named %@.", [sender name]);
296 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
298 ITDebugLog(@"Error resolving service %@.", errorDict);