diff options
author | siqi <me@siqi.fr> | 2013-06-15 14:47:39 +0200 |
---|---|---|
committer | siqi <me@siqi.fr> | 2013-06-18 00:20:01 +0200 |
commit | 894d19ab772a9f0705b97c1490bedc667c0fae5c (patch) | |
tree | a506a97747af487885d7488c2933ab21ac8c9108 /ios | |
parent | d1bb12701c1db47ab6a1f68070afc4dae22f4648 (diff) |
seems somthing get blocked...
Diffstat (limited to 'ios')
12 files changed, 96 insertions, 80 deletions
diff --git a/ios/iosremote/iosremote/Communication/Client.h b/ios/iosremote/iosremote/Communication/Client.h index 45f7e95f295f..51674fd6b3d7 100644 --- a/ios/iosremote/iosremote/Communication/Client.h +++ b/ios/iosremote/iosremote/Communication/Client.h @@ -7,9 +7,10 @@ */ #import <Foundation/Foundation.h> -#import "Server.h" -#import "CommunicationManager.h" -#import "CommandInterpreter.h" + +@class Server; +@class CommunicationManager; +@class CommandInterpreter; @interface Client : NSObject @@ -18,7 +19,7 @@ @property (nonatomic, strong) NSString* name; @property (nonatomic, weak) Server* server; -- (BOOL) connect; +- (void) connect; - (void) disconnect; - (id) initWithServer:(Server*)server diff --git a/ios/iosremote/iosremote/Communication/Client.m b/ios/iosremote/iosremote/Communication/Client.m index bfa7648e00b7..e1fe1edf1d46 100644 --- a/ios/iosremote/iosremote/Communication/Client.m +++ b/ios/iosremote/iosremote/Communication/Client.m @@ -20,14 +20,11 @@ @property (nonatomic, strong) NSOutputStream* outputStream; @property uint mPort; - @property (nonatomic, weak) CommandInterpreter* receiver; @property (nonatomic, weak) CommunicationManager* comManager; @end -NSCondition *connected; - @implementation Client @synthesize inputStream = _mInputStream; @@ -39,6 +36,7 @@ NSCondition *connected; @synthesize connected = _mReady; @synthesize receiver = _receiver; + - (id) initWithServer:(Server*)server managedBy:(CommunicationManager*)manager interpretedBy:(CommandInterpreter*)receiver @@ -46,7 +44,6 @@ NSCondition *connected; self = [self init]; if (self) { - connected = [NSCondition new]; self.connected = NO; self.name = [[UIDevice currentDevice] name]; self.pin = [NSNumber numberWithInteger:[self getPin]]; @@ -111,24 +108,25 @@ NSCondition *connected; [self.outputStream write:(uint8_t *)[data bytes] maxLength:[data length]]; } +int count = 0; + - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { - switch(eventCode) { case NSStreamEventOpenCompleted:{ NSLog(@"Connection established"); - [connected lock]; - NSArray *temp = [[NSArray alloc]initWithObjects:@"LO_SERVER_CLIENT_PAIR\n", self.name, @"\n", self.pin, @"\n\n", nil]; - NSString *command = [temp componentsJoinedByString:@""]; - [self sendCommand:command]; self.connected = YES; - [connected signal]; - [connected unlock]; + if (count == 1) { + [[NSNotificationCenter defaultCenter]postNotificationName:@"connection.status.connected" object:nil]; + } else { + count++; + } + } - break; case NSStreamEventErrorOccurred:{ NSLog(@"Connection error occured"); [self disconnect]; + [[NSNotificationCenter defaultCenter]postNotificationName:@"connection.status.disconnected" object:nil]; } break; case NSStreamEventHasBytesAvailable: @@ -179,18 +177,9 @@ NSCondition *connected; self.connected = NO; } -- (BOOL) connect +- (void) connect { [self streamOpenWithIp:self.server.serverAddress withPortNumber:self.mPort]; - [connected lock]; - if([connected waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]]){ - [connected unlock]; - return YES; - } else { - [self disconnect]; - [connected unlock]; - return NO; - } } diff --git a/ios/iosremote/iosremote/Communication/CommandInterpreter.h b/ios/iosremote/iosremote/Communication/CommandInterpreter.h index faa07cf66d88..287cdc598b2f 100644 --- a/ios/iosremote/iosremote/Communication/CommandInterpreter.h +++ b/ios/iosremote/iosremote/Communication/CommandInterpreter.h @@ -8,7 +8,8 @@ #import <Foundation/Foundation.h> -#import "SlideShow.h" + +@class SlideShow; @interface CommandInterpreter : NSObject diff --git a/ios/iosremote/iosremote/Communication/CommandTransmitter.h b/ios/iosremote/iosremote/Communication/CommandTransmitter.h index 05ddc241d10e..39f85a8f421b 100644 --- a/ios/iosremote/iosremote/Communication/CommandTransmitter.h +++ b/ios/iosremote/iosremote/Communication/CommandTransmitter.h @@ -7,7 +7,9 @@ // #import <Foundation/Foundation.h> -#import "Client.h" + +@class Client; + @interface CommandTransmitter : NSObject diff --git a/ios/iosremote/iosremote/Communication/CommandTransmitter.m b/ios/iosremote/iosremote/Communication/CommandTransmitter.m index ae7c12a90227..cbf0d27d8d7c 100644 --- a/ios/iosremote/iosremote/Communication/CommandTransmitter.m +++ b/ios/iosremote/iosremote/Communication/CommandTransmitter.m @@ -24,6 +24,11 @@ { self = [self init]; self.client = client; + + NSArray *temp = [[NSArray alloc]initWithObjects:@"LO_SERVER_CLIENT_PAIR\n", self.client.name, @"\n", self.client.pin, @"\n\n", nil]; + NSString *command = [temp componentsJoinedByString:@""]; + + [self.client sendCommand:command]; return self; } diff --git a/ios/iosremote/iosremote/Communication/CommunicationManager.h b/ios/iosremote/iosremote/Communication/CommunicationManager.h index f64edb7daebb..a5393d94877d 100644 --- a/ios/iosremote/iosremote/Communication/CommunicationManager.h +++ b/ios/iosremote/iosremote/Communication/CommunicationManager.h @@ -8,9 +8,10 @@ #import <Foundation/Foundation.h> -#import "Client.h" -#import "Server.h" -#import "CommandInterpreter.h" + +@class Client; +@class Server; +@class CommandInterpreter; #define MSG_SLIDESHOW_STARTED @"SLIDESHOW_STARTED" #define MSG_SLIDE_CHANGED @"SLIDE_CHANGED" @@ -48,10 +49,11 @@ enum ConnectionState : NSInteger { CONNECTED }; -dispatch_queue_t backgroundQueue; - @interface CommunicationManager : NSObject +- (void) connectToServer:(Server*)server; + @property ConnectionState state; +@property (nonatomic, strong) id delegate; @end diff --git a/ios/iosremote/iosremote/Communication/CommunicationManager.m b/ios/iosremote/iosremote/Communication/CommunicationManager.m index fdd4ade8ca1a..20b234c08ff6 100644 --- a/ios/iosremote/iosremote/Communication/CommunicationManager.m +++ b/ios/iosremote/iosremote/Communication/CommunicationManager.m @@ -12,6 +12,7 @@ #import "Server.h" #import "CommandTransmitter.h" #import "CommandInterpreter.h" +#import "libreoffice_sdremoteViewController.h" #import <dispatch/dispatch.h> @interface CommunicationManager() @@ -19,7 +20,9 @@ @property (nonatomic, strong) Client* client; @property (nonatomic, strong) CommandInterpreter* interpreter; @property (nonatomic, strong) CommandTransmitter* transmitter; -@property (atomic, strong) NSMutableArray* servers; +@property (atomic, strong) NSMutableSet* servers; +@property (nonatomic, strong) id connectionConnectedObserver; +@property (nonatomic, strong) id connectionDisconnectedObserver; @end @@ -31,8 +34,9 @@ @synthesize interpreter = _interpreter; @synthesize transmitter = _transmitter; @synthesize servers = _servers; - -NSLock *connectionLock; +@synthesize delegate = _delegate; +@synthesize connectionConnectedObserver = _connectionConnectedObserver; +@synthesize connectionDisconnectedObserver = _connectionDisconnectedObserver; + (CommunicationManager *)sharedComManager { @@ -47,15 +51,40 @@ NSLock *connectionLock; return sharedComManager; } + +- (void) connectionStatusHandler:(NSNotification *)note +{ + if([[note name] isEqualToString:@"connection.status.connected"]){ + NSLog(@"Connected"); + self.transmitter = [[CommandTransmitter alloc] initWithClient:self.client]; + self.state = CONNECTED; + [self.delegate setPinLabelText:[NSString stringWithFormat:@"%@", [self getPairingPin]]]; + } else if ([[note name] isEqualToString:@"connection.status.disconnected"]){ + NSLog(@"Connection Failed"); + self.state = DISCONNECTED; + [self.client disconnect]; + } +} + - (id) init { self = [super init]; self.state = DISCONNECTED; - connectionLock = [NSLock new]; - backgroundQueue = dispatch_queue_create("org.libreoffice.iosremote", NULL); + + [[NSNotificationCenter defaultCenter]addObserver: self + selector: @selector(connectionStatusHandler:) + name: @"connection.status.connected" + object: nil]; + [[NSNotificationCenter defaultCenter]addObserver: self + selector: @selector(connectionStatusHandler:) + name: @"connection.status.disconnected" + object: nil]; + return self; } + + - (id) initWithExistingServers { self = [self init]; @@ -70,36 +99,28 @@ NSLock *connectionLock; { NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingExistingServers]; if (oldSavedArray != nil) - self.servers = [[NSMutableArray alloc] initWithArray:oldSavedArray]; + self.servers = [[NSMutableSet alloc] initWithArray:oldSavedArray]; else - self.servers = [[NSMutableArray alloc] init]; + self.servers = [[NSMutableSet alloc] init]; } + return self; } - (void) connectToServer:(Server*)server { - dispatch_async(backgroundQueue, ^(void) { - if ([connectionLock tryLock]) { + [self.servers addObject:server]; + if (self.state == CONNECTING || self.state == CONNECTED) { + return; + } else { self.state = CONNECTING; [self.client disconnect]; // initialise it with a given server self.client = [[Client alloc]initWithServer:server managedBy:self interpretedBy:self.interpreter]; - if([self.client connect]){ - self.state = CONNECTED; - self.transmitter = [[CommandTransmitter alloc] initWithClient:self.client]; - } - else{ - // streams closing is handled by client itself in case of connection failure - self.state = DISCONNECTED; - } - [connectionLock unlock]; - } - else - // Already a threading working on that ... and that thread will unlock in 5 seconds anyway, so just return for now. - return; - }); + [self.client connect]; + } } + - (NSNumber *) getPairingPin{ return [self.client pin]; } diff --git a/ios/iosremote/iosremote/Communication/SlideShow.h b/ios/iosremote/iosremote/Communication/SlideShow.h index 716010063d93..98d14d9003ee 100644 --- a/ios/iosremote/iosremote/Communication/SlideShow.h +++ b/ios/iosremote/iosremote/Communication/SlideShow.h @@ -8,7 +8,8 @@ #import <Foundation/Foundation.h> -#import "slideShowViewController.h" + +@class slideShowViewController; @interface SlideShow : NSObject diff --git a/ios/iosremote/iosremote/en.lproj/MainStoryboard_iPad.storyboard b/ios/iosremote/iosremote/en.lproj/MainStoryboard_iPad.storyboard index 4dc5762cb2d0..a530cbc4674f 100644 --- a/ios/iosremote/iosremote/en.lproj/MainStoryboard_iPad.storyboard +++ b/ios/iosremote/iosremote/en.lproj/MainStoryboard_iPad.storyboard @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8" standalone="no"?> -<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12D78" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" initialViewController="KFV-Ae-zm8"> +<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12E55" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" initialViewController="KFV-Ae-zm8"> <dependencies> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/> </dependencies> @@ -12,7 +12,7 @@ <rect key="frame" x="0.0" y="64" width="768" height="960"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <subviews> - <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="172.25.19.11" borderStyle="roundedRect" minimumFontSize="17" id="9w1-Ym-HcF"> + <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="192.168.40.101" borderStyle="roundedRect" minimumFontSize="17" id="9w1-Ym-HcF"> <rect key="frame" x="234" y="402" width="301" height="30"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <fontDescription key="fontDescription" type="system" pointSize="14"/> @@ -130,11 +130,11 @@ <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <subviews> <imageView userInteractionEnabled="NO" contentMode="scaleToFill" id="T6z-xu-j8h"> - <rect key="frame" x="0.0" y="0.0" width="768" height="458"/> + <rect key="frame" x="14" y="20" width="741" height="442"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> </imageView> <webView contentMode="scaleToFill" id="y0E-Wp-yUc"> - <rect key="frame" x="20" y="492" width="728" height="378"/> + <rect key="frame" x="20" y="513" width="728" height="378"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" heightSizable="YES"/> <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/> </webView> diff --git a/ios/iosremote/iosremote/libreoffice_sdremoteViewController.h b/ios/iosremote/iosremote/libreoffice_sdremoteViewController.h index 504c902a3d50..e1bbb490aa0a 100644 --- a/ios/iosremote/iosremote/libreoffice_sdremoteViewController.h +++ b/ios/iosremote/iosremote/libreoffice_sdremoteViewController.h @@ -11,9 +11,9 @@ @interface libreoffice_sdremoteViewController : UIViewController - (IBAction)connectToServer:(id)sender; +- (void)setPinLabelText:(NSString*)text; @property (weak, nonatomic) IBOutlet UILabel *pinLabel; - @property (weak, nonatomic) IBOutlet UITextField *ipAddressTextEdit; @end diff --git a/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m b/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m index a44eacbaefda..d7d1fc63c1d8 100644 --- a/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m +++ b/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m @@ -9,15 +9,14 @@ #import "libreoffice_sdremoteViewController.h" #import "Server.h" -#import "Client.h" #import "slideShowViewController.h" +#import "CommunicationManager.h" @interface libreoffice_sdremoteViewController () // For debug use, will use a manager to manage server and client instead @property (nonatomic, strong) Server* server; -@property (nonatomic, strong) Client* client; -@property (nonatomic, strong) CommandInterpreter * interpreter; +@property (nonatomic, strong) CommunicationManager *comManager; @property (nonatomic, weak) NSNotificationCenter* center; @property (nonatomic, strong) id slideShowPreviewStartObserver; @@ -26,9 +25,8 @@ @implementation libreoffice_sdremoteViewController @synthesize server = _server; -@synthesize client = _client; @synthesize center = _center; -@synthesize interpreter = _interpreter; +@synthesize comManager = _comManager; @synthesize slideShowPreviewStartObserver = _slideShowPreviewStartObserver; - (void)viewDidLoad @@ -48,7 +46,6 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"slidesPreviewSegue"]) { slideShowViewController *destViewController = segue.destinationViewController; - destViewController.slideshow = [self.interpreter slideShow]; [destViewController.slideshow setDelegate:destViewController]; } } @@ -62,22 +59,19 @@ - (IBAction)connectToServer:(id)sender { NSString * address = [self.ipAddressTextEdit text]; - self.interpreter = [[CommandInterpreter alloc] init]; - self.server = [[Server alloc] initWithProtocol:NETWORK atAddress:address ofName:@"Server"]; - self.client = [[Client alloc] initWithServer:self.server managedBy:nil interpretedBy:self.interpreter]; - [self.client connect]; - - if([self.client connected]) - { - [self.pinLabel setText:[NSString stringWithFormat:@"%@", self.client.pin]]; - } + self.comManager = [[CommunicationManager alloc] init]; + self.server = [[Server alloc] initWithProtocol:NETWORK atAddress:address ofName:@"Macbook Pro Retina"]; + [self.comManager setDelegate:self]; + [self.comManager connectToServer:self.server]; } - - (void)viewDidUnload { [self setIpAddressTextEdit:nil]; [self setPinLabel:nil]; - [self setPinLabel:nil]; [super viewDidUnload]; } + +- (void)setPinLabelText:(NSString *)text{ + [self.pinLabel setText:text]; +} @end diff --git a/ios/iosremote/iosremote/slideShowViewController.h b/ios/iosremote/iosremote/slideShowViewController.h index 314bd12ead12..c5391b40e8f1 100644 --- a/ios/iosremote/iosremote/slideShowViewController.h +++ b/ios/iosremote/iosremote/slideShowViewController.h @@ -14,7 +14,7 @@ @property (weak, nonatomic) IBOutlet UIImageView *image; @property (weak, nonatomic) IBOutlet UIWebView *lecturer_notes; -@property (nonatomic, strong) SlideShow *slideshow; +@property (nonatomic, strong) SlideShow* slideshow; @property (nonatomic, strong) id slideShowImageReadyObserver; @property (nonatomic, strong) id slideShowNoteReadyObserver; |