Adding a new proxy object for the networking. It's broken right now, but
[MenuTunes.git] / NetworkController.m
1 /*
2  *      MenuTunes
3  *  NetworkController
4  *    Rendezvous network controller
5  *
6  *  Original Author : Kent Sutherland <ksuther@ithinksw.com>
7  *   Responsibility : Kent Sutherland <ksuther@ithinksw.com>
8  *
9  *  Copyright (c) 2003 iThink Software.
10  *  All Rights Reserved
11  *
12  */
13
14 #import "NetworkController.h"
15 #import "MainController.h"
16 #import "NetworkObject.h"
17 #import <ITFoundation/ITDebug.h>
18 #import <ITFoundation/ITFoundation.h>
19
20 static NetworkController *sharedController;
21
22 @implementation NetworkController
23
24 + (NetworkController *)sharedController
25 {
26     return sharedController;
27 }
28
29 - (id)init
30 {
31     if ( (self = [super init]) ) {
32         sharedController = self;
33         browser = [[NSNetServiceBrowser alloc] init];
34         [browser setDelegate:self];
35     }
36     return self;
37 }
38
39 - (void)dealloc
40 {
41     [self disconnect];
42     if (serverOn) {
43         [serverConnection invalidate];
44         [serverConnection release];
45     }
46     [serverPass release];
47     [clientPass release];
48     [clientProxy release];
49     [remoteServices release];
50     [browser release];
51     [service stop];
52     [service release];
53     [super dealloc];
54 }
55
56 - (void)startRemoteServerSearch
57 {
58     [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
59     [remoteServices release];
60     remoteServices = [[NSMutableArray alloc] init];
61 }
62
63 - (void)stopRemoteServerSearch
64 {
65     [browser stop];
66 }
67
68 - (void)setServerStatus:(BOOL)status
69 {
70     if (!serverOn && status) {
71         NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
72         unsigned char buffer;
73         NSData *fullPass;
74         //Turn on
75         NS_DURING
76             serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
77             serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
78                                                      sendPort:serverPort];
79             [serverConnection setRootObject:[[NetworkObject alloc] init]];
80             [serverConnection registerName:@"ITMTPlayerHost"];
81             [serverConnection setDelegate:self];
82         NS_HANDLER
83             [serverConnection release];
84             [serverPort release];
85             ITDebugLog(@"Error starting server!");
86         NS_ENDHANDLER
87         ITDebugLog(@"Started server.");
88         if (!name) {
89             name = @"MenuTunes Shared Player";
90         }
91         service = [[NSNetService alloc] initWithDomain:@""
92                                         type:@"_mttp._tcp."
93                                         name:name
94                                         port:SERVER_PORT];
95         fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"sharedPlayerPassword"];
96         if (fullPass) {
97             [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
98             [serverPass release];
99             serverPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
100         } else {
101             serverPass = nil;
102         }
103         [service publish];
104         serverOn = YES;
105     } else if (serverOn && !status && [serverConnection isValid]) {
106         //Turn off
107         [service stop];
108         [serverConnection registerName:nil];
109         [[serverConnection rootObject] release];
110         [serverPort invalidate];
111         [serverConnection invalidate];
112         [serverConnection release];
113         ITDebugLog(@"Stopped server.");
114         serverOn = NO;
115     }
116 }
117
118 - (BOOL)connectToHost:(NSString *)host
119 {
120     NSData *fullPass = [[NSUserDefaults standardUserDefaults] dataForKey:@"connectPassword"];
121     unsigned char buffer;
122     ITDebugLog(@"Connecting to host: %@", host);
123     remoteHost = [host copy];
124     if (fullPass) {
125         [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
126         [clientPass release];
127         clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
128     } else {
129         clientPass = nil;
130     }
131     NS_DURING
132         clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
133                                            host:host];
134         clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
135         [clientConnection setDelegate:self];
136         [clientConnection setReplyTimeout:5];
137         clientProxy = [[clientConnection rootProxy] retain];
138     NS_HANDLER
139         [clientConnection release];
140         [clientPort release];
141         ITDebugLog(@"Connection to host failed: %@", host);
142         return NO;
143     NS_ENDHANDLER
144     ITDebugLog(@"Connected to host: %@", host);
145     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disconnect) name:NSConnectionDidDieNotification object:clientConnection];
146     connectedToServer = YES;
147     return YES;
148 }
149
150 - (BOOL)disconnect
151 {
152     ITDebugLog(@"Disconnecting from host.");
153     connectedToServer = NO;
154     [remoteHost release];
155     remoteHost = nil;
156     [[NSNotificationCenter defaultCenter] removeObserver:self];
157     [clientProxy release];
158     [clientConnection invalidate];
159     [clientConnection release];
160     return YES;
161 }
162
163 - (BOOL)checkForServerAtHost:(NSString *)host
164 {
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);
171     if (fullPass) {
172         [fullPass getBytes:&buffer range:NSMakeRange(6, 4)];
173         [clientPass release];
174         clientPass = [[NSData alloc] initWithBytes:&buffer length:strlen(&buffer)];
175     } else {
176         clientPass = nil;
177     }
178     
179     NS_DURING
180         testPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
181                                            host:host];
182         testConnection = [[NSConnection connectionWithReceivePort:nil sendPort:testPort] retain];
183         [testConnection setReplyTimeout:2];
184         tempProxy = [testConnection rootProxy];
185         [testConnection setDelegate:self];
186         [tempProxy serverName];
187     NS_HANDLER
188         ITDebugLog(@"Connection to host failed: %@", host);
189         [testConnection invalidate];
190         [testConnection release];
191         [testPort release];
192         return NO;
193     NS_ENDHANDLER
194     [testConnection invalidate];
195     [testConnection release];
196     [testPort release];
197     return YES;
198 }
199
200 - (BOOL)isServerOn
201 {
202     return serverOn;
203 }
204
205 - (BOOL)isClientConnected
206 {
207     return clientConnected;
208 }
209
210 - (BOOL)isConnectedToServer
211 {
212     return connectedToServer;
213 }
214
215 - (NSString *)remoteHost
216 {
217     return remoteHost;
218 }
219
220 - (NetworkObject *)networkObject
221 {
222     return clientProxy;
223 }
224
225 - (NSArray *)remoteServices
226 {
227     return remoteServices;
228 }
229
230 /*- (BOOL)authenticateComponents:(NSArray*)components withData:(NSData *)authenticationData
231 {
232     return YES;
233     if (![[NSUserDefaults standardUserDefaults] boolForKey:@"enableSharingPassword"] || [authenticationData isEqualToData:serverPass]) {
234         return YES;
235     } else {
236         return NO;
237     }
238 }
239
240 - (NSData *)authenticationDataForComponents:(NSArray *)components
241 {
242     return clientPass;
243 }*/
244
245 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
246 {
247     ITDebugLog(@"Found service named %@.", [aNetService name]);
248     [remoteServices addObject:aNetService];
249     [aNetService setDelegate:self];
250     [aNetService resolve];
251     if (!moreComing) {
252         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
253     }
254 }
255
256 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
257 {
258     ITDebugLog(@"Removed service named %@.", [aNetService name]);
259     [remoteServices removeObject:aNetService];
260     if (!moreComing) {
261         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
262     }
263 }
264
265 - (void)netServiceDidResolveAddress:(NSNetService *)sender
266 {
267     ITDebugLog(@"Resolved service named %@.", [sender name]);
268     NSLog(@"Resolved service named %@.", [sender name]);
269     [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
270     [sender stop];
271 }
272
273 - (void)netServiceWillResolve:(NSNetService *)sender
274 {
275     ITDebugLog(@"Resolving service named %@.", [sender name]);
276 }
277
278 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
279 {
280     ITDebugLog(@"Error resolving service %@.", errorDict);
281 }
282
283 @end