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