pEpObjCAdapter/PEPSync.m
author Dirk Zimmermann <dirk@pep-project.org>
Fri, 05 Oct 2018 16:35:52 +0200
branchIOSAD-103
changeset 699 924aed6a15e6
parent 698 aaf199099cf1
child 700 4639ec878700
permissions -rw-r--r--
IOSAD-103 quasi-singleton
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@686
    14
dirk@693
    15
// MARK: - Declare internals
dirk@693
    16
dirk@691
    17
@interface PEPSync ()
dirk@691
    18
dirk@699
    19
+ (PEPSync * _Nullable)instance;
dirk@691
    20
dirk@692
    21
@property (nonatomic, nullable, weak) PEPSyncSendMessageDelegate *syncSendMessageDelegate;
dirk@698
    22
@property (nonatomic, nullable, weak) PEPNotifyHandshakeDelegate *notifyHandshakeDelegate;
dirk@692
    23
dirk@691
    24
@end
dirk@691
    25
dirk@693
    26
// MARK: - Globals called by the engine, used in session init
dirk@693
    27
dirk@682
    28
PEP_STATUS messageToSendObjc(struct _message *msg)
dirk@682
    29
{
dirk@699
    30
    PEPSyncSendMessageDelegate *delegate = [[PEPSync instance] syncSendMessageDelegate];
dirk@688
    31
    if (delegate) {
dirk@688
    32
        PEPMessage *theMessage = pEpMessageFromStruct(msg);
dirk@688
    33
        return [delegate sendMessage:theMessage];
dirk@689
    34
    } else {
dirk@689
    35
        return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
dirk@688
    36
    }
dirk@682
    37
}
dirk@682
    38
dirk@682
    39
int inject_sync_eventObjc(SYNC_EVENT ev, void *management)
dirk@682
    40
{
dirk@699
    41
    PEPNotifyHandshakeDelegate *delegate = [[PEPSync instance] notifyHandshakeDelegate];
dirk@698
    42
    if (delegate) {
dirk@698
    43
        return 0;
dirk@698
    44
    }
dirk@698
    45
    return 1;
dirk@682
    46
}
dirk@682
    47
dirk@693
    48
// MARK: - Internal globals
dirk@693
    49
dirk@699
    50
static __weak PEPSync *s_pEpSync;
dirk@686
    51
dirk@693
    52
// MARK: - PEPSync class
dirk@693
    53
dirk@681
    54
@implementation PEPSync
dirk@681
    55
dirk@699
    56
+ (PEPSync * _Nullable)instance
dirk@686
    57
{
dirk@699
    58
    return s_pEpSync;
dirk@686
    59
}
dirk@686
    60
dirk@695
    61
- (instancetype)initWithSyncSendMessageDelegate:(PEPSyncSendMessageDelegate *
dirk@695
    62
                                                 _Nonnull)syncSendMessageDelegate
dirk@695
    63
                        notifyHandshakeDelegate:(PEPNotifyHandshakeDelegate *
dirk@695
    64
                                                 _Nonnull)notifyHandshakeDelegate
dirk@695
    65
{
dirk@695
    66
    if (self = [super init]) {
dirk@699
    67
        _syncSendMessageDelegate = syncSendMessageDelegate;
dirk@699
    68
        _notifyHandshakeDelegate = notifyHandshakeDelegate;
dirk@699
    69
        s_pEpSync = self;
dirk@695
    70
    }
dirk@695
    71
    return self;
dirk@695
    72
}
dirk@695
    73
dirk@681
    74
@end