import random
import tornado

from server.svc_pb2 import Packet
from server.zeromq import sender

task = None
svr_id = 0

PING_INTERVAL_SECS = 20


def send_ping():
    global svr_id

    pkt = Packet()
    pkt.client_id = '*'
    pkt.ping.number = svr_id
    sender.broadcast_services(pkt.SerializeToString())


def setup():
    global task
    global svr_id

    svr_id = random.randint(1, 9999999)
    task = tornado.ioloop.PeriodicCallback(
            send_ping,
            PING_INTERVAL_SECS * 1000)

    task.start()


def cleanup():
    global task
    task.stop()
