pEpObjCAdapter/PEPSync.m
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 30 Oct 2018 14:50:53 +0100
branchIOSAD-103
changeset 724 9586ce948878
parent 723 bb5a5cbdb7bc
child 725 bc093cf0a2d0
permissions -rw-r--r--
IOSAD-103 retrieve_next_sync_event with assumptions
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@686
    16
dirk@693
    17
// MARK: - Declare internals
dirk@693
    18
dirk@691
    19
@interface PEPSync ()
dirk@691
    20
dirk@699
    21
+ (PEPSync * _Nullable)instance;
dirk@691
    22
dirk@704
    23
@property (nonatomic, nullable, weak) id<PEPSyncSendMessageDelegate> syncSendMessageDelegate;
dirk@704
    24
@property (nonatomic, nullable, weak) id<PEPNotifyHandshakeDelegate> notifyHandshakeDelegate;
dirk@700
    25
@property (nonatomic, nonnull) PEPQueue *queue;
dirk@720
    26
@property (nonatomic, nullable) NSThread *syncThread;
dirk@700
    27
dirk@700
    28
- (int)injectSyncEvent:(SYNC_EVENT)event;
dirk@724
    29
- (SYNC_EVENT)retrieveNextSyncEvent:(time_t)threshold;
dirk@692
    30
dirk@691
    31
@end
dirk@691
    32
dirk@721
    33
// MARK: - Callbacks called by the engine, used in session init and register_sync_callbacks
dirk@693
    34
dirk@722
    35
static PEP_STATUS messageToSendObjc(struct _message *msg)
dirk@682
    36
{
dirk@704
    37
    id<PEPSyncSendMessageDelegate> delegate = [[PEPSync instance] syncSendMessageDelegate];
dirk@688
    38
    if (delegate) {
dirk@688
    39
        PEPMessage *theMessage = pEpMessageFromStruct(msg);
dirk@688
    40
        return [delegate sendMessage:theMessage];
dirk@689
    41
    } else {
dirk@689
    42
        return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
dirk@688
    43
    }
dirk@682
    44
}
dirk@682
    45
dirk@722
    46
static int inject_sync_eventObjc(SYNC_EVENT ev, void *management)
dirk@682
    47
{
dirk@700
    48
    PEPSync *pEpSync = [PEPSync instance];
dirk@700
    49
dirk@700
    50
    if (!pEpSync) {
dirk@700
    51
        pEpSync = (PEPSync *) CFBridgingRelease(management);
dirk@698
    52
    }
dirk@700
    53
dirk@700
    54
    if (pEpSync) {
dirk@700
    55
        return [pEpSync injectSyncEvent:ev];
dirk@700
    56
    } else {
dirk@700
    57
        return 1;
dirk@700
    58
    }
dirk@682
    59
}
dirk@682
    60
dirk@724
    61
static SYNC_EVENT retrieve_next_sync_event(void *management, time_t threshold)
dirk@724
    62
{
dirk@724
    63
    PEPSync *sync = (PEPSync *) CFBridgingRelease(management);
dirk@724
    64
    return [sync retrieveNextSyncEvent:threshold];
dirk@724
    65
}
dirk@724
    66
dirk@693
    67
// MARK: - Internal globals
dirk@693
    68
dirk@699
    69
static __weak PEPSync *s_pEpSync;
dirk@686
    70
dirk@720
    71
// MARK: - Public PEPSync class
dirk@693
    72
dirk@681
    73
@implementation PEPSync
dirk@681
    74
dirk@722
    75
+ (t_messageToSendCallback)messageToSendCallback
dirk@722
    76
{
dirk@722
    77
    return messageToSendObjc;
dirk@722
    78
}
dirk@722
    79
dirk@722
    80
+ (t_injectSyncCallback)injectSyncCallback
dirk@722
    81
{
dirk@722
    82
    return inject_sync_eventObjc;
dirk@722
    83
}
dirk@722
    84
dirk@704
    85
- (instancetype)initWithSyncSendMessageDelegate:(id<PEPSyncSendMessageDelegate>
dirk@695
    86
                                                 _Nonnull)syncSendMessageDelegate
dirk@704
    87
                        notifyHandshakeDelegate:(id<PEPNotifyHandshakeDelegate>
dirk@695
    88
                                                 _Nonnull)notifyHandshakeDelegate
dirk@695
    89
{
dirk@695
    90
    if (self = [super init]) {
dirk@699
    91
        _syncSendMessageDelegate = syncSendMessageDelegate;
dirk@699
    92
        _notifyHandshakeDelegate = notifyHandshakeDelegate;
dirk@700
    93
        _queue = [PEPQueue new];
dirk@699
    94
        s_pEpSync = self;
dirk@695
    95
    }
dirk@695
    96
    return self;
dirk@695
    97
}
dirk@695
    98
dirk@720
    99
- (void)startup
dirk@720
   100
{
dirk@720
   101
    NSThread *theSyncThread = [[NSThread alloc] initWithTarget:self
dirk@720
   102
                                                      selector:@selector(syncThreadLoop:)
dirk@720
   103
                                                        object:nil];
dirk@720
   104
    self.syncThread = theSyncThread;
dirk@720
   105
}
dirk@720
   106
dirk@701
   107
- (void)shutdown
dirk@701
   108
{
dirk@701
   109
}
dirk@701
   110
dirk@720
   111
// MARK: - Private
dirk@720
   112
dirk@723
   113
+ (PEPSync * _Nullable)instance
dirk@723
   114
{
dirk@723
   115
    return s_pEpSync;
dirk@723
   116
}
dirk@723
   117
dirk@720
   118
- (void)syncThreadLoop:(id)object {
dirk@720
   119
}
dirk@720
   120
dirk@700
   121
- (int)injectSyncEvent:(SYNC_EVENT)event
dirk@700
   122
{
dirk@700
   123
    [self.queue enqueue:[NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)]];
dirk@700
   124
    return 0;
dirk@700
   125
}
dirk@700
   126
dirk@724
   127
- (SYNC_EVENT)retrieveNextSyncEvent:(time_t)threshold
dirk@724
   128
{
dirk@724
   129
    NSValue *value = [self.queue timedDequeue:&threshold];
dirk@724
   130
    if (value) {
dirk@724
   131
        SYNC_EVENT event;
dirk@724
   132
        [value getValue:&event];
dirk@724
   133
        return event;
dirk@724
   134
    } else {
dirk@724
   135
        return new_sync_timeout_event();
dirk@724
   136
    }
dirk@724
   137
}
dirk@724
   138
dirk@681
   139
@end