summaryrefslogtreecommitdiff
path: root/ios/iosremote/iosremote/Communication/CommunicationManager.m
blob: 6eb175d86bd027002d7ef890e77b5f6bdd3552fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.


#import "CommunicationManager.h"
#import "Client.h"
#import "Server.h"
#import "SlideShow.h"
#import "CommandTransmitter.h"
#import "CommandInterpreter.h"
#import "BasePresentationViewController.h"
#import "MainSplitViewController.h"
#import "ServerListViewController.h"
#import <dispatch/dispatch.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <SystemConfiguration/CaptiveNetwork.h>

#define ExistingServersKey @"CommunicationManager.ExistingServers"

@interface CommunicationManager() <UIAlertViewDelegate>
@end

// Singlton Pattern
@implementation CommunicationManager

@synthesize client = _client;
@synthesize state = _state;
@synthesize interpreter = _interpreter;
@synthesize transmitter = _transmitter;
@synthesize servers = _servers;
@synthesize delegate = _delegate;
@synthesize autoDiscoveryServers = _autoDiscoveryServers;

+ (CommunicationManager *)sharedComManager
{
    static CommunicationManager *sharedComManager = nil;
    static dispatch_once_t _singletonPredicate;
    
    dispatch_once(&_singletonPredicate, ^{
        sharedComManager = [[super allocWithZone:nil] initWithExistingServers];
    });
    
    return sharedComManager;
}

+ (id)fetchSSIDInfo {
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        NSLog(@"%@ => %@", ifnam, info);
        if (info && [info count]) { break; }
    }
    return info;
}

- (void) connectionStatusHandler:(NSNotification *)note
{
    if([[note name] isEqualToString:@"connection.status.connected"]){
        if (self.state!=CONNECTED){
            NSLog(@"Connected, waiting for pairing response");
            // A 5 seconds timer waiting for pairing response.
            [self.client startConnectionTimeoutTimerwithInterval:5.0];
            self.transmitter = [[CommandTransmitter alloc] initWithClient:self.client];
        }
    } else if ([[note name] isEqualToString:@"connection.status.disconnected"]){
        if ([self.delegate respondsToSelector:@selector(disableSpinner)])
            [self.delegate disableSpinner];
        if (self.state != DISCONNECTED) {
            NSLog(@"Connection Failed");
            self.state = DISCONNECTED;
            if ([self.delegate isKindOfClass:[ServerListViewController class]]){
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Failed to reach server", @"Connection failure title")
                                                                      message:NSLocalizedString(@"Failed to reach server Msg", @"Connection failure message")
                                                                     delegate:self
                                                            cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                            otherButtonTitles:NSLocalizedString(@"Help", nil), nil];
                    [message show];
                });
                [[self.delegate navigationController] popToRootViewControllerAnimated:YES];
                [(ServerListViewController *)self.delegate startSearching];
            } else {
                [[self.delegate navigationController] popToRootViewControllerAnimated:YES];
                BasePresentationViewController *bpvc = [[(MainSplitViewController *)[self.delegate presentingViewController] viewControllers] objectAtIndex:1];
                [bpvc didReceiveDisconnection];
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Connection Lost", @"Connection Lost Alert title")
                                                                      message:NSLocalizedString(@"Connection Lost Msg", @"Connection Lost Alert Msg")
                                                                     delegate:nil
                                                            cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                            otherButtonTitles:nil];
                    [message show];
                });
            }
        }
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }else if (buttonIndex == 1){
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Pairing Instructions", @"Pairing instructions Alert Title")
                                                          message:NSLocalizedString(@"Pairing Instructions Msg", nil)
                                                         delegate:nil
                                                cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                otherButtonTitles:nil];
        [message show];
    }
}

- (id) init
{
    self = [super init];
    self.state = DISCONNECTED;
    self.searchState = WAITING;
    self.interpreter = [[CommandInterpreter alloc] init];
    self.servers = [[NSMutableArray alloc] init];
    
    [[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];
    self.autoDiscoveryServers = [[NSMutableArray alloc] init];
    return self;
}



- (id) initWithExistingServers
{
    self = [self init];
    NSUserDefaults * userDefaluts = [NSUserDefaults standardUserDefaults];
    
    NSData *dataRepresentingExistingServers = [userDefaluts objectForKey:ExistingServersKey];
    if (dataRepresentingExistingServers != nil)
    {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingExistingServers];
        if (oldSavedArray != nil)
            self.servers = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            self.servers = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void) connectToServer:(Server*)server
{
    if (self.state == CONNECTING) {
        return;
    } else {
        [self.client disconnect];
        self.state = CONNECTING;
        // initialise it with a given server
        self.client = [[Client alloc]initWithServer:server managedBy:self interpretedBy:self.interpreter];
        self.transmitter = [[CommandTransmitter alloc] initWithClient:self.client];
        [self.client connect];
    }
}


- (NSNumber *) getPairingPin{
    return [self.client pin];
}

- (NSString *) getPairingDeviceName
{
    return [self.client name];
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self sharedComManager];
}

- (void) addServersWithName:(NSString*)name
                  AtAddress:(NSString*)addr
{
    Server * s = [[Server alloc] initWithProtocol:NETWORK atAddress:addr ofName:name];
    [self.servers addObject:s];
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.servers] forKey:ExistingServersKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void) removeServerAtIndex:(NSUInteger)index
{
    [self.servers removeObjectAtIndex:index];
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.servers] forKey:ExistingServersKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end