Ejemplo n.º 1
0
    def _invoke(self, repopath, method, cls, params):
        dp = self.dp
        dp.reset_ack()

        if (not self.usercls and not self.userobj) or method != self.last_method:
            self.usercls = helpers.find_implementation(repopath, cls)
            self.userobj = self.usercls(data_provider = dp)

        self.last_method = method
        helpers.attach_parameters(dp, params)
        invoke_method(self.userobj, method, params)

        acks = dp.get_acks()
        s_acks = serialize(acks)
        s_acks = urllib.quote_plus(s_acks)
        return s_acks
Ejemplo 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()
Ejemplo 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()