Exemplo n.º 1
0
    def invoke(self, repopath, data):
        data = urllib.unquote_plus(data)
        method, cls, params = self._parse_params(data)

        try:
            if method == 'ping':
                return 'PONG'
            elif method == 'get_implementation_info':
                info = helpers.get_implementation_info(repopath)
                s_info = serialize(info)
                return s_info
            else:
                return self._invoke(repopath, method, cls, params)

        except Exception, e:
            info = {
                '__error__': helpers.get_error_info(e)
            }

            s_info = serialize(info)
            s_info = urllib.quote_plus(s_info)
            return s_info
Exemplo n.º 2
0
def run(hostname, calculations=False, backtest=False, portfolio_manager=False, port=consts.PORT):
    from socket import socket, AF_INET, SOCK_STREAM
    from dataproviders import ClientDataProvider

    # import inspect
    import json

    # frm = inspect.stack()[1]
    # mod = inspect.getmodule(frm[0])
    # filepath = os.path.dirname(os.path.abspath(mod.__file__))
    projpath = os.getcwd()

    s = socket(AF_INET, SOCK_STREAM)
    s.connect((hostname, port))

    atexit.register(close_socket_connection, s)

    "signal to start execution"

    sendmsg = mlprotocol.create_message(
        json.dumps({"calculations": calculations, "backtest": backtest, "portfolio_manager": portfolio_manager})
    )
    s.send(sendmsg)

    userobj = None
    socket_dataprovider = ClientDataProvider(socket=s)

    "receive data"
    while True:
        data = mlprotocol.get_message(s)
        # data = s.recv(consts.BUFFER_SIZE)

        if data == consts.STOP_FLAG:
            print "STOPPED"
            break

        socket_dataprovider.reset_ack()

        params = deserialize(data, dotted=True)
        invocation = params.get("_invocation")
        method = invocation["method"]
        cls = invocation["class"]

        if method == "ping":
            return "PONG"
        elif method == "get_implementation_info":
            info = helpers.get_implementation_info(projpath)
            s_info = serialize(info)
            socket_dataprovider.ack_data(s_info)
            socket_dataprovider.send_ack()
            continue

        if not userobj:
            usercls = helpers.find_implementation(projpath, cls)
            userobj = usercls(data_provider=socket_dataprovider)

        helpers.attach_parameters(socket_dataprovider, params)
        invoke_method(userobj, method, params)

        "send acknowledgement to the server"
        socket_dataprovider.send_ack()
Exemplo n.º 3
0
def run(hostname,
        calculations=False,
        backtest=False,
        portfolio_manager=False,
        port=consts.PORT):
    from socket import socket, AF_INET, SOCK_STREAM
    from dataproviders import ClientDataProvider
    # import inspect
    import json

    # frm = inspect.stack()[1]
    # mod = inspect.getmodule(frm[0])
    # filepath = os.path.dirname(os.path.abspath(mod.__file__))
    projpath = os.getcwd()

    s = socket(AF_INET, SOCK_STREAM)
    s.connect((hostname, port))

    atexit.register(close_socket_connection, s)

    "signal to start execution"

    sendmsg = mlprotocol.create_message(
        json.dumps({
            'calculations': calculations,
            'backtest': backtest,
            'portfolio_manager': portfolio_manager
        }))
    s.send(sendmsg)

    userobj = None
    socket_dataprovider = ClientDataProvider(socket=s)

    "receive data"
    while True:
        data = mlprotocol.get_message(s)
        # data = s.recv(consts.BUFFER_SIZE)

        if data == consts.STOP_FLAG:
            print 'STOPPED'
            break

        socket_dataprovider.reset_ack()

        params = deserialize(data, dotted=True)
        invocation = params.get('_invocation')
        method = invocation['method']
        cls = invocation['class']

        if method == 'ping':
            return 'PONG'
        elif method == 'get_implementation_info':
            info = helpers.get_implementation_info(projpath)
            s_info = serialize(info)
            socket_dataprovider.ack_data(s_info)
            socket_dataprovider.send_ack()
            continue

        if not userobj:
            usercls = helpers.find_implementation(projpath, cls)
            userobj = usercls(data_provider=socket_dataprovider)

        helpers.attach_parameters(socket_dataprovider, params)
        invoke_method(userobj, method, params)

        "send acknowledgement to the server"
        socket_dataprovider.send_ack()