import zmq
from typing import List

from mininet.common.node import Node
from mininet.models.models import Address


class Device(Node):
    def __init__(self):
        super().__init__()

    def init_socket(self, addr: Address | List[Address]):
        self.socket = self.context.socket(zmq.PUB)
        self.socket.bind(addr.format())

    def run(self):
        while True:
            if self.is_stopped():
                return
            else:
                self.socket.send_string("Device Name")

class User(Node):
    def __init__(self):
        super().__init__()
        self.device_list = []

    def init_socket(self, addr: Address | List[Address]):
        self.socket = self.context.socket(zmq.SUB)
        self.socket.connect(addr.format())
        self.socket.subscribe("")

    def run(self):
        while True:
            if self.is_stopped():
                return
            else:
                device = self.socket.recv_string()
                if device not in self.device_list:
                    self.device_list.append(device)

class Proxy(Node):
    def __init__(self):
        super().__init__()

    def init_socket(self, addr: Address | List[Address]):
        self.socket = [self.context.socket(zmq.SUB), self.context.socket(zmq.PUB)]
        self.socket[0].connect(addr[0].format())
        self.socket[0].subscribe("")
        self.socket[1].bind(addr[1].format())

    def run(self):
        while True:
            if self.is_stopped():
                return
            else:
                message = self.socket[0].recv_multipart()
                self.socket[1].send_multipart(message)
