pEpObjCAdapter/PEPSync.m
branchIOS-1475_exp_dirk_001
changeset 897 a1c583ffca0e
parent 876 0899b80e06e7
child 901 aba7ae6ea8ae
     1.1 --- a/pEpObjCAdapter/PEPSync.m	Fri Mar 15 16:14:50 2019 +0100
     1.2 +++ b/pEpObjCAdapter/PEPSync.m	Fri Mar 15 16:14:50 2019 +0100
     1.3 @@ -9,7 +9,6 @@
     1.4  #import "PEPSync.h"
     1.5  
     1.6  #import "PEPSendMessageDelegate.h"
     1.7 -#import "PEPNotifyHandshakeDelegate.h"
     1.8  #import "PEPMessageUtil.h"
     1.9  #import <PEPObjCAdapterFramework/PEPMessage.h>
    1.10  #import "PEPQueue.h"
    1.11 @@ -19,114 +18,14 @@
    1.12  #import "PEPSessionProvider.h"
    1.13  #import "PEPInternalSession.h"
    1.14  
    1.15 -// MARK: - Declare internals
    1.16 -
    1.17 -typedef PEP_STATUS (* t_messageToSendCallback)(struct _message *msg);
    1.18 -typedef int (* t_injectSyncCallback)(SYNC_EVENT ev, void *management);
    1.19 -
    1.20 -@interface PEPSync ()
    1.21 -
    1.22 -+ (PEPSync * _Nullable)instance;
    1.23 -
    1.24 -@property (nonatomic, nullable, weak) id<PEPSendMessageDelegate> sendMessageDelegate;
    1.25 -@property (nonatomic, nullable, weak) id<PEPNotifyHandshakeDelegate> notifyHandshakeDelegate;
    1.26 -@property (nonatomic, nonnull) PEPQueue *queue;
    1.27 -@property (nonatomic, nullable) NSThread *syncThread;
    1.28 -@property (nonatomic, nullable) NSConditionLock *conditionLockForJoiningSyncThread;
    1.29 -
    1.30 -/**
    1.31 - @Return: The callback for message sending that should be used on every session init.
    1.32 - */
    1.33 -+ (t_messageToSendCallback)messageToSendCallback;
    1.34 -
    1.35 -/**
    1.36 - @Return: The callback for injectiong sync messages that should be used on every session init.
    1.37 - */
    1.38 -+ (t_injectSyncCallback)injectSyncCallback;
    1.39 -
    1.40 -- (PEP_STATUS)messageToSend:(struct _message *)msg;
    1.41 -
    1.42 -- (int)injectSyncEvent:(SYNC_EVENT)event;
    1.43 -
    1.44 -- (PEP_STATUS)notifyHandshake:(pEp_identity *)me
    1.45 -                      partner:(pEp_identity *)partner
    1.46 -                       signal:(sync_handshake_signal)signal;
    1.47 -
    1.48 -- (SYNC_EVENT)retrieveNextSyncEvent:(time_t)threshold;
    1.49 -
    1.50 -@end
    1.51 -
    1.52 -// MARK: - Callbacks called by the engine, used in session init
    1.53 -
    1.54 -static PEP_STATUS s_messageToSendObjc(struct _message *msg)
    1.55 -{
    1.56 -    PEPSync *pEpSync = [PEPSync instance];
    1.57 -
    1.58 -    if (pEpSync) {
    1.59 -        return [pEpSync messageToSend:msg];
    1.60 -    } else {
    1.61 -        return PEP_SYNC_NO_NOTIFY_CALLBACK;
    1.62 -    }
    1.63 -}
    1.64 -
    1.65 -static int s_inject_sync_event(SYNC_EVENT ev, void *management)
    1.66 -{
    1.67 -    PEPSync *pEpSync = [PEPSync instance];
    1.68 -
    1.69 -    if (pEpSync) {
    1.70 -        return [pEpSync injectSyncEvent:ev];
    1.71 -    } else {
    1.72 -        return 1;
    1.73 -    }
    1.74 -}
    1.75 -
    1.76 -// MARK: - Callbacks called by the engine, used in register_sync_callbacks
    1.77 -
    1.78 -static PEP_STATUS s_notifyHandshake(pEp_identity *me,
    1.79 -                                    pEp_identity *partner,
    1.80 -                                    sync_handshake_signal signal)
    1.81 -{
    1.82 -    PEPSync *pEpSync = [PEPSync instance];
    1.83 -
    1.84 -    if (pEpSync) {
    1.85 -        return [pEpSync notifyHandshake:me partner:partner signal:signal];
    1.86 -    } else {
    1.87 -        return PEP_SYNC_NO_NOTIFY_CALLBACK;
    1.88 -    }
    1.89 -}
    1.90 -
    1.91 -static SYNC_EVENT s_retrieve_next_sync_event(void *management, unsigned threshold)
    1.92 -{
    1.93 -    PEPSync *sync = [PEPSync instance];
    1.94 -    return [sync retrieveNextSyncEvent:threshold];
    1.95 -}
    1.96 -
    1.97 -// MARK: - Internal globals
    1.98 -
    1.99 -static __weak PEPSync *s_pEpSync;
   1.100 -
   1.101 -// MARK: - Public PEPSync class
   1.102 -
   1.103  @implementation PEPSync
   1.104  
   1.105 -+ (t_messageToSendCallback)messageToSendCallback
   1.106 -{
   1.107 -    return s_messageToSendObjc;
   1.108 -}
   1.109 -
   1.110 -+ (t_injectSyncCallback)injectSyncCallback
   1.111 -{
   1.112 -    return s_inject_sync_event;
   1.113 -}
   1.114 -
   1.115  + (PEP_SESSION)createSession:(NSError **)error
   1.116  {
   1.117      PEP_SESSION session = NULL;
   1.118  
   1.119      [PEPLock lockWrite];
   1.120 -    PEP_STATUS status = init(&session,
   1.121 -                             [PEPSync messageToSendCallback],
   1.122 -                             [PEPSync injectSyncCallback]);
   1.123 +    PEP_STATUS status = init(&session);
   1.124      [PEPLock unlockWrite];
   1.125  
   1.126      if (status != PEP_STATUS_OK) {
   1.127 @@ -146,114 +45,4 @@
   1.128      [PEPLock unlockWrite];
   1.129  }
   1.130  
   1.131 -- (instancetype)initWithSendMessageDelegate:(id<PEPSendMessageDelegate>
   1.132 -                                             _Nonnull)sendMessageDelegate
   1.133 -                    notifyHandshakeDelegate:(id<PEPNotifyHandshakeDelegate>
   1.134 -                                             _Nonnull)notifyHandshakeDelegate
   1.135 -{
   1.136 -    if (self = [super init]) {
   1.137 -        _sendMessageDelegate = sendMessageDelegate;
   1.138 -        _notifyHandshakeDelegate = notifyHandshakeDelegate;
   1.139 -        _queue = [PEPQueue new];
   1.140 -        s_pEpSync = self;
   1.141 -    }
   1.142 -    return self;
   1.143 -}
   1.144 -
   1.145 -- (void)startup
   1.146 -{
   1.147 -    // assure the main session exists
   1.148 -    PEPInternalSession *session = [PEPSessionProvider session];
   1.149 -    session = nil;
   1.150 -
   1.151 -    self.conditionLockForJoiningSyncThread = [[NSConditionLock alloc] initWithCondition:NO];
   1.152 -    NSThread *theSyncThread = [[NSThread alloc] initWithTarget:self
   1.153 -                                                      selector:@selector(syncThreadLoop:)
   1.154 -                                                        object:nil];
   1.155 -    theSyncThread.name = @"pEp-sync-loop";
   1.156 -    self.syncThread = theSyncThread;
   1.157 -    [theSyncThread start];
   1.158 -}
   1.159 -
   1.160 -- (void)shutdown
   1.161 -{
   1.162 -    if (self.syncThread) {
   1.163 -        [self injectSyncEvent:nil];
   1.164 -        [self.conditionLockForJoiningSyncThread lockWhenCondition:YES];
   1.165 -        [self.conditionLockForJoiningSyncThread unlock];
   1.166 -    }
   1.167 -    self.conditionLockForJoiningSyncThread = nil;
   1.168 -}
   1.169 -
   1.170 -// MARK: - Private
   1.171 -
   1.172 -+ (PEPSync * _Nullable)instance
   1.173 -{
   1.174 -    return s_pEpSync;
   1.175 -}
   1.176 -
   1.177 -- (void)syncThreadLoop:(id)object
   1.178 -{
   1.179 -    [self.conditionLockForJoiningSyncThread lock];
   1.180 -
   1.181 -    PEPInternalSession *session = [PEPSessionProvider session];
   1.182 -
   1.183 -    if (session) {
   1.184 -        register_sync_callbacks(session.session, nil, s_notifyHandshake,
   1.185 -                                s_retrieve_next_sync_event);
   1.186 -        do_sync_protocol(session.session, nil);
   1.187 -        unregister_sync_callbacks(session.session);
   1.188 -    } else {
   1.189 -        // TODO: indicate error, maybe through `object`?
   1.190 -    }
   1.191 -
   1.192 -    session = nil;
   1.193 -
   1.194 -    [self.conditionLockForJoiningSyncThread unlockWithCondition:YES];
   1.195 -}
   1.196 -
   1.197 -- (PEP_STATUS)messageToSend:(struct _message *)msg
   1.198 -{
   1.199 -    if (self.sendMessageDelegate) {
   1.200 -        PEPMessage *theMessage = pEpMessageFromStruct(msg);
   1.201 -        return [self.sendMessageDelegate sendMessage:theMessage];
   1.202 -    } else {
   1.203 -        return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
   1.204 -    }
   1.205 -}
   1.206 -
   1.207 -- (int)injectSyncEvent:(SYNC_EVENT)event
   1.208 -{
   1.209 -    [self.queue enqueue:[NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)]];
   1.210 -    return 0;
   1.211 -}
   1.212 -
   1.213 -- (PEP_STATUS)notifyHandshake:(pEp_identity *)me
   1.214 -                      partner:(pEp_identity *)partner
   1.215 -                       signal:(sync_handshake_signal)signal
   1.216 -{
   1.217 -    if (self.notifyHandshakeDelegate) {
   1.218 -        PEPIdentity *meIdentity = PEP_identityFromStruct(me);
   1.219 -        PEPIdentity *partnerIdentity = PEP_identityFromStruct(partner);
   1.220 -        return [self.notifyHandshakeDelegate notifyHandshake:NULL
   1.221 -                                                          me:meIdentity
   1.222 -                                                     partner:partnerIdentity
   1.223 -                                                      signal:signal];
   1.224 -    } else {
   1.225 -        return PEP_SYNC_NO_NOTIFY_CALLBACK;
   1.226 -    }
   1.227 -}
   1.228 -
   1.229 -- (SYNC_EVENT)retrieveNextSyncEvent:(time_t)threshold
   1.230 -{
   1.231 -    NSValue *value = [self.queue timedDequeue:&threshold];
   1.232 -    if (value) {
   1.233 -        SYNC_EVENT event;
   1.234 -        [value getValue:&event];
   1.235 -        return event;
   1.236 -    } else {
   1.237 -        return new_sync_timeout_event();
   1.238 -    }
   1.239 -}
   1.240 -
   1.241  @end