1 #import "NetworkController.h"
2 #import "MainController.h"
3 #import "NetworkObject.h"
4 #import "PreferencesController.h"
5 #import <ITFoundation/ITDebug.h>
6 #import <ITFoundation/ITFoundation.h>
8 static NetworkController *sharedController;
10 @implementation NetworkController
12 + (NetworkController *)sharedController
14 return sharedController;
19 if ( (self = [super init]) ) {
20 sharedController = self;
21 browser = [[NSNetServiceBrowser alloc] init];
22 [browser setDelegate:self];
23 rootObject = [[NetworkObject alloc] init];
24 serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
33 [serverConnection release];
39 [clientProxy release];
40 [remoteServices release];
47 - (void)startRemoteServerSearch
49 [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
50 [remoteServices release];
51 remoteServices = [[NSMutableArray alloc] init];
54 - (void)stopRemoteServerSearch
59 - (void)setServerStatus:(BOOL)status
61 if (!serverOn && status) {
62 NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
67 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
69 [serverConnection setRootObject:rootObject];
70 [rootObject makeValid];
71 [serverConnection registerName:@"ITMTPlayerHost"];
73 [serverConnection setRootObject:nil];
74 [serverConnection release];
76 ITDebugLog(@"Error starting server!");
79 ITDebugLog(@"Started server.");
81 name = @"MenuTunes Shared Player";
83 service = [[NSNetService alloc] initWithDomain:@""
87 fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
88 if ([fullPass length]) {
89 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
91 serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
97 ITDebugLog(@"Server service published.");
98 } else if (serverOn && !status && [serverConnection isValid]) {
102 [rootObject invalidate];
103 [serverConnection registerName:nil];
104 [serverConnection invalidate];
105 //[serverConnection setRootObject:nil];
106 //[[serverConnection sendPort] autorelease];
107 [serverConnection release];
108 ITDebugLog(@"Stopped server.");
113 - (int)connectToHost:(NSString *)host
115 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
116 unsigned char buffer;
117 ITDebugLog(@"Connecting to host: %@", host);
118 [remoteHost release];
119 remoteHost = [host copy];
121 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
122 [clientPass release];
123 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
128 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
130 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
131 [clientConnection setReplyTimeout:10];
132 clientProxy = [[clientConnection rootProxy] retain];
133 connectedToServer = YES;
135 [clientConnection release];
136 [clientPort release];
137 ITDebugLog(@"Connection to host failed: %@", host);
142 ITDebugLog(@"Null proxy! Couldn't connect!");
147 if ([clientProxy requiresPassword]) {
148 ITDebugLog(@"Server requires password.");
149 //Check to see if a password is set in defaults
150 if ([[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"] == nil) {
151 ITDebugLog(@"Asking for password.");
152 if (![[PreferencesController sharedPrefs] showPasswordPanel]) {
153 ITDebugLog(@"Giving up connection attempt.");
160 ITDebugLog(@"Sending password.");
161 while (![clientProxy sendPassword:[[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"]]) {
162 ITDebugLog(@"Invalid password!");
163 if (![[PreferencesController sharedPrefs] showInvalidPasswordPanel]) {
164 ITDebugLog(@"Giving up connection attempt.");
171 ITDebugLog(@"Connected to host: %@", host);
172 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
178 ITDebugLog(@"Disconnecting from host.");
179 connectedToServer = NO;
180 [remoteHost release];
182 [[NSNotificationCenter defaultCenter] removeObserver:self];
183 [clientProxy release];
184 [clientConnection release];
188 - (BOOL)checkForServerAtHost:(NSString *)host
190 NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
191 unsigned char buffer;
192 NSConnection *testConnection = nil;
193 NSSocketPort *testPort = nil;
194 NetworkObject *tempProxy;
196 ITDebugLog(@"Checking for shared remote at %@.", host);
198 [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
199 [clientPass release];
200 clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
206 testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
208 testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
209 [testConnection setReplyTimeout:5];
210 [testConnection setRequestTimeout:5];
211 tempProxy = (NetworkObject *)[testConnection rootProxy];
212 [tempProxy serverName];
213 valid = [tempProxy isValid];
215 ITDebugLog(@"Connection to host failed: %@", host);
216 [testConnection release];
222 ITDebugLog(@"Null proxy! Couldn't connect!");
223 [testConnection release];
227 [testConnection release];
232 - (void)resetServerName
234 if ([self isServerOn]) {
237 service = [[NSNetService alloc] initWithDomain:@""
239 name:[[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"]
249 - (BOOL)isClientConnected
251 return clientConnected;
254 - (BOOL)isConnectedToServer
256 return connectedToServer;
259 - (NSString *)remoteHost
264 - (NetworkObject *)networkObject
269 - (NSArray *)remoteServices
271 return remoteServices;
274 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
276 ITDebugLog(@"Found service named %@.", [aNetService name]);
277 [remoteServices addObject:aNetService];
278 [aNetService setDelegate:self];
279 //Figure out if it responds to the 10.4 method
280 if ([aNetService respondsToSelector:@selector(resolveWithTimeout:)]) {
281 (void)[aNetService resolveWithTimeout:5.0];
283 [aNetService resolve];
286 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
290 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
292 ITDebugLog(@"Removed service named %@.", [aNetService name]);
293 [remoteServices removeObject:aNetService];
295 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
299 - (void)netServiceDidResolveAddress:(NSNetService *)sender
301 ITDebugLog(@"Resolved service named %@.", [sender name]);
302 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
303 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"useSharedPlayer"] && !connectedToServer) {
304 [[MainController sharedController] checkForRemoteServerAndConnectImmediately:NO];
309 - (void)netServiceWillResolve:(NSNetService *)sender
311 ITDebugLog(@"Resolving service named %@.", [sender name]);
314 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
316 ITDebugLog(@"Error resolving service %@.", errorDict);