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];
46 [clientProxy release];
47 [remoteServices release];
54 - (void)startRemoteServerSearch
56 [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
57 [remoteServices release];
58 remoteServices = [[NSMutableArray alloc] init];
61 - (void)stopRemoteServerSearch
66 - (void)setServerStatus:(BOOL)status
68 if (!serverOn && status) {
69 NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
72 serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
73 serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
75 [serverConnection setRootObject:[[MainController sharedController] currentRemote]];
76 [serverConnection registerName:@"ITMTPlayerHost"];
77 [serverConnection setDelegate:self];
79 ITDebugLog(@"Error starting server!");
81 ITDebugLog(@"Started server.");
83 name = @"MenuTunes Shared Player";
85 service = [[NSNetService alloc] initWithDomain:@""
91 } else if (serverOn && !status && [serverConnection isValid]) {
94 [serverConnection registerName:nil];
95 [serverPort invalidate];
96 [serverConnection invalidate];
97 [serverConnection release];
98 ITDebugLog(@"Stopped server.");
103 - (BOOL)connectToHost:(NSString *)host
105 ITDebugLog(@"Connecting to host: %@", host);
107 clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
109 clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
110 clientProxy = [[clientConnection rootProxy] retain];
112 ITDebugLog(@"Connection to host failed: %@", host);
115 [clientConnection setReplyTimeout:5];
116 ITDebugLog(@"Connected to host: %@", host);
117 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
118 connectedToServer = YES;
124 ITDebugLog(@"Disconnecting from host.");
125 connectedToServer = NO;
126 [[NSNotificationCenter defaultCenter] removeObserver:self];
127 [clientProxy release];
128 [clientConnection invalidate];
129 [clientConnection release];
138 - (BOOL)isClientConnected
140 return clientConnected;
143 - (BOOL)isConnectedToServer
145 return connectedToServer;
148 - (ITMTRemote *)sharedRemote
150 return (ITMTRemote *)clientProxy;
153 - (NSArray *)remoteServices
155 return remoteServices;
158 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
160 ITDebugLog(@"Found service named %@.", [aNetService name]);
161 [remoteServices addObject:aNetService];
162 [aNetService setDelegate:self];
163 [aNetService resolve];
165 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
169 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
171 ITDebugLog(@"Removed service named %@.", [aNetService name]);
172 [remoteServices removeObject:aNetService];
174 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
178 - (void)netServiceDidResolveAddress:(NSNetService *)sender
180 ITDebugLog(@"Resolved service named %@.", [sender name]);
181 NSLog(@"Resolved service named %@.", [sender name]);
182 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
185 - (void)netServiceWillResolve:(NSNetService *)sender
187 ITDebugLog(@"Resolving service named %@.", [sender name]);
190 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
192 ITDebugLog(@"Error resolving service %@.", errorDict);