-
Notifications
You must be signed in to change notification settings - Fork 1
/
Broker.m
109 lines (89 loc) · 3.22 KB
/
Broker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#import "Broker.h"
#import "NSDistributedNotificationCenter.h"
#import "AAClientLib.h"
#import "AAPilotNotification.h"
// The broker class is responsible for caching analysis results and for transmitting them to the backend
@implementation Broker : NSObject
- (void) setBool:(BOOL) result forKey:(NSString*) key {
if(result && key != nil) {
if([key length]>0) {
DLog(@"Broker: setBool: %@ forKey: %@", result ? @"YES" : @"NO", key);
@synchronized(self.boolResults) {
[self.boolResults setValue:[NSNumber numberWithBool:result] forKey:key];
}
}
}
}
- (void) setObject:(id) result forKey:(NSString*) key {
if(result != nil && key != nil) {
if([key length]>0) {
DLog(@"Broker: setObject: %@ forKey: %@", result, key);
@synchronized(self.objectResults) {
[self.objectResults setValue:result forKey:key];
}
}
}
}
- (void) saveOpenPath:(NSString*) path {
@synchronized(self.openPaths) {
if (![self.openPaths containsObject:path]) {
[self.openPaths addObject:path];
}
}
}
- (void) sendResults {
AAClientLib* client = [AAClientLib sharedInstance];
NSDictionary* taskInfo = client.taskInfo;
if (taskInfo != nil && [taskInfo objectForKey:@"bundleId"] != nil) {
if ([[taskInfo objectForKey:@"bundleId"] isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) {
DLog(@"Delivering results for %@ to DiOS backend", [[NSBundle mainBundle] bundleIdentifier]);
@synchronized(self.boolResults) {
if(self.boolResults != nil) {
if([self.boolResults count]>0) {
[client saveResult:self.boolResults withType:AACResultTypeCriteria];
}
}
}
@synchronized(self.objectResults) {
if(self.objectResults != nil) {
if([self.objectResults count]>0) {
for (id key in self.objectResults) {
id value = [self.objectResults objectForKey:key];
[client saveResult:value withType:key];
}
}
}
}
@synchronized(self.openPaths) {
if(self.openPaths != nil) {
if([self.openPaths count]>0) {
[client saveResult:self.openPaths withType:@"open_paths"];
}
}
}
}
}
}
- (void) registerExitHook {
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(sendResults) name:AAPilotAppExecutionFinished object:nil];
self.enabled = YES;
}
- (id) init {
self = [super init];
if (self) {
self.boolResults = [NSMutableDictionary dictionary];
self.objectResults = [NSMutableDictionary dictionary];
self.openPaths = [NSMutableArray array];
}
return self;
}
+ (Broker*) sharedInstance {
static Broker* sharedSingleton;
@synchronized(self) {
if (!sharedSingleton) {
sharedSingleton = [[Broker alloc] init];
}
return sharedSingleton;
}
}
@end