pEpObjCAdapter/PEPSync.m
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 30 Oct 2018 14:50:53 +0100
branchIOSAD-103
changeset 725 bc093cf0a2d0
parent 724 9586ce948878
child 726 7ebac3f25160
permissions -rw-r--r--
IOSAD-103 PEPSync responsible for creating engine sessions
dirk@681
     1
//
dirk@681
     2
//  PEPSync.m
dirk@681
     3
//  pEpObjCAdapter
dirk@681
     4
//
dirk@681
     5
//  Created by Dirk Zimmermann on 04.10.18.
dirk@681
     6
//  Copyright © 2018 p≡p. All rights reserved.
dirk@681
     7
//
dirk@681
     8
dirk@681
     9
#import "PEPSync.h"
dirk@681
    10
dirk@686
    11
#import "PEPSyncSendMessageDelegate.h"
dirk@704
    12
#import "PEPNotifyHandshakeDelegate.h"
dirk@688
    13
#import "PEPMessageUtil.h"
dirk@688
    14
#import "PEPMessage.h"
dirk@700
    15
#import "PEPQueue.h"
dirk@725
    16
#import "PEPLock.h"
dirk@725
    17
#import "PEPObjCAdapter.h"
dirk@725
    18
#import "NSError+PEP.h"
dirk@686
    19
dirk@693
    20
// MARK: - Declare internals
dirk@693
    21
dirk@691
    22
@interface PEPSync ()
dirk@691
    23
dirk@699
    24
+ (PEPSync * _Nullable)instance;
dirk@691
    25
dirk@704
    26
@property (nonatomic, nullable, weak) id<PEPSyncSendMessageDelegate> syncSendMessageDelegate;
dirk@704
    27
@property (nonatomic, nullable, weak) id<PEPNotifyHandshakeDelegate> notifyHandshakeDelegate;
dirk@700
    28
@property (nonatomic, nonnull) PEPQueue *queue;
dirk@720
    29
@property (nonatomic, nullable) NSThread *syncThread;
dirk@700
    30
dirk@700
    31
- (int)injectSyncEvent:(SYNC_EVENT)event;
dirk@724
    32
- (SYNC_EVENT)retrieveNextSyncEvent:(time_t)threshold;
dirk@692
    33
dirk@691
    34
@end
dirk@691
    35
dirk@721
    36
// MARK: - Callbacks called by the engine, used in session init and register_sync_callbacks
dirk@693
    37
dirk@722
    38
static PEP_STATUS messageToSendObjc(struct _message *msg)
dirk@682
    39
{
dirk@704
    40
    id<PEPSyncSendMessageDelegate> delegate = [[PEPSync instance] syncSendMessageDelegate];
dirk@688
    41
    if (delegate) {
dirk@688
    42
        PEPMessage *theMessage = pEpMessageFromStruct(msg);
dirk@688
    43
        return [delegate sendMessage:theMessage];
dirk@689
    44
    } else {
dirk@689
    45
        return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
dirk@688
    46
    }
dirk@682
    47
}
dirk@682
    48
dirk@722
    49
static int inject_sync_eventObjc(SYNC_EVENT ev, void *management)
dirk@682
    50
{
dirk@700
    51
    PEPSync *pEpSync = [PEPSync instance];
dirk@700
    52
dirk@700
    53
    if (!pEpSync) {
dirk@700
    54
        pEpSync = (PEPSync *) CFBridgingRelease(management);
dirk@698
    55
    }
dirk@700
    56
dirk@700
    57
    if (pEpSync) {
dirk@700
    58
        return [pEpSync injectSyncEvent:ev];
dirk@700
    59
    } else {
dirk@700
    60
        return 1;
dirk@700
    61
    }
dirk@682
    62
}
dirk@682
    63
dirk@724
    64
static SYNC_EVENT retrieve_next_sync_event(void *management, time_t threshold)
dirk@724
    65
{
dirk@724
    66
    PEPSync *sync = (PEPSync *) CFBridgingRelease(management);
dirk@724
    67
    return [sync retrieveNextSyncEvent:threshold];
dirk@724
    68
}
dirk@724
    69
dirk@693
    70
// MARK: - Internal globals
dirk@693
    71
dirk@699
    72
static __weak PEPSync *s_pEpSync;
dirk@686
    73
dirk@720
    74
// MARK: - Public PEPSync class
dirk@693
    75
dirk@681
    76
@implementation PEPSync
dirk@681
    77
dirk@722
    78
+ (t_messageToSendCallback)messageToSendCallback
dirk@722
    79
{
dirk@722
    80
    return messageToSendObjc;
dirk@722
    81
}
dirk@722
    82
