multithreading in comManager

This commit is contained in:
siqi 2013-06-12 09:01:15 +02:00
parent 22e4465d8c
commit 651a96e3fd
8 changed files with 107 additions and 37 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -48,6 +48,7 @@ enum ConnectionState : NSInteger {
CONNECTED
};
dispatch_queue_t backgroundQueue;
@interface CommunicationManager : NSObject

View File

@ -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];

View File

@ -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;

View File

@ -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

View File

@ -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]];
}

View File

@ -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];
// }]
}