pEpObjCAdapter/PEPSessionProvider.m
author buff <andreas@pep-project.org>
Wed, 11 Oct 2017 09:59:01 +0200
branchIOSAD-50
changeset 269 5a187b6cf883
parent 265 030829be070c
child 272 c456123edfa7
permissions -rw-r--r--
IOSAD-50 renames PEPSession to be able to create a fake PEPSession class for clients
andreas@262
     1
//
andreas@262
     2
//  PEPSessionProvider.m
andreas@262
     3
//  pEpObjCAdapter
andreas@262
     4
//
andreas@262
     5
//  Created by Andreas Buff on 09.10.17.
andreas@262
     6
//  Copyright © 2017 p≡p. All rights reserved.
andreas@262
     7
//
andreas@262
     8
andreas@262
     9
#import "PEPSessionProvider.h"
andreas@262
    10
andreas@269
    11
#import "PEPInternalSession.h"
andreas@269
    12
#import "PEPInternalSession+Internal.h"
andreas@262
    13
#import "PEPCopyableThread.h"
andreas@262
    14
andreas@262
    15
@implementation PEPSessionProvider
andreas@262
    16
andreas@262
    17
static NSLock *s_sessionForThreadLock = nil;
andreas@269
    18
static NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *s_sessionForThreadDict;
andreas@262
    19
andreas@262
    20
#pragma mark - Public API
andreas@262
    21
andreas@269
    22
+ (PEPInternalSession * _Nonnull)session
andreas@262
    23
{
andreas@262
    24
    [[self sessionForThreadLock] lock];
andreas@262
    25
andreas@262
    26
    PEPCopyableThread *currentThread = [[PEPCopyableThread alloc] initWithThread:[NSThread currentThread]];
andreas@269
    27
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@269
    28
    PEPInternalSession *session = dict[currentThread];
andreas@262
    29
    if (!session) {
andreas@269
    30
        session = [[PEPInternalSession alloc] initInternal];
andreas@262
    31
        dict[currentThread] = session;
andreas@262
    32
    }
andreas@262
    33
    [self nullifySesssionOfFinishedThreads];
andreas@262
    34
andreas@262
    35
    [[self sessionForThreadLock] unlock];
andreas@262
    36
andreas@262
    37
    return session;
andreas@262
    38
}
andreas@262
    39
andreas@262
    40
+ (void)cleanup
andreas@262
    41
{
andreas@262
    42
    [[self sessionForThreadLock] lock];
andreas@262
    43
andreas@269
    44
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
    45
    for (PEPCopyableThread *thread in dict.allKeys) {
andreas@262
    46
        [thread cancel];
andreas@262
    47
        [self nullifySessionForThread:thread];
andreas@262
    48
    }
andreas@262
    49
    [dict removeAllObjects];
andreas@262
    50
andreas@262
    51
    [[self sessionForThreadLock] unlock];
andreas@262
    52
}
andreas@262
    53
andreas@262
    54
#pragma mark - Life Cycle
andreas@262
    55
andreas@262
    56
+ (void)initialize
andreas@262
    57
{
andreas@262
    58
    s_sessionForThreadLock = [NSLock new];
andreas@262
    59
    s_sessionForThreadDict = [NSMutableDictionary new];
andreas@262
    60
}
andreas@262
    61
andreas@262
    62
#pragma mark - Lock
andreas@262
    63
andreas@262
    64
+ (NSLock *)sessionForThreadLock
andreas@262
    65
{
andreas@262
    66
    return s_sessionForThreadLock;
andreas@262
    67
}
andreas@262
    68
andreas@262
    69
+ (NSMutableDictionary *)sessionForThreadDict
andreas@262
    70
{
andreas@262
    71
    return s_sessionForThreadDict;
andreas@262
    72
}
andreas@262
    73
andreas@262
    74
#pragma mark -
andreas@262
    75
andreas@262
    76
+ (void)nullifySesssionOfFinishedThreads
andreas@262
    77
{
andreas@269
    78
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
    79
    for (PEPCopyableThread *thread in dict.allKeys) {
andreas@262
    80
        if (thread.isFinished) {
andreas@262
    81
            [self nullifySessionForThread:thread];
andreas@262
    82
        }
andreas@262
    83
    }
andreas@262
    84
}
andreas@262
    85
andreas@262
    86
+ (void)nullifySessionForThread:(PEPCopyableThread *)thread
andreas@262
    87
{
andreas@269
    88
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@269
    89
    PEPInternalSession *session = dict[thread];
andreas@262
    90
    [self performSelector:@selector(nullifySession:)
andreas@262
    91
                 onThread:thread.thread
andreas@262
    92
               withObject:session
andreas@265
    93
            waitUntilDone:YES];
andreas@262
    94
    dict[thread] = nil;
andreas@262
    95
}
andreas@262
    96
andreas@269
    97
+ (void)nullifySession:(PEPInternalSession *)session
andreas@262
    98
{
andreas@262
    99
    session = nil;
andreas@262
   100
}
andreas@262
   101
andreas@262
   102
@end