#!/usr/bin/env python
# -*- coding: utf8 -*-

from functools import partial
import os
import sys

sys.path.append(os.getcwd() + '/../../')

import tornado.ioloop

from tornadoyyproto.app import App
from tornadoyyproto.client import Client

from echo import Echo


class EchoApp(App):
    def __init__(self):
        App.__init__(self)
        self.register(Echo, self.OnEcho)

    def OnEcho(self, connection, message):
        print(connection, message.msg)
        tornado.ioloop.IOLoop.instance().stop()


def callback(client, msg):
    client.connection.send(msg)


def main():
    app = EchoApp()

    echo = Echo()
    echo.msg = '1931'

    client = Client(app)
    #client.connect('127.0.0.1', 1931, partial(callback, client, echo))
    client.connect('127.0.0.1', 1327, partial(callback, client, echo))

    tornado.ioloop.IOLoop.instance().start()


if __name__ == '__main__':
    main()
