mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +01:00
tlv: break out TLVs into new subclass
make TLV messages their own subclass of Message. this makes other clean ups easier
This commit is contained in:
committed by
Rusty Russell
parent
13b5047a31
commit
e4658c241e
@@ -354,7 +354,7 @@ class CCode(object):
|
|||||||
|
|
||||||
|
|
||||||
class Message(object):
|
class Message(object):
|
||||||
def __init__(self, name, enum, comments, is_tlv):
|
def __init__(self, name, enum, comments, is_tlv=False):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.enum = enum
|
self.enum = enum
|
||||||
self.comments = comments
|
self.comments = comments
|
||||||
@@ -820,16 +820,22 @@ class Message(object):
|
|||||||
is_internal=('' if not is_tlv else 'static ')
|
is_internal=('' if not is_tlv else 'static ')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TlvMessage(Message):
|
||||||
|
def __init__(self, name, enum, comments):
|
||||||
|
super().__init__(name, enum, comments, is_tlv=True)
|
||||||
|
|
||||||
def print_struct(self):
|
def print_struct(self):
|
||||||
|
return TlvMessage._inner_print_struct('tlv_msg_' + self.name, self.fields)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _inner_print_struct(struct_name, fields):
|
||||||
""" returns a string representation of this message as
|
""" returns a string representation of this message as
|
||||||
a struct"""
|
a struct"""
|
||||||
if not self.is_tlv:
|
|
||||||
raise TypeError('{} is not a TLV-message').format(self.name)
|
|
||||||
|
|
||||||
fmt_fields = CCode()
|
fmt_fields = CCode()
|
||||||
for f in self.fields:
|
for f in fields:
|
||||||
if f.is_len_var or f.is_padding():
|
if f.is_len_var or f.is_padding():
|
||||||
# there is no ethical padding under TLVs
|
# there is no ethical padding under structs
|
||||||
continue
|
continue
|
||||||
elif f.is_variable_size():
|
elif f.is_variable_size():
|
||||||
fmt_fields.append('{} *{};'.format(f.fieldtype.name, f.name))
|
fmt_fields.append('{} *{};'.format(f.fieldtype.name, f.name))
|
||||||
@@ -838,8 +844,12 @@ class Message(object):
|
|||||||
else:
|
else:
|
||||||
fmt_fields.append('{} {};'.format(f.fieldtype.name, f.name))
|
fmt_fields.append('{} {};'.format(f.fieldtype.name, f.name))
|
||||||
|
|
||||||
return tlv_msg_struct_template.format(
|
return """
|
||||||
msg_name=self.name,
|
struct {struct_name} {{
|
||||||
|
{fields}
|
||||||
|
}};
|
||||||
|
""".format(
|
||||||
|
struct_name=struct_name,
|
||||||
fields=str(fmt_fields))
|
fields=str(fmt_fields))
|
||||||
|
|
||||||
|
|
||||||
@@ -849,13 +859,6 @@ tlv_message_towire_stub = """static void towire_{tlv_name}_{name}(u8 **p, struct
|
|||||||
}}
|
}}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
tlv_msg_struct_template = """
|
|
||||||
struct tlv_msg_{msg_name} {{
|
|
||||||
{fields}
|
|
||||||
}};
|
|
||||||
"""
|
|
||||||
|
|
||||||
tlv_struct_template = """
|
tlv_struct_template = """
|
||||||
struct {tlv_name} {{
|
struct {tlv_name} {{
|
||||||
{msg_type_structs}
|
{msg_type_structs}
|
||||||
@@ -1093,10 +1096,14 @@ for line in fileinput.input(options.files):
|
|||||||
is_tlv_msg = len(parts) == 3
|
is_tlv_msg = len(parts) == 3
|
||||||
if len(parts) == 2 or is_tlv_msg:
|
if len(parts) == 2 or is_tlv_msg:
|
||||||
# eg: commit_sig,132,(_tlv)
|
# eg: commit_sig,132,(_tlv)
|
||||||
|
if is_tlv_msg:
|
||||||
|
message = TlvMessage(parts[0],
|
||||||
|
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
|
||||||
|
comments)
|
||||||
|
else:
|
||||||
message = Message(parts[0],
|
message = Message(parts[0],
|
||||||
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
|
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
|
||||||
comments,
|
comments)
|
||||||
is_tlv_msg)
|
|
||||||
|
|
||||||
messages.append(message)
|
messages.append(message)
|
||||||
if is_tlv_msg:
|
if is_tlv_msg:
|
||||||
|
|||||||
Reference in New Issue
Block a user