merge IOSAD-157
authorbuff <andreas@pep-project.org>
Wed, 20 Nov 2019 19:00:40 +0100
changeset 1360adbedc09b0fe
parent 1331 b19fa36ebc9e
parent 1359 decf1a9ccd50
child 1361 d4fbe8a9eb31
child 1366 47fecdfa178f
merge IOSAD-157
     1.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Fri Nov 15 19:58:39 2019 +0100
     1.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Wed Nov 20 19:00:40 2019 +0100
     1.3 @@ -24,6 +24,7 @@
     1.4  #import "PEPAutoPointer.h"
     1.5  #import "NSNumber+PEPRating.h"
     1.6  #import "NSMutableDictionary+PEP.h"
     1.7 +#import "PEPSync.h"
     1.8  #import "PEPSync_Internal.h" // for [PEPSync createSession:]
     1.9  #import "PEPInternalConstants.h"
    1.10  
     2.1 --- a/pEpObjCAdapter/PEPQueue.h	Fri Nov 15 19:58:39 2019 +0100
     2.2 +++ b/pEpObjCAdapter/PEPQueue.h	Wed Nov 20 19:00:40 2019 +0100
     2.3 @@ -10,10 +10,19 @@
     2.4  
     2.5  typedef void (^deleteOp)(id);
     2.6  
     2.7 +/// FiFo queue.
     2.8  @interface PEPQueue : NSObject
     2.9  
    2.10 +/// Puts an object into the queue (FiFo, first in, first out, so that the objects
    2.11 +/// that were put in before are dequeued before this one).
    2.12 +/// @param object The object to put into the queue.
    2.13  - (void)enqueue:(id)object;
    2.14  
    2.15 +/// Puts an object into the queue, so that it will be dequeued before the
    2.16 +/// existing objects.
    2.17 +/// @param object The object to put into the queue.
    2.18 +- (void)prequeue:(id)object;
    2.19 +
    2.20  - (id)timedDequeue:(time_t*)timeout;
    2.21  
    2.22  - (id)dequeue;
    2.23 @@ -22,9 +31,7 @@
    2.24  
    2.25  - (void)purge:(deleteOp)del;
    2.26  
    2.27 -/**
    2.28 - Removes all objects from the queue.
    2.29 - */
    2.30 +/// Removes all objects from the queue.
    2.31  - (void)removeAllObjects;
    2.32  
    2.33  @end
     3.1 --- a/pEpObjCAdapter/PEPQueue.m	Fri Nov 15 19:58:39 2019 +0100
     3.2 +++ b/pEpObjCAdapter/PEPQueue.m	Wed Nov 20 19:00:40 2019 +0100
     3.3 @@ -31,18 +31,35 @@
     3.4      return self;
     3.5  }
     3.6  
     3.7 +/// A block that gets called to modify the queue model (internal).
     3.8 +typedef void (^queueOp)(NSMutableArray *queue);
     3.9 +
    3.10 +/// Lock the queue and calls the given block.
    3.11 +/// @param block The block to invoke once the queue is locked (internal).
    3.12 +- (void)lockQueueAndUpdateWithBlock:(queueOp)block
    3.13 +{
    3.14 +    [_cond lock];
    3.15 +
    3.16 +    if (_queue) {
    3.17 +        block(_queue);
    3.18 +    }
    3.19 +
    3.20 +    [_cond signal];
    3.21 +    [_cond unlock];
    3.22 +}
    3.23 +
    3.24  - (void)enqueue:(id)object
    3.25  {
    3.26 -    [_cond lock];
    3.27 -    
    3.28 -    if (_queue) {
    3.29 -        [_queue insertObject:object atIndex:0];
    3.30 -    }
    3.31 -    
    3.32 -    [_cond signal];
    3.33 -    
    3.34 -    [_cond unlock];
    3.35 -    
    3.36 +    [self lockQueueAndUpdateWithBlock:^(NSMutableArray *queue){
    3.37 +        [queue insertObject:object atIndex:0];
    3.38 +    }];
    3.39 +}
    3.40 +
    3.41 +- (void)prequeue:(id)object
    3.42 +{
    3.43 +    [self lockQueueAndUpdateWithBlock:^(NSMutableArray *queue){
    3.44 +        [queue addObject:object];
    3.45 +    }];
    3.46  }
    3.47  
    3.48  - (id)timedDequeue:(time_t*)timeout
     4.1 --- a/pEpObjCAdapter/PEPSync.m	Fri Nov 15 19:58:39 2019 +0100
     4.2 +++ b/pEpObjCAdapter/PEPSync.m	Wed Nov 20 19:00:40 2019 +0100
     4.3 @@ -8,9 +8,10 @@
     4.4  
     4.5  #import <os/log.h>
     4.6  
     4.7 +#import "pEpEngine.h"
     4.8 +
     4.9  #import "PEPSync.h"
    4.10 -
    4.11 -#import "pEpEngine.h"
    4.12 +#import "PEPSync_Internal.h"
    4.13  
    4.14  #import "PEPSendMessageDelegate.h"
    4.15  #import "PEPNotifyHandshakeDelegate.h"
    4.16 @@ -49,7 +50,7 @@
    4.17  
    4.18  - (PEP_STATUS)messageToSend:(struct _message *)msg;
    4.19  
    4.20 -- (int)injectSyncEvent:(SYNC_EVENT)event;
    4.21 +- (int)injectSyncEvent:(SYNC_EVENT)event isFromShutdown:(BOOL)isFromShutdown;
    4.22  
    4.23  - (PEP_STATUS)notifyHandshake:(pEp_identity *)me
    4.24                        partner:(pEp_identity *)partner
    4.25 @@ -77,7 +78,9 @@
    4.26      PEPSync *pEpSync = [PEPSync instance];
    4.27  
    4.28      if (pEpSync) {
    4.29 -        return [pEpSync injectSyncEvent:ev];
    4.30 +        // The inject comes from the engine, so we know it's not the
    4.31 +        // adapter client calling shutdown.
    4.32 +        return [pEpSync injectSyncEvent:ev isFromShutdown:NO];
    4.33      } else {
    4.34          return 1;
    4.35      }
    4.36 @@ -174,11 +177,8 @@
    4.37  - (void)shutdown
    4.38  {
    4.39      if (self.syncThread) {
    4.40 -        [self injectSyncEvent:nil];
    4.41 -        [self.conditionLockForJoiningSyncThread lockWhenCondition:YES];
    4.42 -        [self.conditionLockForJoiningSyncThread unlock];
    4.43 +        [self injectSyncEvent:nil isFromShutdown:YES];
    4.44      }
    4.45 -    self.conditionLockForJoiningSyncThread = nil;
    4.46  }
    4.47  
    4.48  // MARK: - Private
    4.49 @@ -241,9 +241,23 @@
    4.50      }
    4.51  }
    4.52  
    4.53 -- (int)injectSyncEvent:(SYNC_EVENT)event
    4.54 +- (int)injectSyncEvent:(SYNC_EVENT)event isFromShutdown:(BOOL)isFromShutdown
    4.55  {
    4.56 -    [self.queue enqueue:[NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)]];
    4.57 +    NSValue *value = [NSValue valueWithBytes:&event objCType:@encode(SYNC_EVENT)];
    4.58 +
    4.59 +    if (event) {
    4.60 +        [self.queue enqueue:value];
    4.61 +    } else {
    4.62 +        [self.queue prequeue:value];
    4.63 +        [self.conditionLockForJoiningSyncThread lockWhenCondition:YES];
    4.64 +        [self.conditionLockForJoiningSyncThread unlock];
    4.65 +        self.conditionLockForJoiningSyncThread = nil;
    4.66 +        if (!isFromShutdown) {
    4.67 +            // Only inform the delegate if the shutdown came from the engine
    4.68 +            [self.notifyHandshakeDelegate engineShutdownKeySync];
    4.69 +        }
    4.70 +    }
    4.71 +
    4.72      return 0;
    4.73  }
    4.74  
     5.1 --- a/pEpObjCAdapter/PEPSync_Internal.h	Fri Nov 15 19:58:39 2019 +0100
     5.2 +++ b/pEpObjCAdapter/PEPSync_Internal.h	Wed Nov 20 19:00:40 2019 +0100
     5.3 @@ -12,7 +12,7 @@
     5.4  /**
     5.5   Internal methods of PEPSync.
     5.6   */
     5.7 -@interface PEPSync : NSObject
     5.8 +@interface PEPSync ()
     5.9  
    5.10  /**
    5.11   Creates an engine session (PEP_SESSION).
     6.1 --- a/pEpObjCAdapterFramework/PEPNotifyHandshakeDelegate.h	Fri Nov 15 19:58:39 2019 +0100
     6.2 +++ b/pEpObjCAdapterFramework/PEPNotifyHandshakeDelegate.h	Wed Nov 20 19:00:40 2019 +0100
     6.3 @@ -17,27 +17,29 @@
     6.4   */
     6.5  @protocol PEPNotifyHandshakeDelegate <NSObject>
     6.6  
     6.7 -/**
     6.8 - Requests the app to show a handshake dialog, or change the icon that represents
     6.9 - the key-sync state (as in, grouped, or sole, etc.).
    6.10 -
    6.11 - After the dialog has been shown, the user's choices can be communicated back to the engine
    6.12 - via [PEPSessionProtocol deliverHandshakeResult:identitiesSharing:error].
    6.13 -
    6.14 - @param object This can be used to thread information from the app through the sync-loop back to
    6.15 -               the app. Currently unused and always nil.
    6.16 - @param me The own identity.
    6.17 -           Note that in some cases, only the most essential properties are set.
    6.18 - @param partner The partner identity.
    6.19 -                Note that in some cases, only the most essential properties are set.
    6.20 - @param signal The kind of action that is happening or requested.
    6.21 - @return A status indicating errors in the immediate/synchronous handling of the call.
    6.22 -         The (delayed) response from the user are communicated to the engine
    6.23 -         via separate method calls, as noted in the discussion.
    6.24 - */
    6.25 +/// Requests the app to show a handshake dialog, or change the icon that represents
    6.26 +/// the key-sync state (as in, grouped, or sole, etc.).
    6.27 +///
    6.28 +/// After the dialog has been shown, the user's choices can be communicated back to the engine
    6.29 +/// via [PEPSessionProtocol deliverHandshakeResult:identitiesSharing:error].
    6.30 +///
    6.31 +/// @param object This can be used to thread information from the app through the sync-loop back to
    6.32 +///   the app. Currently unused and always nil.
    6.33 +/// @param me The own identity.
    6.34 +///   Note that in some cases, only the most essential properties are set.
    6.35 +/// @param partner The partner identity.
    6.36 +///   Note that in some cases, only the most essential properties are set.
    6.37 +/// @param signal The kind of action that is happening or requested.
    6.38 +/// @return A status indicating errors in the immediate/synchronous handling of the call.
    6.39 +///   The (delayed) response from the user are communicated to the engine
    6.40 +///   via separate method calls, as noted in the discussion.
    6.41  - (PEPStatus)notifyHandshake:(void * _Nullable)object
    6.42                            me:(PEPIdentity * _Nonnull)me
    6.43                       partner:(PEPIdentity * _Nonnull)partner
    6.44                        signal:(PEPSyncHandshakeSignal)signal;
    6.45  
    6.46 +/// Sent when the sync loop was shut down by the engine,
    6.47 +/// e.g. in response to leving the device group
    6.48 +- (void)engineShutdownKeySync;
    6.49 +
    6.50  @end
     7.1 --- a/pEpObjCTests/PEPSessionTest.m	Fri Nov 15 19:58:39 2019 +0100
     7.2 +++ b/pEpObjCTests/PEPSessionTest.m	Wed Nov 20 19:00:40 2019 +0100
     7.3 @@ -1316,6 +1316,9 @@
     7.4      XCTAssertTrue([session leaveDeviceGroup:&error]);
     7.5      XCTAssertNil(error);
     7.6  
     7.7 +    // leaving a device group should disable sync
     7.8 +    XCTAssertTrue(self.notifyHandshakeDelegate.engineDidShutdownKeySync);
     7.9 +
    7.10      [self shutdownSync];
    7.11  }
    7.12  
    7.13 @@ -1514,6 +1517,7 @@
    7.14                              userID:@"me-myself-and-i"
    7.15                              userName:@"pEp Me"
    7.16                              isOwn:YES];
    7.17 +
    7.18      NSError *error = nil;
    7.19      XCTAssertTrue([session mySelf:identMe error:&error]);
    7.20      XCTAssertNil(error);
     8.1 --- a/pEpObjCTests/TestUtils/PEPSessionTestNotifyHandshakeDelegate.h	Fri Nov 15 19:58:39 2019 +0100
     8.2 +++ b/pEpObjCTests/TestUtils/PEPSessionTestNotifyHandshakeDelegate.h	Wed Nov 20 19:00:40 2019 +0100
     8.3 @@ -12,4 +12,7 @@
     8.4  
     8.5  @interface PEPSessionTestNotifyHandshakeDelegate : NSObject<PEPNotifyHandshakeDelegate>
     8.6  
     8.7 +/// This is set to YES if the _engine_ shut the sync loop down.
     8.8 +@property (nonatomic) BOOL engineDidShutdownKeySync;
     8.9 +
    8.10  @end
     9.1 --- a/pEpObjCTests/TestUtils/PEPSessionTestNotifyHandshakeDelegate.m	Fri Nov 15 19:58:39 2019 +0100
     9.2 +++ b/pEpObjCTests/TestUtils/PEPSessionTestNotifyHandshakeDelegate.m	Wed Nov 20 19:00:40 2019 +0100
     9.3 @@ -18,4 +18,9 @@
     9.4      return PEPStatusOK;
     9.5  }
     9.6  
     9.7 +- (void)engineShutdownKeySync
     9.8 +{
     9.9 +    self.engineDidShutdownKeySync = YES;
    9.10 +}
    9.11 +
    9.12  @end