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 <ITFoundation/ITDebug.h>
17 #import <ITFoundation/ITFoundation.h>
18 #import <ITMTRemote/ITMTRemote.h>
20 static NetworkController *sharedController;
22 @implementation NetworkController
24 + (NetworkController *)sharedController
26 return sharedController;
31 if ( (self = [super init]) ) {
32 sharedController = self;
33 browser = [[NSNetServiceBrowser alloc] init];
34 [browser setDelegate:self];
43 [serverConnection invalidate];
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:[[MainController sharedController] currentRemote]];
80 [serverConnection registerName:@"ITMTPlayerHost"];
81 [serverConnection setDelegate:self];
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 [serverPort invalidate];
110 [serverConnection invalidate];
111 [serverConnection release];
112 ITDebugLog(@"Stopped server.");
117 - (BOOL)connectToHost:(NSString *)host
119 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
120 unsigned char buffer;
121 ITDebugLog(@"Connecting to host: %@", host);
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 setDelegate:self];
135 [clientConnection setReplyTimeout:5];
136 clientProxy = [[clientConnection rootProxy] retain];
138 [clientConnection release];
139 [clientPort release];
140 ITDebugLog(@"Connection to host failed: %@", host);
143 ITDebugLog(@"Connected to host: %@", host);
144 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
145 connectedToServer = YES;
151 ITDebugLog(@"Disconnecting from host.");
152 connectedToServer = NO;
153 [remoteHost release];
155 [[NSNotificationCenter defaultCenter] removeObserver:self];
156 [clientProxy release];
157 [clientConnection invalidate];
158 [clientConnection release];
162 - (BOOL)checkForServerAtHost:(NSString *)host
164 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
165 unsigned char buffer;
166 NSConnection *testConnection;
167 NSSocketPort *testPort;
168 NSDistantObject *tempProxy;
169 ITDebugLog(@"Checking for shared remote at %@.", host);
171 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
172 [clientPass release];
173 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
179 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
181 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
182 [testConnection setReplyTimeout:2];
183 tempProxy = [testConnection rootProxy];
184 [testConnection setDelegate:self];
185 [tempProxy sharedRemoteName];
187 ITDebugLog(@"Connection to host failed: %@", host);
188 [testConnection invalidate];
189 [testConnection release];
193 [testConnection invalidate];
194 [testConnection release];
204 - (BOOL)isClientConnected
206 return clientConnected;
209 - (BOOL)isConnectedToServer
211 return connectedToServer;
214 - (NSString *)remoteHost
219 - (ITMTRemote *)sharedRemote
221 return (ITMTRemote *)clientProxy;
224 - (NSArray *)remoteServices
226 return remoteServices;
229 /*- (BOOL)authenticateComponents:(NSArray*)components withData:(NSData *)authenticationData
232 if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableSharingPassword"] || [authenticationData isEqualToData:serverPass]) {
239 - (NSData *)authenticationDataForComponents:(NSArray *)components
244 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
246 ITDebugLog(@"Found service named %@.", [aNetService name]);
247 [remoteServices addObject:aNetService];
248 [aNetService setDelegate:self];
249 [aNetService resolve];
251 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
255 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
257 ITDebugLog(@"Removed service named %@.", [aNetService name]);
258 [remoteServices removeObject:aNetService];
260 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
264 - (void)netServiceDidResolveAddress:(NSNetService *)sender
266 ITDebugLog(@"Resolved service named %@.", [sender name]);
267 NSLog(@"Resolved service named %@.", [sender name]);
268 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
272 - (void)netServiceWillResolve:(NSNetService *)sender
274 ITDebugLog(@"Resolving service named %@.", [sender name]);
277 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
279 ITDebugLog(@"Error resolving service %@.", errorDict);