-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.py
40 lines (34 loc) · 1.01 KB
/
program.py
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
import threading
import time
try:
from queue import Queue, Empty
except ImportError:
from Queue import Queue, Empty
class BaseProgram(threading.Thread):
"""
Represents a program running on a Node that interprets and responds to
incoming packets
"""
def __init__(self, node):
threading.Thread.__init__(self)
self.keep_listening = True
self.node = node
def run(self):
"""
runloop that reads packets off the node's incoming packet buffer
(node.inq)
"""
while self.keep_listening:
for interface in self.node.interfaces:
try:
self.recv(self.node.inq[interface].get(timeout=0), interface)
except Empty:
time.sleep(0.01)
def stop(self):
self.keep_listening = False
self.join()
def recv(self, packet, interface):
"""
overload this and put logic here to actually do something with the packet
"""
pass