pEpObjCAdapter/PEPSync.m
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 30 Oct 2018 14:50:53 +0100
branchIOSAD-103
changeset 720 1134b54a7082
parent 704 f5036c40fbd8
child 721 0da7ffe1e9e2
permissions -rw-r--r--
IOSAD-103 sync thread loop
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@693
    32
// MARK: - Globals called by the engine, used in session init
dirk@693
    33
dirk@682
    34
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@682
    45
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@699
    68
+ (PEPSync * _Nullable)instance
dirk@686
    69
{
dirk@699
    70
    return s_pEpSync;
dirk@686
    71
}
dirk@686
    72
dirk@704
    73
- (instancetype)initWithSyncSendMessageDelegate:(id<PEPSyncSendMessageDelegate>
dirk@695
    74
                                                 _Nonnull)syncSendMessageDelegate
dirk@704
    75
                        notifyHandshakeDelegate:(id<PEPNotifyHandshakeDelegate>
dirk@695
    76
                                                 _Nonnull)notifyHandshakeDelegate
dirk@695
    77
{
dirk@695
    78
    if (self = [super init]) {
dirk@699
    79
        _syncSendMessageDelegate = syncSendMessageDelegate;
dirk@699
    80
        _notifyHandshakeDelegate = notifyHandshakeDelegate;
dirk@700
    81
        _queue = [PEPQueue new];
dirk@699
    82
        s_pEpSync = self;
dirk@695
    83
    }
dirk@695
    84
    return self;
dirk@695
    85
}
dirk@695
    86
dirk@720
    87
- (void)startup
dirk@720
    88
{
dirk@720
    89
    NSThread *theSyncThread = [[NSThread alloc] initWithTarget:self
dirk@720
    90
                                                      selector:@selector(syncThreadLoop:)
dirk@720
    91
                                                        object:nil];
dirk@720
    92
    self.syncThread = theSyncThread;
dirk@720
    93
}
dirk@720
    94
dirk@701
    95
- (void)shutdown
dirk@701
    96
{
dirk@701
    97
}
dirk@701
    98
dirk@720
    99
// MARK: - Private
dirk@720
   100
dirk@720
   101
- (void)syncThreadLoop:(id)object {
dirk@720
   102
dirk@720
   103
}
dirk@720
   104
dirk@700
   105
- (int)injectSyncEvent:(SYNC_EVENT)event
dirk@700
   106
{
dirk@700
   107
    [self.queue enqueue:[NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)]];
dirk@700
   108
    return 0;
dirk@700
   109
}
dirk@700
   110
dirk@681
   111
@end