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 [clientProxy invalidate];
114 [serverConnection registerName:nil];
115 [serverConnection 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;
204 ITDebugLog(@"Checking for shared remote at %@.", host);
206 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
207 [clientPass release];
208 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
214 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
216 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
217 [testConnection setReplyTimeout:2];
218 tempProxy = (NetworkObject *)[testConnection rootProxy];
219 [tempProxy serverName];
220 valid = [tempProxy isValid];
222 ITDebugLog(@"Connection to host failed: %@", host);
223 [testConnection release];
229 ITDebugLog(@"Null proxy! Couldn't connect!");
230 [testConnection release];
234 [testConnection release];
244 - (BOOL)isClientConnected
246 return clientConnected;
249 - (BOOL)isConnectedToServer
251 return connectedToServer;
254 - (NSString *)remoteHost
259 - (NetworkObject *)networkObject
264 - (NSArray *)remoteServices
266 return remoteServices;
269 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
271 ITDebugLog(@"Found service named %@.", [aNetService name]);
272 [remoteServices addObject:aNetService];
273 [aNetService setDelegate:self];
274 [aNetService resolve];
276 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
280 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
282 ITDebugLog(@"Removed service named %@.", [aNetService name]);
283 [remoteServices removeObject:aNetService];
285 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
289 - (void)netServiceDidResolveAddress:(NSNetService *)sender
291 ITDebugLog(@"Resolved service named %@.", [sender name]);
292 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
296 - (void)netServiceWillResolve:(NSNetService *)sender
298 ITDebugLog(@"Resolving service named %@.", [sender name]);
301 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
303 ITDebugLog(@"Error resolving service %@.", errorDict);