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