import logging
import time
import zmq
from hedgehog.utils.discovery.node import Node

from . import handlers
from .hedgehog_server import HedgehogServer
from .handlers.hardware import HardwareHandler
from .handlers.process import ProcessHandler
from .hardware.serial import SerialHardwareAdapter

logger = logging.getLogger(__name__)


def start(name, hardware, port=0):
    ctx = zmq.Context.instance()
    service = 'hedgehog_server'

    handler = handlers.to_dict(HardwareHandler(hardware()), ProcessHandler())

    server = HedgehogServer('tcp://*:{}'.format(port), handler, ctx=ctx)
    logger.info("{} started on {}".format(name, server.socket.last_endpoint.decode('utf-8')))

    # TODO clean way to exit
    while True:
        logger.info("Starting Node for discovery...")
        node = Node(name, ctx)
        node.start()
        node.join(service)
        node.add_service(service, server.socket)

        while True:
            event = node.socket().recv_multipart()
            command = event.pop(0).decode('UTF-8')
            if command == "$TERM":
                node.stop()
                break
        logger.info("Node terminated (network gone?). Retry in 3 seconds...")
        time.sleep(3)
@@ -361,16 +361,47 @@
 , port=0
+, services=('hedgehog_server',)
 ):%0A    c
@@ -431,40 +431,8 @@
 ce()
-%0A    service = 'hedgehog_server'
 %0A%0A  
@@ -823,24 +823,61 @@
 ode.start()%0A
+        for service in services:%0A    
         node
@@ -891,16 +891,20 @@
 ervice)%0A
+    
         

add support for customizable services