multithreading in comManager
This commit is contained in:
parent
22e4465d8c
commit
651a96e3fd
@ -13,11 +13,13 @@
|
|||||||
|
|
||||||
@interface Client : NSObject
|
@interface Client : NSObject
|
||||||
|
|
||||||
@property BOOL ready;
|
@property BOOL connected;
|
||||||
@property (nonatomic, strong) NSNumber* pin;
|
@property (nonatomic, strong) NSNumber* pin;
|
||||||
@property (nonatomic, strong) NSString* name;
|
@property (nonatomic, strong) NSString* name;
|
||||||
|
@property (nonatomic, weak) Server* server;
|
||||||
|
|
||||||
-(void) connect;
|
- (BOOL) connect;
|
||||||
|
- (void) disconnect;
|
||||||
|
|
||||||
- (id) initWithServer:(Server*)server
|
- (id) initWithServer:(Server*)server
|
||||||
managedBy:(CommunicationManager*)manager
|
managedBy:(CommunicationManager*)manager
|
||||||
|
@ -21,13 +21,12 @@
|
|||||||
|
|
||||||
@property uint mPort;
|
@property uint mPort;
|
||||||
|
|
||||||
@property (nonatomic, weak) Server* server;
|
|
||||||
@property (nonatomic, weak) CommandInterpreter* receiver;
|
@property (nonatomic, weak) CommandInterpreter* receiver;
|
||||||
@property (nonatomic, weak) CommunicationManager* comManager;
|
@property (nonatomic, weak) CommunicationManager* comManager;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
NSCondition *connected;
|
||||||
|
|
||||||
@implementation Client
|
@implementation Client
|
||||||
|
|
||||||
@ -37,7 +36,7 @@
|
|||||||
@synthesize name = _mName;
|
@synthesize name = _mName;
|
||||||
@synthesize server = _mServer;
|
@synthesize server = _mServer;
|
||||||
@synthesize comManager = _mComManager;
|
@synthesize comManager = _mComManager;
|
||||||
@synthesize ready = _mReady;
|
@synthesize connected = _mReady;
|
||||||
@synthesize receiver = _receiver;
|
@synthesize receiver = _receiver;
|
||||||
|
|
||||||
- (id) initWithServer:(Server*)server
|
- (id) initWithServer:(Server*)server
|
||||||
@ -47,7 +46,8 @@
|
|||||||
self = [self init];
|
self = [self init];
|
||||||
if (self)
|
if (self)
|
||||||
{
|
{
|
||||||
self.ready = NO;
|
connected = [NSCondition new];
|
||||||
|
self.connected = NO;
|
||||||
self.name = [[UIDevice currentDevice] name];
|
self.name = [[UIDevice currentDevice] name];
|
||||||
self.pin = [NSNumber numberWithInteger:[self getPin]];
|
self.pin = [NSNumber numberWithInteger:[self getPin]];
|
||||||
self.server = server;
|
self.server = server;
|
||||||
@ -99,14 +99,6 @@
|
|||||||
[self.outputStream setDelegate:self];
|
[self.outputStream setDelegate:self];
|
||||||
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||||
[self.outputStream open];
|
[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 {
|
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
|
||||||
|
|
||||||
switch(eventCode) {
|
switch(eventCode) {
|
||||||
case NSStreamEventOpenCompleted:
|
case NSStreamEventOpenCompleted:{
|
||||||
NSLog(@"Connection established");
|
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;
|
break;
|
||||||
case NSStreamEventErrorOccurred:
|
case NSStreamEventErrorOccurred:{
|
||||||
NSLog(@"Connection error occured");
|
NSLog(@"Connection error occured");
|
||||||
|
[self disconnect];
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case NSStreamEventHasBytesAvailable:
|
case NSStreamEventHasBytesAvailable:
|
||||||
{
|
{
|
||||||
@ -153,7 +155,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
NSArray *commands = [str componentsSeparatedByString:@"\n"];
|
NSArray *commands = [str componentsSeparatedByString:@"\n"];
|
||||||
// NSLog(@"Data Received: %@", commands);
|
|
||||||
|
|
||||||
[self.receiver parse:commands];
|
[self.receiver parse:commands];
|
||||||
data = nil;
|
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];
|
[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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@ enum ConnectionState : NSInteger {
|
|||||||
CONNECTED
|
CONNECTED
|
||||||
};
|
};
|
||||||
|
|
||||||
|
dispatch_queue_t backgroundQueue;
|
||||||
|
|
||||||
@interface CommunicationManager : NSObject
|
@interface CommunicationManager : NSObject
|
||||||
|
|
||||||
|
@ -12,11 +12,13 @@
|
|||||||
#import "Server.h"
|
#import "Server.h"
|
||||||
#import "CommandTransmitter.h"
|
#import "CommandTransmitter.h"
|
||||||
#import "CommandInterpreter.h"
|
#import "CommandInterpreter.h"
|
||||||
|
#import <dispatch/dispatch.h>
|
||||||
|
|
||||||
@interface CommunicationManager()
|
@interface CommunicationManager()
|
||||||
|
|
||||||
@property (nonatomic, strong) Client* client;
|
@property (nonatomic, strong) Client* client;
|
||||||
@property (nonatomic, strong) CommandInterpreter* interpreter;
|
@property (nonatomic, strong) CommandInterpreter* interpreter;
|
||||||
|
@property (nonatomic, strong) CommandTransmitter* transmitter;
|
||||||
@property (atomic, strong) NSMutableArray* servers;
|
@property (atomic, strong) NSMutableArray* servers;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@ -27,8 +29,11 @@
|
|||||||
@synthesize client = _client;
|
@synthesize client = _client;
|
||||||
@synthesize state = _state;
|
@synthesize state = _state;
|
||||||
@synthesize interpreter = _interpreter;
|
@synthesize interpreter = _interpreter;
|
||||||
|
@synthesize transmitter = _transmitter;
|
||||||
@synthesize servers = _servers;
|
@synthesize servers = _servers;
|
||||||
|
|
||||||
|
NSLock *connectionLock;
|
||||||
|
|
||||||
+ (CommunicationManager *)sharedComManager
|
+ (CommunicationManager *)sharedComManager
|
||||||
{
|
{
|
||||||
static CommunicationManager *sharedComManager = nil;
|
static CommunicationManager *sharedComManager = nil;
|
||||||
@ -46,13 +51,15 @@
|
|||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
self.state = DISCONNECTED;
|
self.state = DISCONNECTED;
|
||||||
|
connectionLock = [NSLock new];
|
||||||
|
backgroundQueue = dispatch_queue_create("org.libreoffice.iosremote", NULL);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) initWithExistingServers
|
- (id) initWithExistingServers
|
||||||
{
|
{
|
||||||
self = [self init];
|
self = [self init];
|
||||||
|
|
||||||
NSUserDefaults * userDefaluts = [NSUserDefaults standardUserDefaults];
|
NSUserDefaults * userDefaluts = [NSUserDefaults standardUserDefaults];
|
||||||
|
|
||||||
if(!userDefaluts)
|
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
|
+ (id)allocWithZone:(NSZone *)zone
|
||||||
{
|
{
|
||||||
return [self sharedComManager];
|
return [self sharedComManager];
|
||||||
|
@ -8,11 +8,13 @@
|
|||||||
|
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
#import "slideShowViewController.h"
|
||||||
|
|
||||||
@interface SlideShow : NSObject
|
@interface SlideShow : NSObject
|
||||||
|
|
||||||
@property uint size;
|
@property uint size;
|
||||||
@property uint currentSlide;
|
@property uint currentSlide;
|
||||||
|
@property (nonatomic, weak) id delegate;
|
||||||
|
|
||||||
- (void) putImage: (NSString *)img AtIndex: (uint) index;
|
- (void) putImage: (NSString *)img AtIndex: (uint) index;
|
||||||
- (void) putNotes: (NSString *)notes AtIndex: (uint) index;
|
- (void) putNotes: (NSString *)notes AtIndex: (uint) index;
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#import "SlideShow.h"
|
#import "SlideShow.h"
|
||||||
#import "Base64.h"
|
#import "Base64.h"
|
||||||
|
#import "slideShowViewController.h"
|
||||||
|
|
||||||
@interface SlideShow()
|
@interface SlideShow()
|
||||||
|
|
||||||
@ -21,6 +22,7 @@
|
|||||||
|
|
||||||
@synthesize size = _size;
|
@synthesize size = _size;
|
||||||
@synthesize currentSlide = _currentSlide;
|
@synthesize currentSlide = _currentSlide;
|
||||||
|
@synthesize delegate = _delegate;
|
||||||
|
|
||||||
- (SlideShow *) init{
|
- (SlideShow *) init{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
@ -35,12 +37,14 @@
|
|||||||
NSData* data = [NSData dataWithBase64String:img];
|
NSData* data = [NSData dataWithBase64String:img];
|
||||||
UIImage* image = [UIImage imageWithData:data];
|
UIImage* image = [UIImage imageWithData:data];
|
||||||
[self.imagesArray insertObject:image atIndex:index];
|
[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{
|
- (void) putNotes: (NSString *)notes AtIndex: (uint) index{
|
||||||
[self.notesArray insertObject:notes atIndex: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
|
- (UIImage *) getImageAtIndex: (uint) index
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
if ([segue.identifier isEqualToString:@"slidesPreviewSegue"]) {
|
if ([segue.identifier isEqualToString:@"slidesPreviewSegue"]) {
|
||||||
slideShowViewController *destViewController = segue.destinationViewController;
|
slideShowViewController *destViewController = segue.destinationViewController;
|
||||||
destViewController.slideshow = [self.interpreter slideShow];
|
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 = [[Client alloc] initWithServer:self.server managedBy:nil interpretedBy:self.interpreter];
|
||||||
[self.client connect];
|
[self.client connect];
|
||||||
|
|
||||||
if([self.client ready])
|
if([self.client connected])
|
||||||
{
|
{
|
||||||
[self.pinLabel setText:[NSString stringWithFormat:@"%@", self.client.pin]];
|
[self.pinLabel setText:[NSString stringWithFormat:@"%@", self.client.pin]];
|
||||||
}
|
}
|
||||||
|
@ -32,20 +32,20 @@
|
|||||||
{
|
{
|
||||||
[super viewDidLoad];
|
[super viewDidLoad];
|
||||||
|
|
||||||
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
// NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
||||||
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
|
// NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
|
||||||
[self.image setImage:[self.slideshow getImageAtIndex:0]];
|
// [self.image setImage:[self.slideshow getImageAtIndex:0]];
|
||||||
[self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
|
// [self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
|
||||||
self.slideShowImageReadyObserver = [center addObserverForName:@"IMAGE_READY" object:nil
|
// self.slideShowImageReadyObserver = [center addObserverForName:@"IMAGE_READY" object:nil
|
||||||
queue:mainQueue usingBlock:^(NSNotification *note) {
|
// queue:mainQueue usingBlock:^(NSNotification *note) {
|
||||||
NSLog(@"Getting image to display: %@", [self.slideshow getImageAtIndex:0]);
|
// NSLog(@"Getting image to display: %@", [self.slideshow getImageAtIndex:0]);
|
||||||
[self.image setImage:[self.slideshow getImageAtIndex:0]];
|
// [self.image setImage:[self.slideshow getImageAtIndex:0]];
|
||||||
}];
|
// }];
|
||||||
self.slideShowNoteReadyObserver = [center addObserverForName:@"NOTE_READY" object:nil
|
// self.slideShowNoteReadyObserver = [center addObserverForName:@"NOTE_READY" object:nil
|
||||||
queue:mainQueue usingBlock:^(NSNotification *note) {
|
// queue:mainQueue usingBlock:^(NSNotification *note) {
|
||||||
NSLog(@"Getting note to display: %@", [self.slideshow getNotesAtIndex:0]);
|
// NSLog(@"Getting note to display: %@", [self.slideshow getNotesAtIndex:0]);
|
||||||
[self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
|
// [self.lecturer_notes loadHTMLString: [self.slideshow getNotesAtIndex:0]baseURL:nil];
|
||||||
}];
|
// }]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user