e09569db6a49a7fbf91f1a668af06c4b54e17797
[openvswitch] / python / ovstest / udp.py
1 # Copyright (c) 2011 Nicira Networks
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 ovsudp contains listener and sender classes for UDP protocol
17 """
18
19 from twisted.internet.protocol import DatagramProtocol
20 from twisted.internet.task import LoopingCall
21 import array, struct, time
22
23
24 class UdpListener(DatagramProtocol):
25     """
26     Class that will listen for incoming UDP packets
27     """
28     def __init__(self):
29         self.stats = []
30
31     def startProtocol(self):
32         print "Starting UDP listener"
33
34     def stopProtocol(self):
35         print "Stopping UDP listener"
36
37     def datagramReceived(self, data, (_1, _2)):
38         """This function is called each time datagram is received"""
39         try:
40             self.stats.append(struct.unpack_from("Q", data, 0))
41         except struct.error:
42             pass #ignore packets that are less than 8 bytes of size
43
44     def getResults(self):
45         """Returns number of packets that were actually received"""
46         return len(self.stats)
47
48
49 class UdpSender(DatagramProtocol):
50     """
51     Class that will send UDP packets to UDP Listener
52     """
53     def __init__(self, host, count, size, duration):
54         #LoopingCall does not know whether UDP socket is actually writable
55         self.looper = None
56         self.host = host
57         self.count = count
58         self.duration = duration
59         self.start = time.time()
60         self.sent = 0
61         self.data = array.array('c', 'X' * size)
62
63     def startProtocol(self):
64         print "Starting UDP sender"
65         self.looper = LoopingCall(self.sendData)
66         period = self.duration / float(self.count)
67         self.looper.start(period , now = False)
68
69     def stopProtocol(self):
70         print "Stopping UDP sender"
71         if (self.looper is not None):
72             self.looper.stop()
73             self.looper = None
74
75     def datagramReceived(self, data, (host, port)):
76         pass
77
78     def sendData(self):
79         """This function is called from LoopingCall"""
80         if self.start + self.duration < time.time():
81             self.looper.stop()
82             self.looper = None
83
84         self.sent += 1
85         struct.pack_into('Q', self.data, 0, self.sent)
86         self.transport.write(self.data, self.host)
87
88     def getResults(self):
89         """Returns number of packets that were sent"""
90         return self.sent