dirk@722
    83
+ (t_injectSyncCallback)injectSyncCallback
dirk@722
    84
{
dirk@722
    85
    return inject_sync_eventObjc;
dirk@722
    86
}
dirk@722
    87
dirk@725
    88
+ (PEP_SESSION)createSession:(NSError **)error
dirk@725
    89
{
dirk@725
    90
    [PEPSync setupTrustWordsDB];
dirk@725
    91
dirk@725
    92
    PEP_SESSION session = NULL;
dirk@725
    93
dirk@725
    94
    [PEPLock lockWrite];
dirk@725
    95
    PEP_STATUS status = init(&session,
dirk@725
    96
                             [PEPSync messageToSendCallback],
dirk@725
    97
                             [PEPSync injectSyncCallback]);
dirk@725
    98
    [PEPLock unlockWrite];
dirk@725
    99
dirk@725
   100
    if (status != PEP_STATUS_OK) {
dirk@725
   101
        if (error) {
dirk@725
   102
            *error = [NSError errorWithPEPStatus:status];
dirk@725
   103
        }
dirk@725
   104
        return nil;
dirk@725
   105
    }
dirk@725
   106
dirk@725
   107
    return session;
dirk@725
   108
}
dirk@725
   109
dirk@725
   110
+ (void)releaseSession:(PEP_SESSION)session
dirk@725
   111
{
dirk@725
   112
    [PEPLock lockWrite];
dirk@725
   113
    release(session);
dirk@725
   114
    [PEPLock unlockWrite];
dirk@725
   115
}
dirk@725
   116
dirk@704
   117
- (instancetype)initWithSyncSendMessageDelegate:(id<PEPSyncSendMessageDelegate>
dirk@695
   118
                                                 _Nonnull)syncSendMessageDelegate
dirk@704
   119
                        notifyHandshakeDelegate:(id<PEPNotifyHandshakeDelegate>
dirk@695
   120
                                                 _Nonnull)notifyHandshakeDelegate
dirk@695
   121
{
dirk@695
   122
    if (self = [super init]) {
dirk@699
   123
        _syncSendMessageDelegate = syncSendMessageDelegate;
dirk@699
   124
        _notifyHandshakeDelegate = notifyHandshakeDelegate;
dirk@700
   125
        _queue = [PEPQueue new];
dirk@699
   126
        s_pEpSync = self;
dirk@695
   127
    }
dirk@695
   128
    return self;
dirk@695
   129
}
dirk@695
   130
dirk@720
   131
- (void)startup
dirk@720
   132
{
dirk@720
   133
    NSThread *theSyncThread = [[NSThread alloc] initWithTarget:self
dirk@720
   134
                                                      selector:@selector(syncThreadLoop:)
dirk@720
   135
                                                        object:nil];
dirk@720
   136
    self.syncThread = theSyncThread;
dirk@720
   137
}
dirk@720
   138
dirk@701
   139
- (void)shutdown
dirk@701
   140
{
dirk@701
   141
}
dirk@701
   142
dirk@720
   143
// MARK: - Private
dirk@720
   144
dirk@723
   145
+ (PEPSync * _Nullable)instance
dirk@723
   146
{
dirk@723
   147
    return s_pEpSync;
dirk@723
   148
}
dirk@723
   149
dirk@725
   150
+ (void)setupTrustWordsDB
dirk@725
   151
{
dirk@725
   152
    static dispatch_once_t once;
dirk@725
   153
    dispatch_once(&once, ^{
dirk@725
   154
        [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
dirk@725
   155
    });
dirk@725
   156
}
dirk@725
   157
dirk@720
   158
- (void)syncThreadLoop:(id)object {
dirk@720
   159
}
dirk@720
   160
dirk@700
   161
- (int)injectSyncEvent:(SYNC_EVENT)event
dirk@700
   162
{
dirk@700
   163
    [self.queue enqueue:[NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)]];
dirk@700
   164
    return 0;
dirk@700
   165
}
dirk@700
   166
dirk@724
   167
- (SYNC_EVENT)retrieveNextSyncEvent:(time_t)threshold
dirk@724
   168
{
dirk@724
   169
    NSValue *value = [self.queue timedDequeue:&threshold];
dirk@724
   170
    if (value) {
dirk@724
   171
        SYNC_EVENT event;
dirk@724
   172
        [value getValue:&event];
dirk@724
   173
        return event;
dirk@724
   174
    } else {
dirk@724
   175
        return new_sync_timeout_event();
dirk@724
   176
    }
dirk@724
   177
}
dirk@724
   178
dirk@681
   179
@end