24 A collection of classes which wrap our @ref C-Classes and provide Pythonic interfaces to these C-classes.
27 from AssimCtypes
import *
28 from frameinfo
import FrameTypes, FrameSetTypes
35 NetAddr = POINTER(NetAddr)
36 Frame = POINTER(Frame)
37 AddrFrame = POINTER(AddrFrame)
38 IntFrame = POINTER(IntFrame)
39 SeqnoFrame = POINTER(SeqnoFrame)
40 CstringFrame = POINTER(CstringFrame)
41 UnknownFrame = POINTER(UnknownFrame)
42 SignFrame = POINTER(SignFrame)
43 FrameSet = POINTER(FrameSet)
44 ConfigContext = POINTER(ConfigContext)
45 ConfigValue = POINTER(ConfigValue)
46 IpPortFrame = POINTER(IpPortFrame)
47 guint8 = POINTER(guint8)
48 GSList = POINTER(GSList)
52 Increment the reference count to an AssimObj (_not_ a pyAssimObj)
53 Need to call CCref under the following circumstances:
54 When we are creating an object that points to another underlying C-class object
55 which already has a permanent reference to it somewhere else
56 For example, if we're returning a pyNetAddr object that points to a NetAddr object
57 that's in a ConfigContext object. If we don't, then when our pyNetAddr object goes
58 out of scope, then the underlying NetAddr object will be freed, even though there's
59 a reference to it in the ConfigContext object. Conversely, if the ConfigContext
60 object goes out of scope first, then the our pyNetAddr object could become invalid.
62 Do not call it when you've constructed a new object that there were no previous pointers
66 while (type(base)
is not AssimObj):
72 while (type(base)
is not AssimObj):
78 LLDP_TLV_END: (
'END',
True),
79 LLDP_TLV_CHID: (
'ChassisId',
True),
80 LLDP_TLV_PID: (
'PortId',
True),
81 LLDP_TLV_TTL: (
'TTL',
True),
82 LLDP_TLV_PORT_DESCR: (
'PortDescription',
False),
83 LLDP_TLV_SYS_NAME: (
'SystemName',
True),
84 LLDP_TLV_SYS_DESCR: (
'SystemDescription',
True),
85 LLDP_TLV_SYS_CAPS: (
'SystemCapabilities',
True),
86 LLDP_TLV_MGMT_ADDR: (
'ManagementAddress',
True),
87 LLDP_TLV_ORG_SPECIFIC: (
'(OrgSpecific)',
True),
90 LLDP_ORG802_1_VLAN_PVID: (
'VlanPvId',
False),
91 LLDP_ORG802_1_VLAN_PORTPROTO: (
'VlanPortProtocol',
False),
92 LLDP_ORG802_1_VLAN_NAME: (
'VlanName',
False),
93 LLDP_ORG802_1_VLAN_PROTOID: (
'VlanProtocolId',
False),
96 LLDP_ORG802_3_PHY_CONFIG: (
'PhysicalConfiguration',
False),
97 LLDP_ORG802_3_POWERVIAMDI: (
'PowerViaMDI',
False),
98 LLDP_ORG802_3_LINKAGG: (
'LinkAggregation',
False),
99 LLDP_ORG802_3_MTU: (
'MTU',
False),
103 def _byte0(pktstart):
104 return int(cast(pktstart, cClass.guint8)[0])
107 def _byte1addr(pktstart):
108 addr = addressof(pktstart.contents) + 1
109 return pointer(type(pktstart.contents).from_address(addr))
112 def _byteN(pktstart, n):
113 return int(cast(pktstart, cClass.guint8)[n])
116 def _byteNaddr(pktstart, n):
117 addr = addressof(pktstart.contents) + n
118 return pointer(type(pktstart.contents).from_address(addr))
121 def _decode_netaddr(addrstart, addrlen):
122 byte0 = SwitchDiscovery._byte0(addrstart)
123 byte1addr = SwitchDiscovery._byte1addr(addrstart)
125 if byte0 == ADDR_FAMILY_IPV6:
126 if addrlen != 17:
return None
128 elif byte0 == ADDR_FAMILY_IPV4:
129 if addrlen != 5:
return None
131 elif byte0 == ADDR_FAMILY_802:
136 if Cnetaddr
is not None:
137 return str(
pyNetAddr(
None, Cstruct=Cnetaddr))
143 return SwitchDiscovery._decode_lldp(host, interface, wallclock, pktstart, pktend)
145 return SwitchDiscovery._decode_cdp(host, interface, wallclock, pktstart, pktend)
146 raise ValueError(
'Malformed Switch Discovery Packet')
149 def _decode_lldp(host, interface, wallclock, pktstart, pktend):
150 'Decode LLDP packet into a JSON discovery packet'
152 'ConnectsToHost': host,
153 'ConnectsToInterface': interface,
158 'discovertype':
'#LinkDiscovery',
159 'description':
'Link Level Switch Discovery (lldp)',
160 'source':
'_decode_lldp()',
162 'localtime': str(wallclock),
167 sourcemacptr = SwitchDiscovery._byteNaddr(cast(pktstart, cClass.guint8), 6)
171 sourcemac =
pyNetAddr(
None, Cstruct=Cmacaddr)
175 while this
and this < pktend:
180 if tlvtype
not in SwitchDiscovery.lldpnames:
181 print >>sys.stderr,
'Cannot find tlvtype %d' % tlvtype
184 (tlvname,isswitchinfo) = SwitchDiscovery.lldpnames[tlvtype]
186 if (tlvtype == LLDP_TLV_PORT_DESCR
or tlvtype == LLDP_TLV_SYS_NAME
or
187 tlvtype == LLDP_TLV_SYS_DESCR):
188 value = string_at(tlvptr, tlvlen)
190 elif tlvtype == LLDP_TLV_PID:
191 pidtype = SwitchDiscovery._byte0(tlvptr)
192 if (pidtype == LLDP_PIDTYPE_ALIAS
or pidtype == LLDP_PIDTYPE_IFNAME
193 or pidtype == LLDP_PIDTYPE_LOCAL):
194 sloc = SwitchDiscovery._byte1addr(tlvptr)
195 value = string_at(sloc, tlvlen-1)
197 elif tlvtype == LLDP_TLV_CHID:
198 chidtype = SwitchDiscovery._byte0(tlvptr)
200 if (chidtype == LLDP_CHIDTYPE_COMPONENT
or chidtype == LLDP_CHIDTYPE_ALIAS
201 or chidtype == LLDP_CHIDTYPE_IFNAME
or chidtype == LLDP_CHIDTYPE_LOCAL):
202 sloc = SwitchDiscovery._byte1addr(tlvptr)
203 value = string_at(sloc, tlvlen-1)
204 elif chidtype == LLDP_CHIDTYPE_MACADDR:
205 byte1addr = SwitchDiscovery._byte1addr(tlvptr)
211 if Cmacaddr
is not None:
212 value =
pyNetAddr(
None, Cstruct=Cmacaddr)
213 elif chidtype == LLDP_CHIDTYPE_NETADDR:
214 byte1addr = SwitchDiscovery._byte1addr(tlvptr)
215 value = SwitchDiscovery._decode_netaddr(byte1addr, tlvlen-1)
217 elif tlvtype == LLDP_TLV_MGMT_ADDR:
218 addrlen = SwitchDiscovery._byte0(tlvptr)
219 byte1addr = SwitchDiscovery._byte1addr(tlvptr)
220 value = SwitchDiscovery._decode_netaddr(byte1addr, addrlen)
222 elif tlvtype == LLDP_TLV_ORG_SPECIFIC:
223 print >>sys.stderr,
'Found LLDP org-specific extensions (not processed)'
225 if value
is not None:
226 if tlvtype == LLDP_TLV_PID:
227 switchinfo[
'ports'][value] = thisportinfo
229 while len(numericpart) > 0
and not numericpart.isdigit():
230 numericpart = numericpart[1:]
231 if len > 0
and numericpart.isdigit():
232 thisportinfo[
'_PORTNO'] = int(numericpart)
235 switchinfo[tlvname] = value
237 thisportinfo[tlvname] = value
239 thisportinfo[
'sourceMAC'] = sourcemac
244 def _decode_cdp(host, interface, wallclock, pktstart, pktend):
245 'Decode CDP packet into a JSON discovery packet'
247 'ConnectsToHost': host,
248 'ConnectsToInterface': interface,
253 'discovertype':
'#LinkDiscovery',
254 'description':
'Link Level Switch Discovery (cdp)',
255 'source':
'_decode_cdp()',
257 'localtime': str(wallclock),
268 'Create a base pyAssimObj object'
270 if (Cstruct
is not None):
271 assert type(Cstruct)
is not int
281 'Convert this AssimObj into a printable string'
285 while (type(base)
is not AssimObj):
287 cstringret = base.toString(self.
_Cstruct)
288 ret = string_at(cstringret)
293 'Free up the underlying Cstruct for this pyAssimObj object.'
303 print >>sys.stderr,
"Attempt to free something already freed(%s)" % str(self.
_Cstruct)
304 traceback.print_stack()
310 while (hasattr(base,
'baseclass')):
312 return base._refcount
315 '''This class represents the Python version of our C-class @ref NetAddr - represented by the struct _NetAddr.
317 def __init__(self, addrstring, port=None, Cstruct=None):
318 '''This constructor needs a list of integers of the right length as its first argument.
319 The length of the list determines the type of address generated.
321 6 bytes == MAC address
322 8 bytes == MAC address
323 16 bytes == ipv6 address
324 This is slightly sleazy but it should work for the forseeable future.
329 if (Cstruct
is not None):
330 assert type(Cstruct)
is not int
331 pyAssimObj.__init__(self, Cstruct=Cstruct)
336 if port
is None: port = 0
338 if isinstance(addrstring, unicode)
or isinstance(addrstring, pyNetAddr):
339 addrstring = str(addrstring)
340 if isinstance(addrstring, str):
343 raise ValueError(
'Illegal NetAddr initial value: "%s"' % addrstring)
346 pyAssimObj.__init__(self, Cstruct=cs)
349 alen = len(addrstring)
350 addr = create_string_buffer(alen)
353 for i
in range(0, alen):
358 elif type(asi)
is unicode:
365 pyAssimObj.__init__(self, Cstruct=NA)
375 raise ValueError(
'Invalid address length - not 4, 6, 8, or 16')
378 'Return the port (if any) for this pyNetAddr object'
380 while (type(base)
is not NetAddr):
385 'Return the port (if any) for this pyNetAddr object'
387 while (type(base)
is not NetAddr):
392 'Return the type of address for this pyNetAddr object'
394 while (type(base)
is not NetAddr):
399 "Return the number of bytes necessary to represent this pyNetAddr object on the wire."
401 while (type(base)
is not NetAddr):
406 'Return True if this address is a local address'
408 while (type(base)
is not NetAddr):
413 'Return an equivalent IPv6 address to the one that was given. Guaranteed to be a copy'
415 while (type(base)
is not NetAddr):
417 newcs = cast(base.toIPv6(self.
_Cstruct), cClass.NetAddr)
418 return pyNetAddr(
None, Cstruct=newcs, port=port)
421 'Return a canonical representation of this NetAddr'
423 while (type(base)
is not NetAddr):
425 cstringret = base.canonStr(self.
_Cstruct)
426 ret = string_at(cstringret)
432 "Return True if the two pyNetAddrs are equal"
433 if not other._Cstruct
or not self.
_Cstruct:
436 while (type(base)
is not NetAddr):
438 return base.equal(self.
_Cstruct, other._Cstruct)
441 'Return a hash value for the given pyNetAddr'
445 while (type(base)
is not NetAddr):
451 '''This class represents the Python version of our C-class @ref Frame - represented by the struct _Frame.
452 This class is a base class for several different pyFrame subclasses.
453 Each of these various pyFrame subclasses have a corresponding C-class @ref Frame subclass.
454 The purpose of these pyFrames and their subclasses is to talk on the wire with our C code in our
457 Deliberately leaving out the updatedata() C-class member function - at least for mow.
458 I suspect that the Python code will only need the corresponding calls in a @ref FrameSet - which would
459 then update the corresponding @ref Frame member functions...
469 "Initializer for the pyFrame object."
472 frametype = initval.tlvtype
474 frametype = int(initval)
476 pyAssimObj.__init__(self, Cstruct=
frame_new(frametype, 0))
478 pyAssimObj.__init__(self, Cstruct=Cstruct)
481 "Return the TLV type for the pyFrame object."
483 while (type(base)
is not Frame):
488 "Return the length of this frame in bytes (TLV length)."
490 while (type(base)
is not Frame):
495 'Return a C-style pointer to the underlying raw TLV data (if any)'
497 while (type(base)
is not Frame):
499 return cast(base.value, c_char_p)
502 'Return a C-style pointer to the underlying raw TLV data (if any)'
504 while (type(base)
is not Frame):
506 return cast(base.value+base.length, c_char_p)
509 'Return the amount of space this frame needs - including type and length'
511 while (type(base)
is not Frame):
513 return base.dataspace(self.
_Cstruct)
516 "Return True if this Frame is valid"
518 while (type(base)
is not Frame):
520 pstart = pointer(cast(base.value, c_char_p))
523 return (int(base.isvalid(self.
_Cstruct,
None,
None)) != 0)
526 'Assign a chunk of memory to the Value portion of this Frame'
528 if type(value)
is str:
529 valbuf = create_string_buffer(vlen+1)
530 for i
in range(0, vlen):
533 valbuf[vlen] = chr(0)
536 valbuf = create_string_buffer(vlen)
537 for i
in range(0, vlen):
542 memmove(valptr, valbuf, vlen)
543 while (type(base)
is not Frame):
545 base.setvalue(self.
_Cstruct, valptr, vlen, cast(
None, GDestroyNotify))
548 'Dump out this Frame (using C-class "dump" member function)'
550 while (type(base)
is not Frame):
552 base.dump(self.
_Cstruct, cast(prefix, c_char_p))
556 while (type(base)
is not AssimObj):
558 cstringret = base.toString(self.
_Cstruct)
559 ret = string_at(cstringret)
561 return '%s: %s' % (FrameTypes.get(self.
frametype())[1] , ret)
565 frameptr = cast(frameptr, cClass.Frame)
567 frametype = frameptr[0].type
569 pyclassname =
"py" + Cclassname
570 if Cclassname ==
'NetAddr':
571 statement =
"%s(%d, None, Cstruct=cast(frameptr, cClass.%s))" % (pyclassname, frametype, Cclassname)
572 elif Cclassname == Cclassname ==
'IpPortFrame':
573 statement =
"%s(%d, None, None, Cstruct=cast(frameptr, cClass.%s))" % (pyclassname, frametype, Cclassname)
575 statement =
"%s(%d, Cstruct=cast(frameptr, cClass.%s))" % (pyclassname, frametype, Cclassname)
577 return eval(statement)
580 '''This class represents the Python version of our C-class AddrFrame - represented by the struct _AddrFrame.
582 def __init__(self, frametype, addrstring=None, port=None, Cstruct=None):
583 "Initializer for the pyAddrFrame object."
586 if isinstance(addrstring, pyNetAddr):
591 if addrstring
is not None:
592 Cstruct[0].setnetaddr(Cstruct, self._pyNetAddr._Cstruct)
595 assert addrstring
is None
596 addrlen = Cstruct[0].baseclass.length - 2
597 assert addrlen == 4
or addrlen == 6
or addrlen == 8
or addrlen == 16, (
"addrlen is %d" % addrlen)
598 addrstr = Cstruct[0].baseclass.value+2
599 addrstring = create_string_buffer(addrlen)
600 memmove(addrstring, addrstr, addrlen)
602 pyFrame.__init__(self, frametype, Cstruct=Cstruct)
605 return self._pyNetAddr.addrtype()
611 return (
"pyAddrFrame(%s, (%s))" % (FrameTypes.get(self.
frametype())[1], str(self.
_pyNetAddr)))
614 '''This class represents the Python version of our C-class IpPortFrame - represented by the struct _IpPortFrame.
616 def __init__(self, frametype, addrstring, port=None, Cstruct=None):
617 "Initializer for the pyIpPortFrame object."
620 if isinstance(addrstring, pyNetAddr):
624 raise ValueError(
"invalid initializer");
627 addrlen = len(addrstring)
630 raise ValueError(
"Invalid initializer.");
631 addrstr = create_string_buffer(addrlen)
632 for j
in range(0, addrlen):
633 addrstr[j] = chr(addrstring[j])
639 raise ValueError(
'Bad address length: %d' % addrlen)
642 raise ValueError(
"zero port");
644 raise ValueError(
"invalid initializer");
648 assert addrstring
is None
649 addrlen = Cstruct[0].baseclass.length - 4
650 if addrlen != 4
and addrlen != 16:
651 raise ValueError(
"Bad addrlen: %d" % addrlen)
652 port = Cstruct[0].port
654 addrstr = Cstruct[0].baseclass.value+4
655 addrstring = create_string_buffer(addrlen)
656 memmove(addrstring, addrstr, addrlen)
658 pyFrame.__init__(self, frametype, Cstruct=Cstruct)
661 return self._pyNetAddr.addrtype()
668 '''This class represents the Python version of our C-class CstringFrame - represented by the struct _CstringFrame
669 This class represents a Frame standard NUL-terminated C string.
671 def __init__(self, frametype, initval=None, Cstruct=None):
672 'Constructor for pyCstringFrame object - initial value should be something that looks a lot like a Python string'
675 pyFrame.__init__(self, frametype, Cstruct)
676 if initval
is not None:
681 while (
not hasattr(base,
'value')):
683 return string_at(base.value)
686 '''This class represents the Python version of our IntFrame C-class - represented by the struct _IntFrame
687 This class represents an integer of 1, 2, 3, 4 or 8 bytes.
689 def __init__(self, frametype, initval=None, intbytes=4, Cstruct=None):
690 'Constructor for pyIntFrame object - initial value should be something that looks a lot like an integer'
695 raise ValueError, (
"Invalid integer size (%d) in pyIntFrame constructor" % intbytes)
696 pyFrame.__init__(self, frametype, Cstruct=Cstruct)
697 if initval
is not None:
701 'Return the integer value of this pyIntFrame. (implemented by the underlying IntFrame object)'
705 'Return a string representation of this pyIntFrame (the integer value).'
706 return (
"pyIntFrame(%s, (%d))" % (FrameTypes.get(self.
frametype())[1], int(self)))
709 'Return the integer value of this pyIntFrame - same as __int__.'
713 '''Set the value of this pyIntFrame to the given integer value.
714 Note that this value is range checked by the underlying IntFrame implementation.
719 '''Return the number of bytes in the integer underlying this pyIntFrame object.
720 (implemented by underlying IntFrame object)'''
724 "Class for a Frame type we don't recognize"
726 'Initializer for pyUnknownFrame'
729 pyFrame.__init__(self, frametype, Cstruct)
732 'Class for a Sequence Number Frame - for reliable UDP packet transmission.'
733 def __init__(self, frametype, initval=None, Cstruct=None):
734 'Initializer for pySeqnoFrame'
740 raise ValueError,
"Constructor error for PySeqnoFrame()"
741 pyFrame.__init__(self, frametype, Cstruct=Cstruct)
742 if initval
is not None:
759 'Compare this pySeqnoFrame to another pySeqnoFrame'
761 while (type(lhsbase)
is not SeqnoFrame):
762 lhsbase=lhsbase.baseclass
763 return lhsbase.equal(self.
_Cstruct, rhs._Cstruct)
769 'Class for Digital Signature Frames - for authenticating data (subclasses will authenticate senders)'
771 'Initializer for pySignFrame'
776 raise ValueError, (
"Invalid checksum type (%s) for PySignFrame()" % gchecksumtype)
777 pyFrame.__init__(self, initval=FRAMETYPE_SIG, Cstruct=Cstruct)
780 'Class for a Frame containing a single name/value pair'
781 def __init__(self, frametype, name, value, Cstruct=None):
782 'Initializer for pyNVpairFrame'
787 raise ValueError, (
"Invalid NVpair initializer for pyNVPairFrame()")
788 pyFrame.__init__(self, initval=frametype, Cstruct=Cstruct)
791 'Return the name portion of a pyNVpairFrame'
792 return string_at(self.
_Cstruct[0].name)
795 'Return the name portion of a pyNVpairFrame'
796 return string_at(self.
_Cstruct[0].value)
801 'Class for Frame Sets - for collections of Frames making up a logical packet'
803 'Initializer for pyFrameSet'
806 pyAssimObj.__init__(self, Cstruct=Cstruct)
809 'Append a frame to the end of a @ref FrameSet'
813 'Prepend a frame before the first frame in a @ref FrameSet'
817 'Construct packet from curent frameset + special prefix frames'
820 if cryptframe
is not None:
821 cf = cryptframe._Cstruct
822 if compressframe
is not None:
823 cmpf = compressframe._Cstruct
827 'Return frameset type of this FrameSet'
831 'Return current flags for this FrameSet'
835 "'OR' the given flags into the set of flags for this FrameSet"
839 "Clear the given flags for this FrameSet"
843 'Dump out the given frameset'
848 raise ValueError,
"No packet constructed for frameset"
852 curframe = self.
_Cstruct[0].framelist
856 curframe=g_slist_next(curframe)
860 'Generator yielding the set of pyFrames in this pyFrameSet'
861 curframe = self.
_Cstruct[0].framelist
863 cast(curframe[0].data, struct__GSList._fields_[0][1])
864 yieldval = pyFrame.Cstruct2Frame(cast(curframe[0].data, cClass.Frame))
865 if not yieldval.isvalid():
866 print "OOPS! Constructed frame from iter() is not valid"
869 curframe=g_slist_next(curframe)
872 'Convert pyFrameSet to string'
875 for frame
in self.
iter():
876 result +=
'%s%s' % (comma, str(frame))
883 'Class for Decoding packets - for returning an array of FrameSets from a physical packet.'
885 'Initializer for pyPacketDecoder'
888 pyAssimObj.__init__(self, Cstruct=Cstruct)
891 'Make a list of FrameSets out of a packet.'
893 while (type(base)
is not PacketDecoder):
895 fs_gslistint = base.pktdata_to_framesetlist(self.
_Cstruct, pktlocation[0], pktlocation[1])
896 return pyPacketDecoder.fslist_to_pyfs_array(fs_gslistint)
900 'Converts a GSList of FrameSets to a python array of pyFrameSets'
901 fs_gslist = cast(listheadint, POINTER(GSList))
905 cfs = cast(curfs[0].data, cClass.FrameSet)
907 frameset_list.append(fs)
908 curfs = g_slist_next(curfs)
909 g_slist_free(fs_gslist)
913 'Class for Holding configuration information.'
916 'Initializer for pyConfigContext'
919 if (isinstance(init, str)
or isinstance(init, unicode)):
922 raise ValueError(
'Bad JSON [%s]' % str(init))
926 pyAssimObj.__init__(self, Cstruct=Cstruct)
928 for key
in init.keys():
929 self[key] = init[key]
933 'Return the integer associated with "name"'
937 'Set the integer associated with "name"'
941 'Return the boolean associated with "name"'
945 'Set the boolean associated with "name"'
949 'Return the NetAddr associated with "name"'
952 naddr = cast(naddr, cClass.NetAddr)
956 raise IndexError(
"No such NetAddr value [%s]" % name)
959 'Set the @ref NetAddr associated with "name"'
963 'Return the Frame associated with "name"'
967 return pyFrame.Cstruct2Frame(faddr)
968 raise IndexError(
"No such Frame value [%s]" % name)
971 'Set the @ref Frame associated with "name"'
975 'Return the pyConfigContext object associated with "name"'
978 caddr=cast(caddr, cClass.ConfigContext)
982 raise IndexError(
"No such ConfigContext value [%s]" % name)
985 'Set the @ref ConfigContext associated with "name"'
989 'Return the string associated with "name"'
992 return string_at(ret)
993 raise IndexError(
"No such String value [%s]" % name)
996 'Return the string associated with "name"'
1000 'Return the array value associated with "name"'
1007 data = cast(curlist[0].data, cClass.ConfigValue)
1013 curlist=g_slist_next(curlist)
1017 'Return the set of keys for this object'
1022 l.append(string_at(curkey[0].data))
1023 curkey=g_slist_next(curkey)
1024 g_slist_free(keylist)
1028 'return True if it has the given key'
1030 return ktype != CFG_EEXIST
1033 'return True if our object contains the given key'
1035 return ktype != CFG_EEXIST
1043 'Return a value associated with "name"'
1046 if ktype == CFG_EEXIST:
1047 traceback.print_stack()
1048 raise IndexError(
"No such value [%s] in [%s]" % (name, str(self)))
1049 if ktype == CFG_CFGCTX:
1051 if ktype == CFG_STRING:
1053 if ktype == CFG_NETADDR:
1055 if ktype == CFG_FRAME:
1057 if ktype == CFG_INT64:
1059 if ktype == CFG_BOOL:
1061 if ktype == CFG_ARRAY:
1067 'Set a value associated with "name" - in the appropriate table'
1068 if isinstance(value, str):
1070 if isinstance(value, pyNetAddr):
1071 return self.
setaddr(name, value)
1072 if isinstance(value, pyFrame):
1074 if isinstance(value, pyConfigContext):
1076 self.
setint(name, int(value))
1080 'Initializer for pyConfigValue - now a subclass of pyAssimObj'
1088 if vtype == CFG_BOOL:
1089 return self.
_Cstruct[0].u.intvalue != 0
1090 if vtype == CFG_INT64:
1091 return int(self.
_Cstruct[0].u.intvalue)
1092 if vtype == CFG_STRING:
1093 return str(self.
_Cstruct[0].u.strvalue)
1094 if vtype == CFG_FLOAT:
1095 return float(self.
_Cstruct[0].u.floatvalue)
1096 if vtype == CFG_CFGCTX:
1100 if vtype == CFG_NETADDR:
1105 if vtype == CFG_FRAME:
1107 ret = pyFrame.Cstruct2Frame(self.
_Cstruct[0].u.framevalue)
1109 if vtype == CFG_ARRAY:
1112 this = self.
_Cstruct[0].u.arrayvalue
1114 dataptr = cast(this[0].data, struct__GSList._fields_[0][1])
1115 dataptr = cast(dataptr, cClass.ConfigValue)
1119 this = g_slist_next(this)
1121 raise ValueError(
'Invalid valtype (%s)in pyConfigValue object' % self._Cstruct.valtype)
1124 def __init__(self, configobj, packetdecoder, Cstruct=None):
1125 'Initializer for pyNetIO'
1128 Cstruct=
netio_new(0, configobj._Cstruct, packetdecoder._Cstruct)
1133 while (
not hasattr(base,
'_configinfo')):
1136 CCref(base._configinfo)
1137 pyAssimObj.__init__(self, Cstruct=Cstruct)
1140 'Set this NetIO object to blocking IO mode'
1142 while (
not hasattr(base,
'setblockio')):
1144 return base.setblockio(self.
_Cstruct, int(mode))
1147 'Return the file descriptor for this pyNetIO object'
1149 while (
not hasattr(base,
'getfd')):
1154 'Bind the socket underneath this NetIO object to the given address'
1156 while (
not hasattr(base,
'bindaddr')):
1158 return base.bindaddr(self.
_Cstruct, addr._Cstruct, silent)
1161 'Return the socket underlying this NetIO object'
1163 while (
not hasattr(base,
'bindaddr')):
1165 boundaddr = base.boundaddr(self.
_Cstruct)
1167 ret =
pyNetAddr(
None, Cstruct=boundaddr)
1172 'Join the underlying socket to the given multicast address'
1174 while (
not hasattr(base,
'mcastjoin')):
1176 return base.mcastjoin(self.
_Cstruct, addr._Cstruct,
None)
1179 'Return the max packet size for this pyNetIO'
1181 while (
not hasattr(base,
'getmaxpktsize')):
1183 return base.getmaxpktsize(self.
_Cstruct)
1186 'Set the max packet size for this pyNetIO'
1188 while (
not hasattr(base,
'setmaxpktsize')):
1190 return base.setmaxpktsize(self.
_Cstruct, int(size))
1193 'Return the compression frame for this pyNetIO - may be None'
1196 while (
not hasattr(base,
'compressframe')):
1198 return base.compressframe(self.
_Cstruct)
1201 'Return the encryption frame for this pyNetIO - may be None'
1204 while (
not hasattr(base,
'cryptframe')):
1206 return base.cryptframe(self.
_Cstruct)
1209 'Return the digital signature frame for this pyNetIO'
1211 while (
not hasattr(base,
'signframe')):
1216 'Send the (collection of) frameset(s) out on this pyNetIO'
1217 if destaddr.port() == 0:
1218 raise ValueError(
"Zero Port in sendframesets: destaddr=%s" % str(destaddr))
1219 if not isinstance(framesetlist, collections.Sequence):
1220 framesetlist = (framesetlist, )
1222 while (
not hasattr(base,
'sendaframeset')):
1226 for frameset
in framesetlist:
1227 base.sendaframeset(self.
_Cstruct, destaddr._Cstruct, frameset._Cstruct)
1230 'Reliably send the (collection of) frameset(s) out on this pyNetIO (if possible)'
1231 if destaddr.port() == 0:
1232 raise ValueError(
"Zero Port in sendreliablefs: destaddr=%s" % str(destaddr))
1233 if not isinstance(framesetlist, collections.Sequence):
1234 framesetlist = (framesetlist, )
1236 while (
not hasattr(base,
'sendaframeset')):
1238 for frameset
in framesetlist:
1239 success = base.sendareliablefs(self.
_Cstruct, destaddr._Cstruct, qid, frameset._Cstruct)
1241 raise IOError(
"sendareliablefs(%s, %s) failed." % (destaddr, frameset))
1244 'ACK (acknowledge) this frameset - (presumably sent reliably).'
1247 while (
not hasattr(base,
'ackmessage')):
1249 base.ackmessage(self.
_Cstruct, destaddr._Cstruct, frameset._Cstruct)
1252 'Close (reset) our connection to this address'
1255 while (
not hasattr(base,
'closeconn')):
1257 base.closeconn(self.
_Cstruct, qid, destaddr._Cstruct)
1260 'Close (reset) our connection to this address'
1263 while (
not hasattr(base,
'closeconn')):
1265 base.addalias(self.
_Cstruct, fromaddr._Cstruct, toaddr._Cstruct)
1268 '''Receive a collection of framesets read from this pyNetIO - all from the same Address.
1269 @return The return value is a tuple (address, framesetlist). '''
1273 while (
not hasattr(base,
'recvframesets')):
1276 netaddr = cast(netaddrint, cClass.NetAddr)
1277 netaddr[0].baseclass.unref(netaddr)
1280 fs_gslistint = base.recvframesets(self.
_Cstruct, byref(netaddr))
1281 fslist = pyPacketDecoder.fslist_to_pyfs_array(fs_gslistint)
1282 if netaddr
and len(fslist) > 0:
1285 address =
pyNetAddr(
None, Cstruct=netaddr)
1289 return (address, fslist)
1293 return netio_is_dual_ipv4v6stack()
1296 'UDP version of the pyNetIO abstract base class'
1297 def __init__(self, config, packetdecoder, Cstruct=None):
1298 'Initializer for pyNetIOudp'
1301 Cstruct=
netioudp_new(0, config._Cstruct, packetdecoder._Cstruct)
1303 raise ValueError(
"Invalid parameters to pyNetIOudp constructor")
1304 pyNetIO.__init__(self, config, packetdecoder, Cstruct=Cstruct)
1307 'Reliable UDP version of the pyNetIOudp abstract base class'
1308 def __init__(self, config, packetdecoder, rexmit_timer_uS=0, Cstruct=None):
1309 'Initializer for pyReliableUDP'
1312 Cstruct=
reliableudp_new(0, config._Cstruct, packetdecoder._Cstruct, rexmit_timer_uS)
1314 raise ValueError(
"Invalid parameters to pyReliableUDP constructor")
1315 pyNetIOudp.__init__(self, config, packetdecoder, Cstruct=Cstruct)
1326 , address._Cstruct, 1)
1327 fs = cast(ucfs, cClass.FrameSet)