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 "netinet/in.h"
18 #import <ITFoundation/ITDebug.h>
19 #import <ITFoundation/ITFoundation.h>
20 #import <ITMTRemote/ITMTRemote.h>
22 static NetworkController *sharedController;
24 @implementation NetworkController
26 + (NetworkController *)sharedController
28 return sharedController;
33 if ( (self = [super init]) ) {
34 sharedController = self;
35 browser = [[NSNetServiceBrowser alloc] init];
36 [browser setDelegate:self];
45 [serverConnection invalidate];
46 [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"];
74 serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
75 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
77 [serverConnection setRootObject:[[MainController sharedController] currentRemote]];
78 [serverConnection registerName:@"ITMTPlayerHost"];
79 [serverConnection setDelegate:self];
81 ITDebugLog(@"Error starting server!");
83 ITDebugLog(@"Started server.");
85 name = @"MenuTunes Shared Player";
87 service = [[NSNetService alloc] initWithDomain:@""
93 } else if (serverOn && !status && [serverConnection isValid]) {
96 [serverConnection registerName:nil];
97 [serverPort invalidate];
98 [serverConnection invalidate];
99 [serverConnection release];
100 ITDebugLog(@"Stopped server.");
105 - (BOOL)connectToHost:(NSString *)host
107 ITDebugLog(@"Connecting to host: %@", host);
109 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
111 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
112 clientProxy = [[clientConnection rootProxy] retain];
114 ITDebugLog(@"Connection to host failed: %@", host);
117 [clientConnection setReplyTimeout:5];
118 ITDebugLog(@"Connected to host: %@", host);
119 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
120 connectedToServer = YES;
126 ITDebugLog(@"Disconnecting from host.");
127 connectedToServer = NO;
128 [[NSNotificationCenter defaultCenter] removeObserver:self];
129 [clientProxy release];
130 [clientConnection invalidate];
131 [clientConnection release];
140 - (BOOL)isClientConnected
142 return clientConnected;
145 - (BOOL)isConnectedToServer
147 return connectedToServer;
150 - (ITMTRemote *)sharedRemote
152 return (ITMTRemote *)clientProxy;
155 - (NSArray *)remoteServices
157 return remoteServices;
160 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
162 [aNetService setDelegate:self];
163 [aNetService resolve];
164 ITDebugLog(@"Found service named %@.", [aNetService name]);
166 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
170 - (void)netServiceDidResolveAddress:(NSNetService *)sender
172 [remoteServices addObject:[NSDictionary dictionaryWithObjectsAndKeys:[sender name], @"name",
173 [NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[sender addresses] objectAtIndex:0] bytes]).sin_addr)], @"ip",
175 ITDebugLog(@"Resolved service named %@.", [sender name]);
177 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
180 - (void)netServiceWillResolve:(NSNetService *)sender
182 ITDebugLog(@"Resolving service named %@.", [sender name]);
185 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
187 ITDebugLog(@"Error resolving service %@.", errorDict);