Kaydet (Commit) 894d19ab authored tarafından siqi's avatar siqi

seems somthing get blocked...

üst d1bb1270
......@@ -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
......
......@@ -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;
}
}
......
......@@ -8,7 +8,8 @@
#import <Foundation/Foundation.h>
#import "SlideShow.h"
@class SlideShow;
@interface CommandInterpreter : NSObject
......
......@@ -7,7 +7,9 @@
//
#import <Foundation/Foundation.h>
#import "Client.h"
@class Client;
@interface CommandTransmitter : NSObject
......
......@@ -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;
}
......
......@@ -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
......@@ -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];
}
......
......@@ -8,7 +8,8 @@
#import <Foundation/Foundation.h>
#import "slideShowViewController.h"
@class slideShowViewController;
@interface SlideShow : NSObject
......
<?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>
......
......@@ -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
......@@ -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
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment