File: eiproto.py.tmpl

package info (click to toggle)
libei 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,916 kB
  • sloc: ansic: 23,868; python: 2,712; xml: 1,243; sh: 142; makefile: 63; cpp: 12; lisp: 2
file content (347 lines) | stat: -rw-r--r-- 12,110 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
#
# GENERATED FILE, DO NOT EDIT
#
# SPDX-License-Identifier: MIT
#

{#- this is a jinja template, warning above is for the generated file

   Non-obvious variables set by the scanner that are used in this template:
   - target: because eis is actually eis_client in the code, the target points to
     either "ei" or "eis_client". The various attributes on target resolve
     accordingly.
   - request.fqdn/event.fqdn - the full name of a request/event with the
     interface name prefixed, "ei_foo_request_bar" or "ei_foo_event_bar"
   - incoming/outgoing: points to the list of requests or events, depending
     which one is the outgoing one from the perspective of the file we're
     generating (ei or eis)
#}

from typing import Any, Callable, Generator, Tuple
from enum import IntEnum
from dataclasses import dataclass, field
try:
    from enum import StrEnum
except ImportError:
    from strenum import StrEnum

import binascii
import itertools
import logging
import struct
import structlog
import time

# type aliases
s = str
i = int
u = int
x = int
t = int
o = int
n = int
f = float
h = int  # FIXME this should be a file-like object

logger = structlog.get_logger()


def hexlify(data):
    return binascii.hexlify(data, sep=" ", bytes_per_sep=4)


class ObjectId(int):
    def __repr__(self) -> str:
        return f"{self:#x}"


@dataclass
class MethodCall:
    name: str
    args: dict[str, Any]
    objects: dict[str, "Interface"] = field(default_factory=dict)
    timestamp: float = field(default_factory=time.time)


@dataclass
class MessageHeader:
    object_id: ObjectId
    msglen: int
    opcode: int

    @classmethod
    def size(cls) -> int:
        return 16

    @classmethod
    def from_data(cls, data: bytes) -> "MessageHeader":
        object_id, msglen, opcode = struct.unpack("=QII", data[:cls.size()])
        return cls(ObjectId(object_id), msglen, opcode)

    @property
    def as_tuple(self) -> Tuple[int, int, int]:
        return self.object_id, self.msglen, self.opcode


@dataclass
class Context:
    objects: dict[str, "Interface"] = field(default_factory=dict)
    _callbacks: dict[str, dict[int, Callable]] = field(
        init=False,
        default_factory=lambda: { "register": {}, "unregister": {}}
    )
    _ids: Generator = field(init=False, default_factory=itertools.count)

    def register(self, object: "Interface") -> None:
        assert object.object_id not in self.objects
        logger.debug(f"registering object", interface=object.name, object_id=f"{object.object_id:#x}")
        self.objects[object.object_id] = object
        for cb in self._callbacks["register"].values():
            cb(object)

    def unregister(self, object: "Interface") -> None:
        assert object.object_id in self.objects
        logger.debug(f"unregistering object", interface=object.name, object=object, object_id=f"{object.object_id:#x}")
        del self.objects[object.object_id]
        for cb in self._callbacks["unregister"].values():
            cb(object)

    def connect(self, signal: str, callback: Callable) -> int:
        cbs = self._callbacks[signal]
        id = next(self._ids)
        cbs[id] = callback
        return id

    def disconnect(self, signal: str, id: int) -> None:
        del self._callbacks[signal][id]

    def dispatch(self, data: bytes) -> None:
        if len(data) < MessageHeader.size():
            return

        header = MessageHeader.from_data(data)
        object_id, opcode, msglen = header.object_id, header.opcode, header.msglen
        logger.debug(f"incoming packet ({msglen} bytes)", object_id=f"{object_id:x}", opcode=opcode, bytes=hexlify(data[:msglen]))

        try:
            dispatcher = self.objects[object_id]
        except KeyError:
            logger.error("Message from unknown object", object_id=f"{object_id:x}")
            return msglen

        try:
            logger.debug(f"incoming packet: dispatching", func=f"{dispatcher.name}.{dispatcher.incoming[opcode]}()", object=dispatcher)
        except KeyError:
            logger.error("Invalid opcode for object", object_id=f"{object_id:x}", opcode=opcode)
            return msglen
        consumed = dispatcher.dispatch(data, context=self)
        return consumed

    @classmethod
    def create(cls) -> "Context":
        o = cls()
        o.register(EiHandshake.create(object_id=0, version=1))
        return o


class InterfaceName(StrEnum):
{% for interface in interfaces %}
    {{ interface.name.upper() }} = "{{interface.protocol_name}}"
{% endfor %}


@dataclass(eq=False)
class Interface:
    object_id: int
    version: int
    callbacks: dict[str, Callable] = field(init=False, default_factory=dict, repr=False)
    calllog: list[MethodCall] = field(init=False, default_factory=list, repr=False)
    name: str = field(default="<overridden by subclass>")
    incoming: dict[int, str] = field(default_factory=list, repr=False)
    outgoing: dict[int, str] = field(default_factory=list, repr=False)

    def format(self, *args, opcode: int, signature: str) -> bytes:
        encoding = ["=QII"]
        arguments = []
        for sig, arg in zip(signature, args):
            if sig in ["u"]:
                encoding.append("I")
            elif sig in ["i"]:
                encoding.append("i")
            elif sig in ["f"]:
                encoding.append("f")
            elif sig in ["n", "o", "t"]:
                encoding.append("Q")
            elif sig in ["x"]:
                encoding.append("q")
            elif sig in ["s"]:
                encoding.append("I")
                arguments.append(len(arg) + 1)
                slen = ((len(arg) + 1 + 3) // 4) * 4
                encoding.append(f"{slen}s")
                arg = arg.encode("utf8")
            elif sig in ["h"]:
                raise NotImplementedError("fd passing is not yet supported here")

            arguments.append(arg)

        format = "".join(encoding)
        length = struct.calcsize(format)
        header = MessageHeader(self.object_id, length, opcode)
        # logger.debug(f"Packing {encoding}: {arguments}")
        return struct.pack(format, *header.as_tuple, *arguments)

    def unpack(self, data, signature: str, names: list[str]) -> Tuple[int, dict[str, Any]]:
        encoding = ["=QII"]  # the header
        for sig in signature:
            if sig in ["u"]:
                encoding.append("I")
            elif sig in ["i"]:
                encoding.append("i")
            elif sig in ["f"]:
                encoding.append("f")
            elif sig in ["x"]:
                encoding.append("q")
            elif sig in ["n", "o", "t"]:
                encoding.append("Q")
            elif sig in ["s"]:
                length_so_far = struct.calcsize("".join(encoding))
                slen, = struct.unpack("I", data[length_so_far:length_so_far + 4])
                slen = ((slen + 3) // 4) * 4
                encoding.append(f"I{slen}s")
            elif sig in ["h"]:
                raise NotImplementedError("fd passing is not yet supported here")

        format = "".join(encoding)
        msglen = struct.calcsize(format)
        try:
            values = list(struct.unpack(format, data[:msglen]))
        except struct.error as e:
            logger.error(f"{e}", bytes=hexlify(data), length=len(data), encoding=format)
            raise e

        # logger.debug(f"unpacked {format} to {values}")

        results = []
        values = values[3:]  # drop id, length, opcode

        # we had to insert the string length into the format, filter the
        # value for that out again.
        for sig in signature:
            if sig in ["s"]:
                values.pop(0)
                s = values.pop(0)
                if not s:
                    s = None  # zero-length string is None
                else:
                    s = s.decode("utf8").rstrip("\x00")  # strip trailing zeroes
                results.append(s)
            else:
                results.append(values.pop(0))

        # First two values are object_id and len|opcode
        return (msglen, { name: value for name, value in zip(names, results) })

    def connect(self, event: str, callback: Callable):
        self.callbacks[event] = callback

    @classmethod
    def lookup(cls, name: str) -> "Interface":
        return {
{% for interface in interfaces %}
            "{{interface.name}}": {{interface.camel_name}},
{% endfor %}
        }[name]


{% for interface in interfaces %}
@dataclass
class {{interface.camel_name}}(Interface):
    {% for enum in interface.enums %}
    class {{component.capitalize()}}{{enum.camel_name}}(IntEnum):
        {% for entry in enum.entries %}
        {{entry.name.upper()}} = {{entry.value}}
        {% endfor %}
    {% endfor %}


    {% for outgoing in interface.outgoing %}
    def {{outgoing.camel_name}}(self{%- for arg in outgoing.arguments %}, {{arg.name}}: {{arg.signature}}{% endfor -%}) -> bytes:
        data = self.format({%- for arg in outgoing.arguments %}{{arg.name}}, {% endfor -%}opcode={{outgoing.opcode}}, signature="{{outgoing.signature}}")
        logger.debug("composing message", oject=self, func="{{interface.name}}.{{outgoing.name}}", args={ {%- for arg in outgoing.arguments %}"{{arg.name}}": {{arg.name}}, {% endfor -%} }, result=hexlify(data))
        return data

    {% endfor %}
    {% for incoming in interface.incoming %}
    def on{{incoming.camel_name}}(self, context: Context{%- for arg in incoming.arguments %}, {{arg.name}}: {{arg.signature}}{% endfor -%}):
        new_objects = {
        {% for arg in incoming.arguments %}
        {% if arg.signature == "n" %}
            {% if arg.interface %}
            "{{arg.name}}": {{arg.interface.camel_name}}.create({{arg.name}}, {{arg.version_arg.name}}),
            {% else %}
            "{{arg.name}}": Interface.lookup(interface_name).create({{arg.name}}, {{arg.version_arg.name}}),
            {% endif %}
        {% endif %}
        {% endfor %}
        }

        for o in new_objects.values():
            context.register(o)

        cb = self.callbacks.get("{{incoming.camel_name}}", None)
        if cb is not None:
            if new_objects:
                cb(self{%- for arg in incoming.arguments %}, {{arg.name}}{% endfor -%}, new_objects=new_objects)
            else:
                cb(self{%- for arg in incoming.arguments %}, {{arg.name}}{% endfor -%})

        m = MethodCall(name="{{incoming.camel_name}}", args={
            {% for arg in incoming.arguments %}
            "{{arg.name}}": {{arg.name}},
            {% endfor %}
            }, objects=new_objects)
        self.calllog.append(m)

        {% if incoming.is_destructor %}
        context.unregister(self)
        {% endif %}

    {% endfor %}
    def dispatch(self, data: bytes, context: Context) -> int:
        header = MessageHeader.from_data(data)
        object_id, opcode = header.object_id, header.opcode
        if False:
            pass
        {% for incoming in interface.incoming %}
        elif opcode == {{incoming.opcode}}:
            consumed, args = self.unpack(data, signature="{{incoming.signature}}", names=[
                    {% for arg in incoming.arguments %}
                    "{{arg.name}}",
                    {% endfor %}
                    ])
            logger.debug("dispatching", object=self, func="{{incoming.camel_name}}", args=args)
            self.on{{incoming.camel_name}}(context, **args)
        {% endfor %}
        else:
            raise NotImplementedError(f"Invalid opcode {opcode}")

        return consumed

    @classmethod
    def create(cls, object_id: int, version: int):
        incoming = {
        {% for incoming in interface.incoming %}
            {{incoming.opcode}}: "{{incoming.name}}",
        {% endfor %}
        }
        outgoing = {
        {% for outgoing in interface.outgoing %}
            {{outgoing.opcode}}: "{{outgoing.name}}",
        {% endfor %}
        }
        return cls(object_id=object_id, version=version, name="{{interface.name}}", incoming=incoming, outgoing=outgoing)


{% endfor %}