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 <ITFoundation/ITDebug.h>
18 #import <ITFoundation/ITFoundation.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:[[NetworkObject alloc] init]];
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 [[serverConnection rootObject] release];
110 [serverPort invalidate];
111 [serverConnection invalidate];
112 [serverConnection release];
113 ITDebugLog(@"Stopped server.");
118 - (BOOL)connectToHost:(NSString *)host
120 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
121 unsigned char buffer;
122 ITDebugLog(@"Connecting to host: %@", host);
123 remoteHost = [host copy];
125 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
126 [clientPass release];
127 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
132 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
134 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
135 [clientConnection setDelegate:self];
136 [clientConnection setReplyTimeout:5];
137 clientProxy = [[clientConnection rootProxy] retain];
139 [clientConnection release];
140 [clientPort release];
141 ITDebugLog(@"Connection to host failed: %@", host);
144 ITDebugLog(@"Connected to host: %@", host);
145 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
146 connectedToServer = YES;
152 ITDebugLog(@"Disconnecting from host.");
153 connectedToServer = NO;
154 [remoteHost release];
156 [[NSNotificationCenter defaultCenter] removeObserver:self];
157 [clientProxy release];
158 [clientConnection invalidate];
159 [clientConnection release];
163 - (BOOL)checkForServerAtHost:(NSString *)host
165 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
166 unsigned char buffer;
167 NSConnection *testConnection;
168 NSSocketPort *testPort;
169 NetworkObject *tempProxy;
170 ITDebugLog(@"Checking for shared remote at %@.", host);
172 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
173 [clientPass release];
174 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
180 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
182 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
183 [testConnection setReplyTimeout:2];
184 tempProxy = [testConnection rootProxy];
185 [testConnection setDelegate:self];
186 [tempProxy serverName];
188 ITDebugLog(@"Connection to host failed: %@", host);
189 [testConnection invalidate];
190 [testConnection release];
194 [testConnection invalidate];
195 [testConnection release];
205 - (BOOL)isClientConnected
207 return clientConnected;
210 - (BOOL)isConnectedToServer
212 return connectedToServer;
215 - (NSString *)remoteHost
220 - (NetworkObject *)networkObject
225 - (NSArray *)remoteServices
227 return remoteServices;
230 /*- (BOOL)authenticateComponents:(NSArray*)components withData:(NSData *)authenticationData
233 if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableSharingPassword"] || [authenticationData isEqualToData:serverPass]) {
240 - (NSData *)authenticationDataForComponents:(NSArray *)components
245 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
247 ITDebugLog(@"Found service named %@.", [aNetService name]);
248 [remoteServices addObject:aNetService];
249 [aNetService setDelegate:self];
250 [aNetService resolve];
252 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
256 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
258 ITDebugLog(@"Removed service named %@.", [aNetService name]);
259 [remoteServices removeObject:aNetService];
261 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
265 - (void)netServiceDidResolveAddress:(NSNetService *)sender
267 ITDebugLog(@"Resolved service named %@.", [sender name]);
268 NSLog(@"Resolved service named %@.", [sender name]);
269 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
273 - (void)netServiceWillResolve:(NSNetService *)sender
275 ITDebugLog(@"Resolving service named %@.", [sender name]);
278 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
280 ITDebugLog(@"Error resolving service %@.", errorDict);