-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPacketProcessor.py
More file actions
61 lines (48 loc) · 1.94 KB
/
Copy pathPacketProcessor.py
File metadata and controls
61 lines (48 loc) · 1.94 KB
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
'''
Copyright 2013 Lukasz Szmit
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
from sys import exit
from abc import abstractmethod
from threading import Thread
from Queue import Queue
class PacketProcessor(object):
def __init__(self, num_workers, results_callback, log_callback=None):
self._log = log_callback
self.work_q = Queue()
self.result_q = Queue()
self.num_workers = num_workers
self.workers = []
for i in range(self.num_workers):
worker = Thread(target=self.process, name="worker-%s" % i)
self.workers.append(worker)
worker = Thread(target=results_callback, name="results-%s" % i)
self.results = []
self.results.append(worker)
def is_alive(self):
return len(self.workers) > 0
def put_packet(self, packet_bytes):
self.work_q.put(packet_bytes)
def start(self):
workers = self.workers + self.results
for worker in workers:
worker.start()
def stop(self):
for i in range(self.num_workers):
self.work_q.put(None)
num_results = len(self.results)
for i in range(num_results):
self.result_q.put(None)
@abstractmethod
def process(self):
raise NotImplementedError("The process() method is not implemented")
if __name__ == '__main__':
exit(1)