# Path: core/interfaces/base.py
# class BaseInterface(ABC, Module):
# 
#     def  __init__(self, hplayer, name="INTERFACE", color="blue"):
#         super().__init__(hplayer, name, color)
#         self.hplayer = hplayer
# 
#         # stopping flag
#         self.stopped = threading.Event()
#         self.stopped.set()
# 
#         # Listen thread
#         self.recvThread = threading.Thread(target=self.listen)
# 
# 
#     # Receiver THREAD (ABSTRACT)
#     @abstractmethod
#     def listen(self):
#         self.stopped.wait()
# 
#     # Start
#     def start(self):
#         self.stopped.clear()
#         self.recvThread.start()
#         return self
# 
#     # Stop
#     def quit(self):
#         self.log("stopping...")
#         self.stopped.set()
#         self.recvThread.join()
#         self.log("stopped")
# 
# 	# is Running
#     def isRunning(self, state=None):
#         if state is not None:
#             self.stopped.clear() if state else self.stopped.set()
#         return not self.stopped.is_set()
# 
# Path: core/engine/network.py
# def get_ip(iface=None):
# def get_allip():
# def get_broadcast(iface=None):
# def get_hostname():
# def get_ethmac():
# def get_essid(iface):
# def get_rssi(iface):
# 
# Path: core/engine/network.py
# def get_allip():
#     ip = []
#     ifaces = ni.interfaces()
#     for iface in ifaces:
#         if iface.startswith("e"):
#             try:
#                 ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#             except:
#                 pass
#     else:
#         for iface in ifaces:
#             if iface.startswith("w"):
#                 try:
#                     ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#                 except:
#                     pass
#     return ip
# 
# def get_hostname():
#     import socket
#     return socket.gethostname()


from .base import BaseInterface
from core.engine import network
from sys import getsizeof
from ..engine.network import get_allip, get_hostname
from zeroconf import ServiceInfo, Zeroconf
import liblo
import random, time
import socket
        # OSC trigger decorator
        def osc(path, argsTypes=None):
            def handler(func):
                if self.isRunning():
                    # oscServer.add_method(path, argsTypes, oscdump)
                    oscServer.add_method(path, argsTypes, func)
                return func
            return handler

        @osc("/burst")
        def burst(path, args, types):
            if len(args) < 2: return
            mac = args.pop(0)
            stamp = args.pop(0)
            if not mac in self.burstMem or self.burstMem[mac] != stamp:
                self.burstMem[mac] = stamp
                path = args.pop(0)
                types = types[2:]
                # print("relaying bursted frame", path, args, types)
                target = liblo.Address("osc.udp://127.0.0.1:"+str(self._portIn))
                liblo.send(target, path, *args)

        @osc(None, None)
        def fallback(path, args, types, src):
            self.emit(path[1:], *args)

        # Advertize on ZeroConf
        zeroconf = Zeroconf()
        info = ServiceInfo(
            "_osc._udp.local.",

# Path: core/interfaces/base.py
# class BaseInterface(ABC, Module):
# 
#     def  __init__(self, hplayer, name="INTERFACE", color="blue"):
#         super().__init__(hplayer, name, color)
#         self.hplayer = hplayer
# 
#         # stopping flag
#         self.stopped = threading.Event()
#         self.stopped.set()
# 
#         # Listen thread
#         self.recvThread = threading.Thread(target=self.listen)
# 
# 
#     # Receiver THREAD (ABSTRACT)
#     @abstractmethod
#     def listen(self):
#         self.stopped.wait()
# 
#     # Start
#     def start(self):
#         self.stopped.clear()
#         self.recvThread.start()
#         return self
# 
#     # Stop
#     def quit(self):
#         self.log("stopping...")
#         self.stopped.set()
#         self.recvThread.join()
#         self.log("stopped")
# 
# 	# is Running
#     def isRunning(self, state=None):
#         if state is not None:
#             self.stopped.clear() if state else self.stopped.set()
#         return not self.stopped.is_set()
# 
# Path: core/engine/network.py
# def get_ip(iface=None):
# def get_allip():
# def get_broadcast(iface=None):
# def get_hostname():
# def get_ethmac():
# def get_essid(iface):
# def get_rssi(iface):
# 
# Path: core/engine/network.py
# def get_allip():
#     ip = []
#     ifaces = ni.interfaces()
#     for iface in ifaces:
#         if iface.startswith("e"):
#             try:
#                 ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#             except:
#                 pass
#     else:
#         for iface in ifaces:
#             if iface.startswith("w"):
#                 try:
#                     ip.append(ni.ifaddresses(iface)[AF_INET][0]['addr'])
#                 except:
#                     pass
#     return ip
# 
# def get_hostname():
#     import socket
#     return socket.gethostname()


# Path: core/interfaces/osc.py
from .base import BaseInterface
from core.engine import network
from sys import getsizeof
from ..engine.network import get_allip, get_hostname
from zeroconf import ServiceInfo, Zeroconf
import liblo
import random, time
import socket

        # OSC trigger decorator
        def osc(path, argsTypes=None):
            def handler(func):
                if self.isRunning():
                    # oscServer.add_method(path, argsTypes, oscdump)
                    oscServer.add_method(path, argsTypes, func)
                return func
            return handler

        @osc("/burst")
        def burst(path, args, types):
            if len(args) < 2: return
            mac = args.pop(0)
            stamp = args.pop(0)
            if not mac in self.burstMem or self.burstMem[mac] != stamp:
                self.burstMem[mac] = stamp
                path = args.pop(0)
                types = types[2:]
                # print("relaying bursted frame", path, args, types)
                target = liblo.Address("osc.udp://127.0.0.1:"+str(self._portIn))
                liblo.send(target, path, *args)

        @osc(None, None)
        def fallback(path, args, types, src):
            self.emit(path[1:], *args)

        # Advertize on ZeroConf
        zeroconf = Zeroconf()
        info = ServiceInfo(
            "_osc._udp.local.",

            "OSC input._"+get_hostname()+"._osc._udp.local.",