try:
    host = sys.argv[1]
except IndexError:
    print("Usage: {0} hostname".format(sys.argv[0]))
    sys.exit(1)


sys.path.insert(0, "src/main/python")
from yadtbroadcastclient import WampBroadcaster
from time import time

test_id = str(int(time()))

w = WampBroadcaster(host, "8080", test_id)
w.onEvent = partial(print, "Got event ")


def connected():
    reactor.connected = True


def send_full_update_and_service_change():
    w.sendServiceChange([{'uri': "service://foo/bar", 'state': "UP"}])
    w.sendFullUpdate(dummy_data)
    reactor.callLater(5, reactor.stop)


def ensure_test_data_is_present():
    tc = unittest.TestCase('__init__')
    logger.info("Checking full target API..")
    try:
        verify_if_is_expected_event(event)
    except Exception as e:
        logger.error(e)


def verify_if_is_expected_event(event):
    if expected_event == event:
        logger.info("Success: found target event %s" % expected_event)
        global exit_code
        exit_code = 0
        reactor.stop()
    else:
        logger.info("Expected message not yet found")


def timeout(timeout_in_seconds):
    logger.error("Timed out after %d seconds" % timeout_in_seconds)
    reactor.stop()


wamp = WampBroadcaster(host, "8080", "topic")
wamp.onEvent = onEvent

logger.info("Waiting %d seconds for target event %s" %
            (timeout_in_seconds, expected_event))

reactor.callLater(timeout_in_seconds, timeout, timeout_in_seconds)
reactor.run()
sys.exit(exit_code)