Permalink
Browse files

Replace the MYBonjourBrowser code on the device side with ServerBrows…

…er by Peter Bakhyryev
  • Loading branch information...
1 parent 7f1afce commit 77d2b22003d940ed9020d20996bb5054ffabf003 Michael Fey committed Aug 11, 2010
@@ -27,19 +27,21 @@
// OTHER DEALINGS IN THE SOFTWARE.
#import "ZSyncShared.h"
+#import "ServerBrowserDelegate.h"
@class ZSyncTouchHandler;
+@class ServerBrowser;
@interface ZSyncService : NSObject
{
NSString *name;
NSString *uuid;
- MYBonjourService *service;
+ NSNetService *service;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *uuid;
-@property (nonatomic, retain) MYBonjourService *service;
+@property (nonatomic, retain) NSNetService *service;
@end
@@ -122,14 +124,14 @@ typedef enum {
ZSyncServerActionDeregister
} ZSyncServerAction;
-@interface ZSyncTouchHandler : NSObject <BLIPConnectionDelegate>
+@interface ZSyncTouchHandler : NSObject <BLIPConnectionDelegate, ServerBrowserDelegate>
{
NSTimer *networkTimer;
NSDate *findServerTimeoutDate;
NSMutableArray *storeFileIdentifiers;
NSMutableArray *availableServers;
- MYBonjourBrowser *_serviceBrowser;
+ ServerBrowser *_serviceBrowser;
BLIPConnection *_connection;
NSInteger majorVersionNumber;
@@ -151,7 +153,7 @@ typedef enum {
}
@property (nonatomic, assign) ZSyncServerAction serverAction;
-@property (nonatomic, retain) MYBonjourBrowser *serviceBrowser;
+@property (nonatomic, retain) ServerBrowser *serviceBrowser;
@property (nonatomic, assign) BLIPConnection *connection;
@property (nonatomic, assign) NSInteger majorVersionNumber;
@property (nonatomic, assign) NSInteger minorVersionNumber;
@@ -170,6 +172,7 @@ typedef enum {
- (void)authenticatePairing:(NSString*)code;
- (void)cancelPairing;
- (void)disconnectPairing;
+- (void)stopRequestingSync;
/*
* When this is called the client will attempt to connect to the server and deregister any sync data.
@@ -28,6 +28,8 @@
#import "ZSyncTouchHandler.h"
#import "ZSyncShared.h"
+#import "ServerBrowser.h"
+
#import "Reachability.h"
@@ -66,6 +68,7 @@ - (void)applicationWillTerminate:(NSNotification*)notification
[[self connection] close];
}
if ([self serviceBrowser]) {
+ [[self serviceBrowser] setDelegate:nil];
[[self serviceBrowser] stop];
[_serviceBrowser release], _serviceBrowser = nil;
}
@@ -87,7 +90,6 @@ - (void)networkTimeout:(NSTimer*)timer
[[self delegate] zSyncServerUnavailable:self];
}
[[timer userInfo] stopNotifer];
- [self setServiceBrowser:nil];
}
- (void)reachabilityChanged:(NSNotification*)notification
@@ -105,12 +107,13 @@ - (void)startServerSearch
{
if ([self serviceBrowser]) {
DLog(@"service browser is not nil");
- return; //Already in the middle of something
+ [[self serviceBrowser] setDelegate:nil];
+ [[self serviceBrowser] stop];
+ [self setServiceBrowser:nil];
}
- MYBonjourBrowser *browser = [[MYBonjourBrowser alloc] initWithServiceType:zsServiceName];
- [self setServiceBrowser:browser];
- [browser release], browser = nil;
+ _serviceBrowser = [[ServerBrowser alloc] init];
+ _serviceBrowser.delegate = self;
Reachability *reachability = [Reachability reachabilityForLocalWiFi];
if ([reachability currentReachabilityStatus] == NotReachable) {
@@ -133,10 +136,7 @@ - (void)startServerSearch
} else {
DLog(@"starting browser");
[[self serviceBrowser] start];
- /// !!!: Temporary test to see if a timeout is the issue
- findServerTimeoutDate = [[NSDate alloc] initWithTimeIntervalSinceNow:300.0f];
}
- [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(services:) userInfo:nil repeats:YES];
}
- (void)requestSync;
@@ -150,6 +150,13 @@ - (void)requestSync;
}
}
+- (void)stopRequestingSync
+{
+ [[self serviceBrowser] setDelegate:nil];
+ [[self serviceBrowser] stop];
+ [self setServerAction:ZSyncServerActionNoActivity];
+}
+
- (void)deregister;
{
DLog(@"deregister request received");
@@ -220,8 +227,8 @@ - (void)cancelPairing;
- (void)requestPairing:(ZSyncService*)server;
{
- MYBonjourService *service = [server service];
- BLIPConnection *conn = [[BLIPConnection alloc] initToBonjourService:service];
+ NSNetService *service = [server service];
+ BLIPConnection *conn = [[BLIPConnection alloc] initToNetService:service];
[self setConnection:conn];
[conn setDelegate:self];
[conn open];
@@ -249,34 +256,19 @@ - (void)authenticatePairing:(NSString*)code;
[[self connection] sendRequest:request];
}
-- (void)beginSyncWithService:(MYBonjourService*)service
+- (void)beginSyncWithService:(NSNetService*)service
{
- BLIPConnection *conn = [[BLIPConnection alloc] initToBonjourService:service];
+ BLIPConnection *conn = [[BLIPConnection alloc] initToNetService:service];
[self setConnection:conn];
[conn setDelegate:self];
[conn open];
[conn release], conn = nil;
}
-- (void)services:(NSTimer*)timer
+#pragma mark -
+#pragma mark ServerBrowserDelegate methods
+- (void)updateServerList
{
- if (![[_serviceBrowser services] count]) {
- if ([findServerTimeoutDate earlierDate:[NSDate date]] == findServerTimeoutDate) {
- [findServerTimeoutDate release], findServerTimeoutDate = nil;
- [timer invalidate];
- [[self delegate] zSyncServerUnavailable:self];
- [self setServerAction:ZSyncServerActionNoActivity];
- [_serviceBrowser stop];
- [_serviceBrowser release], _serviceBrowser = nil;
- }
- return;
- }
-
- DLog(@"server list found");
-
- [timer invalidate];
- [findServerTimeoutDate release], findServerTimeoutDate = nil;
-
NSString *serverUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID];
@try {
@@ -285,7 +277,7 @@ - (void)services:(NSTimer*)timer
return;
}
- for (MYBonjourService *service in [_serviceBrowser services]) {
+ for (NSNetService *service in [_serviceBrowser servers]) {
NSString *serverName = [service name];
NSArray *components = [serverName componentsSeparatedByString:zsServerNameSeperator];
ZAssert([components count] == 2,@"Wrong number of components: %i\n%@", [components count], serverName);
@@ -302,11 +294,12 @@ - (void)services:(NSTimer*)timer
[[self delegate] zSyncServerUnavailable:self];
[self setServerAction:ZSyncServerActionNoActivity];
} @finally {
- [_serviceBrowser stop];
- [_serviceBrowser release], _serviceBrowser = nil;
+ // Not sure what to put in here, we don't need to stop the service browser each time anymore.
}
}
+#pragma mark -
+
- (void)registerDelegate:(id<ZSyncDelegate>)delegate withPersistentStoreCoordinator:(NSPersistentStoreCoordinator*)coordinator;
{
[self setDelegate:delegate];
@@ -434,24 +427,23 @@ - (void)completeSync
- (void)startBrowser;
{
if (_serviceBrowser) return;
- _serviceBrowser = [[MYBonjourBrowser alloc] initWithServiceType:zsServiceName];
- [_serviceBrowser start];
-
- [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(services:) userInfo:nil repeats:YES];
+ _serviceBrowser = [[ServerBrowser alloc] init];
+ _serviceBrowser.delegate = self;
+ [_serviceBrowser start];
}
- (NSArray*)availableServers;
{
NSMutableSet *set = [NSMutableSet set];
- for (MYBonjourService *bonjourService in [_serviceBrowser services]) {
+ for (NSNetService *bonjourService in [_serviceBrowser servers]) {
NSString *serverName = [bonjourService name];
NSArray *components = [serverName componentsSeparatedByString:zsServerNameSeperator];
- if (!components || [components count] != 2) {
- NSLog(@"Wrong number of components: %i\n%@", [components count], serverName);
- continue;
- }
+ if (!components || [components count] != 2) {
+ NSLog(@"Wrong number of components: %i\n%@", [components count], serverName);
+ continue;
+ }
- ZAssert([components count] == 2,@"Wrong number of components: %i\n%@", [components count], serverName);
+ ZAssert([components count] == 2,@"Wrong number of components: %i\n%@", [components count], serverName);
NSString *serverUUID = [components objectAtIndex:1];
serverName = [components objectAtIndex:0];
@@ -484,6 +476,7 @@ - (void)sendUploadComplete
- (void)uploadDataToServer;
{
+ [[self serviceBrowser] setDelegate:nil];
[[self serviceBrowser] stop];
[self setServiceBrowser:nil];
View
@@ -0,0 +1,47 @@
+//
+// ServerBrowser.h
+// Chatty
+//
+// Copyright (c) 2009 Peter Bakhyryev <peter@byteclub.com>, ByteClub LLC
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+
+#import <Foundation/Foundation.h>
+
+@class ServerBrowserDelegate;
+
+@interface ServerBrowser : NSObject <NSNetServiceBrowserDelegate> {
+ NSNetServiceBrowser* netServiceBrowser;
+ NSMutableArray* servers;
+ id<ServerBrowserDelegate> delegate;
+}
+
+@property(nonatomic,readonly) NSArray* servers;
+@property(nonatomic,retain) id<ServerBrowserDelegate> delegate;
+
+// Start browsing for Bonjour services
+- (BOOL)start;
+
+// Stop everything
+- (void)stop;
+
+@end
Oops, something went wrong.

0 comments on commit 77d2b22

Please sign in to comment.