-
Notifications
You must be signed in to change notification settings - Fork 13
/
Queue.m
119 lines (91 loc) · 2.25 KB
/
Queue.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
110
111
112
113
114
115
116
117
118
119
//
// Queue.m
// TaskExplorer
//
// Created by Patrick Wardle on 9/26/14.
// Copyright (c) 2014 Objective-See. All rights reserved.
//
#import "Queue.h"
#import "Consts.h"
#import "Binary.h"
#import "VirusTotal.h"
#import "AppDelegate.h"
@implementation Queue
@synthesize itemsIn;
@synthesize itemsOut;
@synthesize eventQueue;
@synthesize queueCondition;
@synthesize qProcessorThread;
-(id)init
{
//init super
self = [super init];
if(nil != self)
{
//init queue
eventQueue = [NSMutableArray array];
//init empty condition
queueCondition = [[NSCondition alloc] init];
//spin up thread to watch/process queue
self.qProcessorThread = [[NSThread alloc] initWithTarget:self selector:@selector(processQueue:) object:nil];
//start it
[self.qProcessorThread start];
}
return self;
}
//process events from Q
-(void)processQueue:(id)threadParam
{
//Binary obj
Binary* binary = nil;
//nap for a bit
// ->don't want UI thread, etc to suffer
[NSThread sleepForTimeInterval:5.0f];
//for ever
while(YES)
{
//pool
@autoreleasepool {
//lock
[self.queueCondition lock];
//wait while queue is empty
while(YES == [self.eventQueue empty])
{
//wait
[self.queueCondition wait];
}
//get item off queue
binary = [eventQueue dequeue];
//inc
itemsOut++;
//unlock
[self.queueCondition unlock];
//generate hashes, etc
[binary generateDetailedInfo];
//when connected
// add item for VT processing
if(YES == isConnected)
{
//add
[virusTotal addItem:binary];
}
} //pool
}//foreverz process queue
return;
}
//add an object to the queue
-(void)enqueue:(id)anObject
{
//lock
[self.queueCondition lock];
//add to queue
[self.eventQueue enqueue:anObject];
//inc
itemsIn++;
//signal
[self.queueCondition signal];
//unlock
[self.queueCondition unlock];
return;
}
@end