pEpObjCAdapter/PEPSync.m
author Dirk Zimmermann <dirk@pep-project.org>
Fri, 05 Oct 2018 16:35:52 +0200
branchIOSAD-103
changeset 700 4639ec878700
parent 699 924aed6a15e6
child 701 c1503dfd3cd8
permissions -rw-r--r--
IOSAD-103 support for injectSyncEvent
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@688
    12
#import "PEPMessageUtil.h"
dirk@688
    13
#import "PEPMessage.h"
dirk@700
    14
#import "PEPQueue.h"
dirk@686
    15
dirk@693
    16
// MARK: - Declare internals
dirk@693
    17
dirk@691
    18
@interface PEPSync ()
dirk@691
    19
dirk@699
    20
+ (PEPSync * _Nullable)instance;
dirk@691
    21
dirk@692
    22
@property (nonatomic, nullable, weak) PEPSyncSendMessageDelegate *syncSendMessageDelegate;
dirk@698
    23
@property (nonatomic, nullable, weak) PEPNotifyHandshakeDelegate *notifyHandshakeDelegate;
dirk@700
    24
@property (nonatomic, nonnull) PEPQueue *queue;
dirk@700
    25
dirk@700
    26
- (int)injectSyncEvent:(SYNC_EVENT)event;
dirk@692
    27
dirk@691
    28
@end
dirk@691
    29
dirk@693
    30
// MARK: - Globals called by the engine, used in session init
dirk@693
    31
dirk@682
    32
PEP_STATUS messageToSendObjc(struct _message *msg)
dirk@682
    33
{
dirk@699
    34
    PEPSyncSendMessageDelegate *delegate = [[PEPSync instance] syncSendMessageDelegate];
dirk@688
    35
    if (delegate) {
dirk@688
    36
        PEPMessage *theMessage = pEpMessageFromStruct(msg);
dirk@688
    37
        return [delegate sendMessage:theMessage];
dirk@689
    38
    } else {
dirk@689
    39
        return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
dirk@688
    40
    }
dirk@682
    41
}
dirk@682
    42
dirk@682
    43
int inject_sync_eventObjc(SYNC_EVENT ev, void *management)
dirk@682
    44
{
dirk@700
    45
    PEPSync *pEpSync = [PEPSync instance];
dirk@700
    46
dirk@700
    47
    if (!pEpSync) {
dirk@700
    48
        pEpSync = (PEPSync *) CFBridgingRelease(management);
dirk@698
    49
    }
dirk@700
    50
dirk@700
    51
    if (pEpSync) {
dirk@700
    52
        return [pEpSync injectSyncEvent:ev];
dirk@700
    53
    } else {
dirk@700
    54
        return 1;
dirk@700
    55
    }
dirk@682
    56
}
dirk@682
    57
dirk@693
    58
// MARK: - Internal globals
dirk@693
    59
dirk@699
    60
static __weak PEPSync *s_pEpSync;
dirk@686
    61
dirk@693
    62
// MARK: - PEPSync class
dirk@693
    63
dirk@681
    64
@implementation PEPSync
dirk@681
    65
dirk@699
    66
+ (PEPSync * _Nullable)instance
dirk@686
    67
{
dirk@699
    68
    return s_pEpSync;
dirk@686
    69
}
dirk@686
    70
dirk@695
    71
- (instancetype)initWithSyncSendMessageDelegate:(PEPSyncSendMessageDelegate *
dirk@695
    72
                                                 _Nonnull)syncSendMessageDelegate
dirk@695
    73
                        notifyHandshakeDelegate:(PEPNotifyHandshakeDelegate *
dirk@695
    74
                                                 _Nonnull)notifyHandshakeDelegate
dirk@695
    75
{
dirk@695
    76
    if (self = [super init]) {
dirk@699
    77
        _syncSendMessageDelegate = syncSendMessageDelegate;
dirk@699
    78
        _notifyHandshakeDelegate = notifyHandshakeDelegate;
dirk@700
    79
        _queue = [PEPQueue new];
dirk@699
    80
        s_pEpSync = self;
dirk@695
    81
    }
dirk@695
    82
    return self;
dirk@695
    83
}
dirk@695
    84
dirk@700
    85
- (int)injectSyncEvent:(SYNC_EVENT)event
dirk@700
    86
{
dirk@700
    87
    [self.queue enqueue:[NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)]];
dirk@700
    88
    return 0;
dirk@700
    89
}
dirk@700
    90
dirk@681
    91
@end