Esempio n. 1
0
 def __on_close(self, ws):
     Log.info(self.__class__.__name__, "Socket <%s> is closed." % self.id)
     self._connecting = False
     self._connected = False
     if len(self.on_close_handlers) > 0:
         for handler in self.on_close_handlers:
             handler(ws)
Esempio n. 2
0
 def on_close_handler(self, instmt, ws):
     """
     Socket on close handler
     :param instmt: Instrument
     :param ws: Web socket
     """
     Logger.info(self.__class__.__name__, "Instrument %s is unsubscribed in channel %s" % \
                 (instmt.get_instmt_name(), instmt.get_exchange_name()))
     instmt.set_subscribed(False)
Esempio n. 3
0
 def connect(self, **kwargs):
     """
     Connect
     :param path: sqlite file to connect
     """
     addr = kwargs['addr']
     self.conn.bind(addr)
     Logger.info(self.__class__.__name__,
                 'Zmq client is connecting to %s' % addr)
     return self.conn is not None
Esempio n. 4
0
 def start(self, instmt):
     self.init_instmt_snapshot_table(instmt)
     Logger.info(
         self.__class__.__name__,
         'instmt snapshot table: {}'.format(instmt.get_instmt_code()))
     return [
         self.api_socket.connect(
             self.api_socket.get_link(),
             on_message_handler=partial(self.on_message_handler, instmt),
             on_open_handler=partial(self.on_open_handler, instmt),
             on_close_handler=partial(self.on_close_handler, instmt))
     ]
Esempio n. 5
0
    def connect(self,
                url,
                on_message_handler=None,
                on_open_handler=None,
                on_close_handler=None,
                on_error_handler=None,
                reconnect_interval=10):
        """
        :param url: Url link
        :param on_message_handler: Message handler which take the message as
                           the first argument
        :param on_open_handler: Socket open handler which take the socket as
                           the first argument
        :param on_close_handler: Socket close handler which take the socket as
                           the first argument
        :param on_error_handler: Socket error handler which take the socket as
                           the first argument and the error as the second
                           argument
        :param reconnect_interval: The time interval for reconnection
        """
        Log.info(self.__class__.__name__,
                 "Connecting to socket <%s>..." % self.id)
        if on_message_handler is not None:
            self.on_message_handlers.append(on_message_handler)
        if on_open_handler is not None:
            self.on_open_handlers.append(on_open_handler)
        if on_close_handler is not None:
            self.on_close_handlers.append(on_close_handler)
        if on_error_handler is not None:
            self.on_error_handlers.append(on_error_handler)
        if not self._connecting and not self._connected:
            self._connecting = True
            self.ws = websocket.WebSocketApp(url,
                                             on_message=self.__on_message,
                                             on_close=self.__on_close,
                                             on_open=self.__on_open,
                                             on_error=self.__on_error)
            self.wst = threading.Thread(target=lambda: self.__start(
                reconnect_interval=reconnect_interval))
            self.wst.start()

        return self.wst
Esempio n. 6
0
    def create(self, table, columns, types, primary_key_index=(), is_ifnotexists=True):
        """
        Create table in the database
        :param table: Table name
        :param columns: Column array
        :param types: Type array
        :param is_ifnotexists: Create table if not exists keyword
        """
        file_path = os.path.join(self.file_directory, table + ".csv")
        columns = [e.split(' ')[0] for e in columns]
        if len(columns) != len(types):
            return False

        self.lock.acquire()
        if os.path.isfile(file_path):
            Logger.info(self.__class__.__name__, "File (%s) has been created already." % file_path)
        else:
            with open(file_path, 'w+') as csvfile:
                csvfile.write(','.join(["\"" + e + "\"" for e in columns])+'\n')

        self.lock.release()
        return True
Esempio n. 7
0
 def on_open_handler(self, instmt, ws):
     """
     Socket on open handler
     :param instmt: Instrument
     :param ws: Web socket
     """
     Logger.info(self.__class__.__name__, "Instrument %s is subscribed in channel %s" % \
                 (instmt.get_instmt_name(), instmt.get_exchange_name()))
     if not instmt.get_subscribed():
         Logger.info(
             self.__class__.__name__, 'last trade string:{}'.format(
                 self.api_socket.get_last_trades_subscription_string(
                     instmt)))
         Logger.info(
             self.__class__.__name__, 'ticker string:{}'.format(
                 self.api_socket.get_ticker_subscription_string(instmt)))
         #ws.send(self.api_socket.get_last_trades_subscription_string(instmt))
         ws.send(self.api_socket.get_ticker_subscription_string(instmt))
         instmt.set_subscribed(True)
Esempio n. 8
0
 def __start(self, reconnect_interval=10):
     while True:
         self.ws.run_forever()
         Log.info(self.__class__.__name__,
                  "Socket <%s> is going to reconnect..." % self.id)
         time.sleep(reconnect_interval)
Esempio n. 9
0
 def __on_error(self, ws, error):
     Log.info(self.__class__.__name__,
              "Socket <%s> error:\n %s" % (self.id, error))
     if len(self.on_error_handlers) > 0:
         for handler in self.on_error_handlers:
             handler(ws, error)
Esempio n. 10
0
 def __on_open(self):
     Log.info(self.__class__.__name__, "Socket <%s> is opened." % self.id)
     self._connected = True
     if len(self.on_open_handlers) > 0:
         for handler in self.on_open_handlers:
             handler(self.ws)
Esempio n. 11
0
               isFetchAll=True):
        """
        Select rows from the table
        :param table: Table name
        :param columns: Selected columns
        :param condition: Where condition
        :param orderby: Order by condition
        :param limit: Rows limit
        :param isFetchAll: Indicator of fetching all
        :return Result rows
        """
        return []

    def delete(self, table, condition='1==1'):
        """
        Delete rows from the table
        :param table: Table name
        :param condition: Where condition
        """
        return True


if __name__ == '__main__':
    Logger.init_log()
    db_client = ZmqClient()
    db_client.connect(addr='tcp://127.0.0.1:5000')
    for i in range(1, 100):
        Logger.info('1', '2')
        db_client.insert('test', ['c1', 'c2', 'c3', 'c4'], [],
                         ['abc', u'sb_' + str(i), 1.1, 5])
        time.sleep(0.1)