Added more stuff to network things.
[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 <ITFoundation/ITDebug.h>
17 #import <ITFoundation/ITFoundation.h>
18 #import <ITMTRemote/ITMTRemote.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     [clientProxy release];
47     [remoteServices release];
48     [browser release];
49     [service stop];
50     [service release];
51     [super dealloc];
52 }
53
54 - (void)startRemoteServerSearch
55 {
56     [browser searchForServicesOfType:@"_mttp._tcp." inDomain:@""];
57     [remoteServices release];
58     remoteServices = [[NSMutableArray alloc] init];
59 }
60
61 - (void)stopRemoteServerSearch
62 {
63     [browser stop];
64 }
65
66 - (void)setServerStatus:(BOOL)status
67 {
68     if (!serverOn && status) {
69         NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"sharedPlayerName"];
70         //Turn on
71         NS_DURING
72             serverPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
73             serverConnection = [[NSConnection alloc] initWithReceivePort:serverPort
74                                                      sendPort:serverPort];
75             [serverConnection setRootObject:[[MainController sharedController] currentRemote]];
76             [serverConnection registerName:@"ITMTPlayerHost"];
77             [serverConnection setDelegate:self];
78         NS_HANDLER
79             ITDebugLog(@"Error starting server!");
80         NS_ENDHANDLER
81         ITDebugLog(@"Started server.");
82         if (!name) {
83             name = @"MenuTunes Shared Player";
84         }
85         service = [[NSNetService alloc] initWithDomain:@""
86                                         type:@"_mttp._tcp."
87                                         name:name
88                                         port:SERVER_PORT];
89         [service publish];
90         serverOn = YES;
91     } else if (serverOn && !status && [serverConnection isValid]) {
92         //Turn off
93         [service stop];
94         [serverConnection registerName:nil];
95         [serverPort invalidate];
96         [serverConnection invalidate];
97         [serverConnection release];
98         ITDebugLog(@"Stopped server.");
99         serverOn = NO;
100     }
101 }
102
103 - (BOOL)connectToHost:(NSString *)host
104 {
105     ITDebugLog(@"Connecting to host: %@", host);
106     NS_DURING
107         clientPort = [[NSSocketPort alloc] initRemoteWithTCPPort:SERVER_PORT
108                                            host:host];
109         clientConnection = [[NSConnection connectionWithReceivePort:nil sendPort:clientPort] retain];
110         clientProxy = [[clientConnection rootProxy] retain];
111     NS_HANDLER
112         ITDebugLog(@"Connection to host failed: %@", host);
113         return NO;
114     NS_ENDHANDLER
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;
119     return YES;
120 }
121
122 - (BOOL)disconnect
123 {
124     ITDebugLog(@"Disconnecting from host.");
125     connectedToServer = NO;
126     [[NSNotificationCenter defaultCenter] removeObserver:self];
127     [clientProxy release];
128     [clientConnection invalidate];
129     [clientConnection release];
130     return YES;
131 }
132
133 - (BOOL)isServerOn
134 {
135     return serverOn;
136 }
137
138 - (BOOL)isClientConnected
139 {
140     return clientConnected;
141 }
142
143 - (BOOL)isConnectedToServer
144 {
145     return connectedToServer;
146 }
147
148 - (ITMTRemote *)sharedRemote
149 {
150     return (ITMTRemote *)clientProxy;
151 }
152
153 - (NSArray *)remoteServices
154 {
155     return remoteServices;
156 }
157
158 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
159 {
160     ITDebugLog(@"Found service named %@.", [aNetService name]);
161     [remoteServices addObject:aNetService];
162     [aNetService setDelegate:self];
163     [aNetService resolve];
164     if (!moreComing) {
165         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
166     }
167 }
168
169 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing
170 {
171     ITDebugLog(@"Removed service named %@.", [aNetService name]);
172     [remoteServices removeObject:aNetService];
173     if (!moreComing) {
174         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
175     }
176 }
177
178 - (void)netServiceDidResolveAddress:(NSNetService *)sender
179 {
180     ITDebugLog(@"Resolved service named %@.", [sender name]);
181     NSLog(@"Resolved service named %@.", [sender name]);
182     [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTFoundNetService" object:nil];
183 }
184
185 - (void)netServiceWillResolve:(NSNetService *)sender
186 {
187     ITDebugLog(@"Resolving service named %@.", [sender name]);
188 }
189
190 - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
191 {
192     ITDebugLog(@"Error resolving service %@.", errorDict);
193 }
194
195 @end