UDP message management implementation
| 24 | |
| 25 | |
| 26 | class UDPMessage: |
| 27 | "UDP message management implementation" |
| 28 | |
| 29 | UNICAST = 'unicast' |
| 30 | MULTICAST = 'multicast' |
| 31 | |
| 32 | def __init__(self, env, addr, port, msgType, initialDelay=0, |
| 33 | unicast_num=UNICAST_UDP_REPEAT, |
| 34 | multicast_num=MULTICAST_UDP_REPEAT): |
| 35 | """msgType shall be UDPMessage.UNICAST or UDPMessage.MULTICAST""" |
| 36 | self._env = env |
| 37 | self._addr = addr |
| 38 | self._port = port |
| 39 | self._msgType = msgType |
| 40 | |
| 41 | if msgType == self.UNICAST: |
| 42 | udpRepeat, udpMinDelay, udpMaxDelay, udpUpperDelay = \ |
| 43 | unicast_num, \ |
| 44 | UNICAST_UDP_MIN_DELAY, \ |
| 45 | UNICAST_UDP_MAX_DELAY, \ |
| 46 | UNICAST_UDP_UPPER_DELAY |
| 47 | else: |
| 48 | udpRepeat, udpMinDelay, udpMaxDelay, udpUpperDelay = \ |
| 49 | multicast_num, \ |
| 50 | MULTICAST_UDP_MIN_DELAY, \ |
| 51 | MULTICAST_UDP_MAX_DELAY, \ |
| 52 | MULTICAST_UDP_UPPER_DELAY |
| 53 | |
| 54 | self._udpRepeat = udpRepeat |
| 55 | self._udpUpperDelay = udpUpperDelay |
| 56 | self._t = (udpMinDelay + ((udpMaxDelay - udpMinDelay) * random.random())) / 2 |
| 57 | self._nextTime = int(time.time() * 1000) + initialDelay |
| 58 | |
| 59 | def getEnv(self): |
| 60 | return self._env |
| 61 | |
| 62 | def getAddr(self): |
| 63 | return self._addr |
| 64 | |
| 65 | def getPort(self): |
| 66 | return self._port |
| 67 | |
| 68 | def msgType(self): |
| 69 | return self._msgType |
| 70 | |
| 71 | def isFinished(self): |
| 72 | return self._udpRepeat <= 0 |
| 73 | |
| 74 | def canSend(self): |
| 75 | ct = int(time.time() * 1000) |
| 76 | return self._nextTime < ct |
| 77 | |
| 78 | def refresh(self): |
| 79 | self._t = self._t * 2 |
| 80 | if self._t > self._udpUpperDelay: |
| 81 | self._t = self._udpUpperDelay |
| 82 | self._nextTime = int(time.time() * 1000) + self._t |
| 83 | self._udpRepeat = self._udpRepeat - 1 |
no outgoing calls
no test coverage detected