EMANE  1.2.1
emane/events/commeffectevent.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2013-2014,2017 - Adjacent Link LLC, Bridgewater,
3 # New Jersey
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 # * Neither the name of Adjacent Link LLC nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 
34 from . import Event
35 from . import commeffectevent_pb2
36 
37 class CommEffectEvent(Event):
38  IDENTIFIER = 103
39 
40  def __init__(self):
41  self._event = commeffectevent_pb2.CommEffectEvent()
42 
43  def append(self,nemId,**kwargs):
44  commeffect = self._event.commEffects.add()
45 
46  commeffect.nemId = nemId
47 
48  for (name,value) in list(kwargs.items()):
49 
50  if name == 'latency':
51  if (isinstance(value,int) or \
52  isinstance(value,float)) and \
53  value >= 0:
54  commeffect.latencySeconds = value
55  else:
56  raise ValueError("latency must be a positive numeric")
57 
58  elif name == 'jitter':
59  if (isinstance(value,int) or \
60  isinstance(value,float)) and \
61  value >= 0:
62  commeffect.jitterSeconds = value
63  else:
64  raise ValueError("jitter must be a positive numeric")
65 
66  elif name == 'loss':
67  if (isinstance(value,int) or \
68  isinstance(value,float)) and \
69  value >= 0:
70  commeffect.probabilityLoss = value
71  else:
72  raise ValueError("loss must be numeric")
73 
74  elif name == 'duplicate':
75  if (isinstance(value,int) or \
76  isinstance(value,float)) and \
77  value >= 0:
78  commeffect.probabilityDuplicate = value
79  else:
80  raise ValueError("duplicate must be a positive numeric")
81 
82  elif name == 'unicast':
83  if (isinstance(value,int) or isinstance(value,int)) and \
84  value >= 0:
85  commeffect.unicastBitRatebps = value
86  else:
87  raise ValueError("unicast must be a positive integer")
88 
89  elif name == 'broadcast':
90  if (isinstance(value,int) or isinstance(value,int)) and \
91  value >= 0:
92  commeffect.broadcastBitRatebps = value
93  else:
94  raise ValueError("broadcast must be a positive integer")
95 
96  else:
97  raise KeyError("unknown parameter: %s" % name)
98 
99 
100  def serialize(self):
101  return self._event.SerializeToString()
102 
103  def restore(self,data):
104  self._event.ParseFromString(data)
105 
106  def __iter__(self):
107  for commeffect in self._event.commEffects:
108  kwargs = {'latency': commeffect.latencySeconds,
109  'jitter' : commeffect.jitterSeconds,
110  'loss' : commeffect.probabilityLoss,
111  'duplicate' : commeffect.probabilityDuplicate,
112  'unicast' : commeffect.unicastBitRatebps,
113  'broadcast' : commeffect.broadcastBitRatebps}
114 
115  yield (commeffect.nemId,kwargs)