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