summaryrefslogtreecommitdiff
path: root/ios
diff options
context:
space:
mode:
authorsiqi <me@siqi.fr>2013-06-12 09:01:15 +0200
committersiqi <me@siqi.fr>2013-06-12 09:01:34 +0200
commit651a96e3fde964164f80aa7c83af01168465aa2b (patch)
tree56c080294f91fa7ebe14d53f5f2ec6bef400d4d2 /ios
parent22e4465d8cbc7b8cbc29948705994284c84475a1 (diff)
multithreading in comManager
Diffstat (limited to 'ios')
-rw-r--r--ios/iosremote/iosremote/Communication/Client.h6
-rw-r--r--ios/iosremote/iosremote/Communication/Client.m54
-rw-r--r--ios/iosremote/iosremote/Communication/CommunicationManager.h1
-rw-r--r--ios/iosremote/iosremote/Communication/CommunicationManager.m42
-rw-r--r--ios/iosremote/iosremote/Communication/SlideShow.h2
-rw-r--r--ios/iosremote/iosremote/Communication/SlideShow.m8
-rw-r--r--ios/iosremote/iosremote/libreoffice_sdremoteViewController.m3
-rw-r--r--ios/iosremote/iosremote/slideShowViewController.m28
8 files changed, 107 insertions, 37 deletions
diff --git a/ios/iosremote/iosremote/Communication/Client.h b/ios/iosremote/iosremote/Communication/Client.h
index 94fe6c70749c..45f7e95f295f 100644
--- a/ios/iosremote/iosremote/Communication/Client.h
+++ b/ios/iosremote/iosremote/Communication/Client.h
@@ -13,11 +13,13 @@
@interface Client : NSObject
-@property BOOL ready;
+@property BOOL connected;
@property (nonatomic, strong) NSNumber* pin;
@property (nonatomic, strong) NSString* name;
+@property (nonatomic, weak) Server* server;
--(void) connect;
+- (BOOL) connect;
+- (void) disconnect;
- (id) initWithServer:(Server*)server
managedBy:(CommunicationManager*)manager
diff --git a/ios/iosremote/iosremote/Communication/Client.m b/ios/iosremote/iosremote/Communication/Client.m
index 3b1f1b6b5914..bfa7648e00b7 100644
--- a/ios/iosremote/iosremote/Communication/Client.m
+++ b/ios/iosremote/iosremote/Communication/Client.m
@@ -21,13 +21,12 @@
@property uint mPort;
-@property (nonatomic, weak) Server* server;
@property (nonatomic, weak) CommandInterpreter* receiver;
@property (nonatomic, weak) CommunicationManager* comManager;
@end
-
+NSCondition *connected;
@implementation Client
@@ -37,7 +36,7 @@
@synthesize name = _mName;
@synthesize server = _mServer;
@synthesize comManager = _mComManager;
-@synthesize ready = _mReady;
+@synthesize connected = _mReady;
@synthesize receiver = _receiver;
- (id) initWithServer:(Server*)server
@@ -47,7 +46,8 @@
self = [self init];
if (self)
{
- self.ready = NO;
+ connected = [NSCondition new];
+ self.connected = NO;
self.name = [[UIDevice currentDevice] name];
self.pin = [NSNumber numberWithInteger:[self getPin]];
self.server = server;
@@ -99,14 +99,6 @@
[self.outputStream setDelegate:self];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream open];
-
- // NSLog(@"Stream opened %@ %@", @"iPad", self.mPin);
-
- 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];
}
}
@@ -122,12 +114,22 @@
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
- case NSStreamEventOpenCompleted:
+ case NSStreamEventOpenCompleted:{
NSLog(@"Connection established");
- self.ready = YES;
+ [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];
+ }
+
break;
- case NSStreamEventErrorOccurred:
+ case NSStreamEventErrorOccurred:{
NSLog(@"Connection error occured");
+ [self disconnect];
+ }
break;
case NSStreamEventHasBytesAvailable:
{
@@ -153,7 +155,6 @@
}
NSArray *commands = [str componentsSeparatedByString:@"\n"];
-// NSLog(@"Data Received: %@", commands);
[self.receiver parse:commands];
data = nil;
@@ -167,10 +168,29 @@
}
}
+- (void) disconnect
+{
+ if(self.inputStream == nil && self.outputStream == nil)
+ return;
+ [self.inputStream close];
+ [self.outputStream close];
+ self.inputStream = nil;
+ self.outputStream = nil;
+ self.connected = NO;
+}
-- (void) connect
+- (BOOL) 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/CommunicationManager.h b/ios/iosremote/iosremote/Communication/CommunicationManager.h
index 93046a188a77..f64edb7daebb 100644
--- a/ios/iosremote/iosremote/Communication/CommunicationManager.h
+++ b/ios/iosremote/iosremote/Communication/CommunicationManager.h
@@ -48,6 +48,7 @@ enum ConnectionState : NSInteger {
CONNECTED
};
+dispatch_queue_t backgroundQueue;
@interface CommunicationManager : NSObject
diff --git a/ios/iosremote/iosremote/Communication/CommunicationManager.m b/ios/iosremote/iosremote/Communication/CommunicationManager.m
index a62988611365..fdd4ade8ca1a 100644
--- a/ios/iosremote/iosremote/Communication/CommunicationManager.m
+++ b/ios/iosremote/iosremote/Communication/CommunicationManager.m
@@ -12,11 +12,13 @@
#import "Server.h"
#import "CommandTransmitter.h"
#import "CommandInterpreter.h"
+#import <dispatch/dispatch.h>
@interface CommunicationManager()
@property (nonatomic, strong) Client* client;
@property (nonatomic, strong) CommandInterpreter* interpreter;
+@property (nonatomic, strong) CommandTransmitter* transmitter;
@property (atomic, strong) NSMutableArray* servers;
@end
@@ -27,8 +29,11 @@
@synthesize client = _client;
@synthesize state = _state;
@synthesize interpreter = _interpreter;
+@synthesize transmitter = _transmitter;
@synthesize servers = _servers;
+NSLock *connectionLock;
+
+ (CommunicationManager *)sharedComManager
{
static CommunicationManager *sharedComManager = nil;
@@ -46,13 +51,15 @@
{
self = [super init];
self.state = DISCONNECTED;
+ connectionLock = [NSLock new];
+ backgroundQueue = dispatch_queue_create("org.libreoffice.iosremote", NULL);
return self;
}
- (id) initWithExistingServers
{
self = [self init];
-
+
NSUserDefaults * userDefaluts = [NSUserDefaults standardUserDefaults];
if(!userDefaluts)
@@ -69,6 +76,39 @@
}
}
+- (void) connectToServer:(Server*)server
+{
+ dispatch_async(backgroundQueue, ^(void) {
+ if ([connectionLock tryLock]) {
+ 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;
+ });
+}
+
+- (NSNumber *) getPairingPin{
+ return [self.client pin];
+}
+
+- (NSString *) getPairingDeviceName
+{
+ return [self.client name];
+}
+
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedComManager];
diff --git a/ios/iosremote/iosremote/Communication/SlideShow.h b/ios/iosremote/iosremote/Communication/SlideShow.h
index b43c81001a11..716010063d93 100644
--- a/ios/iosremote/iosremote/Communication/SlideShow.h
+++ b/ios/iosremote/iosremote/Communication/SlideShow.h
@@ -8,11 +8,13 @@
#import <Foundation/Foundation.h>
+#import "slideShowViewController.h"
@interface SlideShow : NSObject
@property uint size;
@property uint currentSlide;
+@property (nonatomic, weak) id delegate;
- (void) putImage: (NSString *)img AtIndex: (uint) index;
- (void) putNotes: (NSString *)notes AtIndex: (uint) index;
diff --git a/ios/iosremote/iosremote/Communication/SlideShow.m b/ios/iosremote/iosremote/Communication/SlideShow.m
index dfecb9f8f71f..7bd8c55266df 100644
--- a/ios/iosremote/iosremote/Communication/SlideShow.m
+++ b/ios/iosremote/iosremote/Communication/SlideShow.m
@@ -9,6 +9,7 @@
#import "SlideShow.h"
#import "Base64.h"
+#import "slideShowViewController.h"
@interface SlideShow()
@@ -21,6 +22,7 @@
@synthesize size = _size;
@synthesize currentSlide = _currentSlide;
+@synthesize delegate = _delegate;
- (SlideShow *) init{
self = [super init];
@@ -35,12 +37,14 @@
NSData* data = [NSData dataWithBase64String:img];
UIImage* image = [UIImage imageWithData:data];
[self.imagesArray insertObject:image atIndex:index];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"IMAGE_READY" object:nil];
+ slideShowViewController* vc = [self delegate];
+ [[vc image] setImage:image];
}
- (void) putNotes: (NSString *)notes AtIndex: (uint) index{
[self.notesArray insertObject:notes atIndex:index];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTE_READY" object:nil];
+ slideShowViewController* vc = [self delegate];
+ [[vc lecturer_notes] loadHTMLString:notes baseURL:nil];
}
- (UIImage *) getImageAtIndex: (uint) index
diff --git a/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m b/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m
index 0f913db85268..a44eacbaefda 100644
--- a/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m
+++ b/ios/iosremote/iosremote/libreoffice_sdremoteViewController.m
@@ -49,6 +49,7 @@
if ([segue.identifier isEqualToString:@"slidesPreviewSegue"]) {
slideShowViewController *destViewController = segue.destinationViewController;
destViewController.slideshow = [self.interpreter slideShow];
+ [destViewController.slideshow setDelegate:destViewController];
}
}
@@ -66,7 +67,7 @@
self.client = [[Client alloc] initWithServer:self.server managedBy:nil interpretedBy:self.interpreter];
[self.client connect];
- if([self.client ready])
+ if([self.client connected])
{
[self.pinLabel setText:[NSString stringWithFormat:@"%@", self.client.pin]];
}
diff --git a/ios/iosremote/iosremote/slideShowViewController.m b/ios/iosremote/iosremote/slideShowViewController.m
index d79cb28a5d98..9f9bf7541288 100644
--- a/ios/iosremote/iosremote/slideShowViewController.m
+++ b/ios/iosremote/iosremote/slideShowViewController.m
@@ -32,20 +32,20 @@
{
[super viewDidLoad];
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
- [self.image setImage:[self.slideshow getImageAtIndex:0]];
- [self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
- self.slideShowImageReadyObserver = [center addObserverForName:@"IMAGE_READY" object:nil
- queue:mainQueue usingBlock:^(NSNotification *note) {
- NSLog(@"Getting image to display: %@", [self.slideshow getImageAtIndex:0]);
- [self.image setImage:[self.slideshow getImageAtIndex:0]];
- }];
- self.slideShowNoteReadyObserver = [center addObserverForName:@"NOTE_READY" object:nil
- queue:mainQueue usingBlock:^(NSNotification *note) {
- NSLog(@"Getting note to display: %@", [self.slideshow getNotesAtIndex:0]);
- [self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
- }];
+// NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+// NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
+// [self.image setImage:[self.slideshow getImageAtIndex:0]];
+// [self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
+// self.slideShowImageReadyObserver = [center addObserverForName:@"IMAGE_READY" object:nil
+// queue:mainQueue usingBlock:^(NSNotification *note) {
+// NSLog(@"Getting image to display: %@", [self.slideshow getImageAtIndex:0]);
+// [self.image setImage:[self.slideshow getImageAtIndex:0]];
+// }];
+// self.slideShowNoteReadyObserver = [center addObserverForName:@"NOTE_READY" object:nil
+// queue:mainQueue usingBlock:^(NSNotification *note) {
+// NSLog(@"Getting note to display: %@", [self.slideshow getNotesAtIndex:0]);
+// [self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
+// }]
}