base.py 2.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright 2024 Max Planck Institute for Software Systems, and
# National University of Singapore
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Hejing Li's avatar
Hejing Li committed
23
from __future__ import annotations
Jakob Görgen's avatar
Jakob Görgen committed
24
import abc
25
import itertools
26

Jakob Görgen's avatar
Jakob Görgen committed
27
28
class System:
    """Defines System configuration of the whole simulation"""
29
30

    def __init__(self) -> None:
Jakob Görgen's avatar
Jakob Görgen committed
31
        self.hosts: list[Component] = []
32
33
34
35
36

    def add_component(self, c: Component) -> None:
        assert c.system == self


37
38
39
40
41
42
43
44
45
class IdObj(abc.ABC):
    __id_iter = itertools.count()

    def __init__(self):
        self._id = next(self.__id_iter)


class Component(IdObj):

46
    def __init__(self, s: System) -> None:
47
        super().__init__()
48
        self.system = s
49
        s.parameters = {}
50
        s.add_component(self)
51
        self.name: str = ""
52

Jakob Görgen's avatar
Jakob Görgen committed
53
54
55
    @abc.abstractmethod
    def interfaces(self) -> list[Interface]:
        return []
56

Jakob Görgen's avatar
Jakob Görgen committed
57
    def channels(self) -> list[Channel]:
58
59
60
        return [i.channel for i in self.interfaces() if i.is_connected()]


61
class Interface(IdObj):
62
    def __init__(self, c: Component) -> None:
63
        super().__init__()
64
        self.component = c
Jakob Görgen's avatar
Jakob Görgen committed
65
        self.channel: Channel | None = None
66
67
68
69
70
71
72
73
74
75
76
77

    def is_connected(self) -> bool:
        return self.channel is not None

    def disconnect(self) -> None:
        self.channel = None

    def connect(self, c: Channel) -> None:
        assert self.channel is None
        self.channel = c


78
class Channel(IdObj):
79
    def __init__(self, a: Interface, b: Interface) -> None:
80
        super().__init__()
81
82
83
84
        self.latency = 500
        self.a: Interface = a
        self.b: Interface = b

Jakob Görgen's avatar
Jakob Görgen committed
85
    def interfaces(self) -> list[Interface]:
86
87
88
89
90
91
        return [self.a, self.b]

    def disconnect(self):
        # Note AK: this is a bit ugly, this leaves the channel dangling. But
        # it's not referenced anywhere, so that's fine I guess.
        self.a.disconnect()
Jakob Görgen's avatar
Jakob Görgen committed
92
        self.b.disconnect()