]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - listener.py
minor comment change
[quix0rs-apt-p2p.git] / listener.py
1 ## Copyright 2002 Andrew Loewenstern, All Rights Reserved
2
3 from socket import *
4
5 # simple UDP communicator
6
7 class Listener:
8     def __init__(self, host, port):
9         self.msgq = []
10         self.sock = socket(AF_INET, SOCK_DGRAM)
11         self.sock.setblocking(0)
12         self.sock.bind((host, port))
13             
14     def qMsg(self, msg, host, port):
15         self.msgq.append((msg, host, port))
16     
17     def qLen(self):
18         return len(self.msgq)
19     
20     def dispatchMsg(self):
21         if self.qLen() > 0:
22             msg, host, port = self.msgq[0]
23             del self.msgq[0]
24             self.sock.sendto(msg, 0, (host, port))
25         
26     def receiveMsg(self):
27         msg = ()
28         try:
29             msg = self.sock.recvfrom(65536)
30         except error, tup:
31             if tup[1] == "Resource temporarily unavailable":
32                 # no message
33                 return msg
34             print error, tup
35         else:
36             return msg
37             
38     def __del__(self):
39         self.sock.close()
40
41
42
43 ###########################
44 import unittest
45
46 class ListenerTest(unittest.TestCase):
47     def setUp(self):
48         self.a = Listener('localhost', 8080)
49         self.b = Listener('localhost', 8081)
50     def tearDown(self):
51         del(self.a)
52         del(self.b)
53         
54     def testQueue(self):
55         assert self.a.qLen() == 0, "expected queue to be empty"
56         self.a.qMsg('hello', 'localhost', 8081)
57         assert self.a.qLen() == 1, "expected one message to be in queue"
58         self.a.qMsg('hello', 'localhost', 8081)
59         assert self.a.qLen() == 2, "expected two messages to be in queue"
60         self.a.dispatchMsg()
61         assert self.a.qLen() == 1, "expected one message to be in queue"
62         self.a.dispatchMsg()
63         assert self.a.qLen() == 0, "expected all messages to be flushed from queue"
64
65     def testSendReceiveOne(self):
66         self.a.qMsg('hello', 'localhost', 8081)
67         self.a.dispatchMsg()
68         
69         assert self.b.receiveMsg()[0] == "hello", "did not receive expected message"
70         assert self.b.receiveMsg() == (), "received unexpected message"
71  
72         self.b.qMsg('hello', 'localhost', 8080)
73         self.b.dispatchMsg()
74         
75         assert self.a.receiveMsg()[0] == "hello", "did not receive expected message"
76                 
77         assert self.a.receiveMsg() == (), "received unexpected message"
78
79     def testSendReceiveInterleaved(self):
80         self.a.qMsg('hello', 'localhost', 8081)
81         self.a.qMsg('hello', 'localhost', 8081)
82         self.a.dispatchMsg()
83         self.a.dispatchMsg()
84         
85         assert self.b.receiveMsg()[0] == "hello", "did not receive expected message"
86         assert self.b.receiveMsg()[0] == "hello", "did not receive expected message"            
87         assert self.b.receiveMsg() == (), "received unexpected message"
88  
89         self.b.qMsg('hello', 'localhost', 8080)
90         self.b.qMsg('hello', 'localhost', 8080)
91         self.b.dispatchMsg()
92         self.b.dispatchMsg()
93         
94         assert self.a.receiveMsg()[0] == "hello", "did not receive expected message"
95         assert self.a.receiveMsg()[0] == "hello", "did not receive expected message"            
96         assert self.a.receiveMsg() == (), "received unexpected message"
97     
98
99 if __name__ == '__main__':
100     unittest.main()