66
77from pyof .foundation .base import GenericMessage , GenericStruct
88from pyof .foundation .basic_types import BinaryData , FixedTypeList , UBInt16
9+ from pyof .foundation .exceptions import PackException
910from pyof .v0x04 .common .header import Header , Type
1011
1112# Third-party imports
1213
13- __all__ = ('Hello' , 'HelloElemHeader' , 'HelloElemType' ,
14- 'HelloElemVersionbitmap' , 'ListOfHelloElements' )
14+ __all__ = ('Hello' , 'HelloElemHeader' , 'HelloElemType' , 'ListOfHelloElements' )
1515
1616# Enums
1717
@@ -29,10 +29,11 @@ class HelloElemType(IntEnum):
2929class HelloElemHeader (GenericStruct ):
3030 """Common header for all Hello Elements."""
3131
32- element_type = UBInt16 (enum_ref = HelloElemType )
32+ element_type = UBInt16 ()
3333 length = UBInt16 ()
34+ content = BinaryData ()
3435
35- def __init__ (self , element_type = None , length = None ):
36+ def __init__ (self , element_type = None , length = None , content = b'' ):
3637 """Create a HelloElemHeader with the optional parameters below.
3738
3839 Args:
@@ -43,6 +44,50 @@ def __init__(self, element_type=None, length=None):
4344 super ().__init__ ()
4445 self .element_type = element_type
4546 self .length = length
47+ self .content = content
48+
49+ def pack (self , value = None ):
50+ """Update the length and pack the massege into binary data.
51+
52+ Returns:
53+ bytes: A binary data that represents the Message.
54+
55+ Raises:
56+ Exception: If there are validation errors.
57+
58+ """
59+ if value is None :
60+ self .update_length ()
61+ return super ().pack ()
62+ elif isinstance (value , type (self )):
63+ return value .pack ()
64+ else :
65+ msg = "{} is not an instance of {}" .format (value ,
66+ type (self ).__name__ )
67+ raise PackException (msg )
68+
69+ def update_length (self ):
70+ """Update length attribute."""
71+ self .length = self .get_size ()
72+
73+ def unpack (self , buff = None , offset = 0 ):
74+ """Unpack *buff* into this object.
75+
76+ This method will convert a binary data into a readable value according
77+ to the attribute format.
78+
79+ Args:
80+ buff (bytes): Binary buffer.
81+ offset (int): Where to begin unpacking.
82+
83+ Raises:
84+ :exc:`~.exceptions.UnpackException`: If unpack fails.
85+
86+ """
87+ length = UBInt16 ()
88+ length .unpack (buff , offset = offset + 2 )
89+
90+ super ().unpack (buff [:offset + length .value ], offset )
4691
4792
4893class ListOfHelloElements (FixedTypeList ):
@@ -82,23 +127,3 @@ def __init__(self, xid=None, elements=None):
82127 """
83128 super ().__init__ (xid )
84129 self .elements = elements
85-
86-
87- class HelloElemVersionbitmap (HelloElemHeader ):
88- """Version bitmap Hello Element."""
89-
90- #: List of bitmaps - supported versions
91- bitmaps = BinaryData ()
92-
93- def __init__ (self , bitmaps = b'' ):
94- """Create a HelloElemVersionbitmap with the optional parameters below.
95-
96- Args:
97- bitmaps(BinaryData): A BinaryData with exactly (length - 4) bytes
98- containing the bitmaps, then exactly
99- (length + 7)/8*8 - (length) (between 0 and 7)
100- bytes of all-zero bytes.
101- """
102- super ().__init__ (element_type = HelloElemType .OFPHET_VERSIONBITMAP ,
103- length = None )
104- self .bitmaps = bitmaps
0 commit comments