| | |
| | | # 心跳信息 |
| | | from huaxin_client import socket_util |
| | | from huaxin_client.client_network import SendResponseSkManager |
| | | from log_module.log import logger_debug, logger_system |
| | | from log_module.log import logger_system |
| | | from utils import middle_api_protocol, tool |
| | | |
| | | MSG_TYPE_HEART = "heart" |
| | | # 命令信息 |
| | | MSG_TYPE_CMD = "cmd" |
| | | |
| | | CLIENT_TYPE_COMMON = "common" |
| | | CLIENT_TYPE_TRADE = "trade" |
| | | |
| | | # 心跳时间间隔 |
| | |
| | | API_TYPE_SAVE_RUNNING_DATA = "save_running_data" # 保存运行时数据 |
| | | API_TYPE_GET_CODE_POSITION_INFO = "get_code_position_info" # 获取代码持仓信息 |
| | | API_TYPE_COMMON_REQUEST = "common_request" # 通用请求 |
| | | |
| | | |
| | | class ActionCallback(object): |
| | | # 交易 |
| | |
| | | # 交易指令管理 |
| | | # 交易指令管理 |
| | | class ApiCommandManager: |
| | | common_client_dict = {} |
| | | trade_client_dict = {} |
| | | trade_client_count = 0 |
| | | common_client_count = 0 |
| | | |
| | | _instance = None |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | |
| | | return key, sk |
| | | |
| | | @classmethod |
| | | def init(cls, addr, port, trade_action_callback, trade_client_count=20): |
| | | cls.trade_client_dict = {} |
| | | def init(cls, addr, port, trade_action_callback, common_client_count=20, trade_client_count=15): |
| | | cls.common_client_dict.clear() |
| | | cls.common_client_count = common_client_count |
| | | cls.trade_client_dict.clear() |
| | | cls.trade_client_count = trade_client_count |
| | | cls.action_callback = trade_action_callback |
| | | cls.ip_port = (addr, port) |
| | | |
| | | for i in range(common_client_count): |
| | | result = cls.__create_and_run_client(CLIENT_TYPE_COMMON, i) |
| | | cls.common_client_dict[result[0]] = result[1] |
| | | for i in range(trade_client_count): |
| | | result = cls.__create_and_run_client(CLIENT_TYPE_TRADE, i) |
| | | cls.trade_client_dict[result[0]] = result[1] |
| | |
| | | cls.action_callback.OnGetCodePositionInfo(client_id, request_id, data) |
| | | elif content_type == API_TYPE_COMMON_REQUEST: |
| | | cls.action_callback.OnCommonRequest(client_id, request_id, data) |
| | | |
| | | |
| | | |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | pass |
| | |
| | | |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | if _type == CLIENT_TYPE_TRADE: |
| | | if _type == CLIENT_TYPE_COMMON: |
| | | if client_id in cls.common_client_dict: |
| | | cls.common_client_dict.pop(client_id) |
| | | elif _type == CLIENT_TYPE_TRADE: |
| | | if client_id in cls.trade_client_dict: |
| | | cls.trade_client_dict.pop(client_id) |
| | | print("pop trade client", client_id) |
| | | try: |
| | | sk.close() |
| | | except: |
| | |
| | | sk.send(socket_util.load_header(json.dumps({"type": "heart", "client_id": client_id}).encode('utf-8'))) |
| | | # print("心跳信息发送成功", client_id) |
| | | except Exception as e: |
| | | if _type == CLIENT_TYPE_TRADE: |
| | | if _type == CLIENT_TYPE_COMMON: |
| | | if client_id in cls.common_client_dict: |
| | | cls.common_client_dict.pop(client_id) |
| | | elif _type == CLIENT_TYPE_TRADE: |
| | | if client_id in cls.trade_client_dict: |
| | | cls.trade_client_dict.pop(client_id) |
| | | try: |
| | |
| | | logger_system.info(f"outside_api __maintain_client 线程ID:{tool.get_thread_id()}") |
| | | while True: |
| | | try: |
| | | if len(cls.common_client_dict) < cls.common_client_count: |
| | | for i in range(cls.common_client_count - len(cls.common_client_dict)): |
| | | result = cls.__create_and_run_client(CLIENT_TYPE_COMMON) |
| | | cls.common_client_dict[result[0]] = result[1] |
| | | |
| | | if len(cls.trade_client_dict) < cls.trade_client_count: |
| | | print("__maintain_client", CLIENT_TYPE_TRADE, cls.trade_client_count - len(cls.trade_client_dict)) |
| | | for i in range(cls.trade_client_count - len(cls.trade_client_dict)): |
| | | result = cls.__create_and_run_client(CLIENT_TYPE_TRADE) |
| | | cls.trade_client_dict[result[0]] = result[1] |