实现RPC客户端的appEngine代理功能
This commit is contained in:
parent
286a8f72a1
commit
09e0f7af7f
@ -42,7 +42,7 @@ def runChildProcess():
|
||||
|
||||
sleep(5) # 等待CTP接口初始化
|
||||
|
||||
cta = me.appDict[ctaStrategy.appName]
|
||||
cta = me.getApp(ctaStrategy.appName)
|
||||
|
||||
cta.loadSetting()
|
||||
le.info(u'CTA策略载入成功')
|
||||
|
@ -9,7 +9,7 @@
|
||||
"darkStyle": true,
|
||||
"language": "chinese",
|
||||
|
||||
"logActive": true,
|
||||
"logActive": false,
|
||||
"logLevel": "debug",
|
||||
"logConsole": true,
|
||||
"logFile": true
|
||||
|
@ -5,26 +5,32 @@ import sys
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
|
||||
# 判断操作系统
|
||||
import platform
|
||||
system = platform.system()
|
||||
from time import sleep
|
||||
|
||||
# vn.trader模块
|
||||
from vnpy.event import EventEngine2
|
||||
from vnpy.trader.vtEngine import MainEngine
|
||||
from vnpy.trader.vtEngine import MainEngine, LogEngine
|
||||
|
||||
# 加载底层接口
|
||||
from vnpy.trader.gateway import ctpGateway
|
||||
|
||||
# 加载上层应用
|
||||
from vnpy.trader.app import (riskManager, ctaStrategy, rpcService)
|
||||
from vnpy.trader.app import ctaStrategy, rpcService
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def main():
|
||||
"""主程序入口"""
|
||||
le = LogEngine()
|
||||
le.setLogLevel(le.LEVEL_INFO)
|
||||
le.addConsoleHandler()
|
||||
le.addFileHandler()
|
||||
|
||||
le.info(u'服务器进程启动')
|
||||
|
||||
# 创建事件引擎
|
||||
ee = EventEngine2()
|
||||
le.info(u'事件引擎创建成功')
|
||||
|
||||
# 创建主引擎
|
||||
me = MainEngine(ee)
|
||||
@ -33,14 +39,15 @@ def main():
|
||||
me.addGateway(ctpGateway)
|
||||
|
||||
# 添加上层应用
|
||||
#me.addApp(riskManager)
|
||||
#me.addApp(ctaStrategy)
|
||||
#me.addApp(rpcService)
|
||||
me.addApp(ctaStrategy)
|
||||
me.addApp(rpcService)
|
||||
|
||||
le.info(u'主引擎创建成功')
|
||||
|
||||
# 阻塞运行
|
||||
cmd = ''
|
||||
while cmd != 'exit':
|
||||
cmd = raw_input()
|
||||
le.info(u'服务器启动成功')
|
||||
while 1:
|
||||
sleep(1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
@ -1,11 +1,10 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
from rsEngine import RsEngine
|
||||
#from uiRmWidget import RmEngineManager
|
||||
from vnpy.trader.uiQt import QtWidgets
|
||||
from .rsEngine import RsEngine
|
||||
from .uiRsWidget import RsEngineManager
|
||||
|
||||
appName = 'RpcService'
|
||||
appDisplayName = u'RPC服务'
|
||||
appEngine = RsEngine
|
||||
appWidget = QtWidgets.QWidget
|
||||
appWidget = RsEngineManager
|
||||
appIco = 'rs.ico'
|
@ -6,8 +6,8 @@ from vnpy.rpc import RpcClient
|
||||
|
||||
|
||||
########################################################################
|
||||
class AttributeProxy(object):
|
||||
"""属性代理"""
|
||||
class ObjectProxy(object):
|
||||
"""对象代理"""
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, nameList, client):
|
||||
@ -23,7 +23,7 @@ class AttributeProxy(object):
|
||||
newNameList.append(name)
|
||||
|
||||
# 创建代理对象
|
||||
proxy = AttributeProxy(newNameList, self.client)
|
||||
proxy = ObjectProxy(newNameList, self.client)
|
||||
|
||||
# 缓存代理对象
|
||||
self.__dict__[name] = proxy
|
||||
@ -43,7 +43,7 @@ class AttributeProxy(object):
|
||||
|
||||
########################################################################
|
||||
class RsClient(RpcClient):
|
||||
""""""
|
||||
"""RPC服务客户端"""
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, reqAddress, subAddress):
|
||||
@ -54,45 +54,45 @@ class RsClient(RpcClient):
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def callback(self, topic, data):
|
||||
""""""
|
||||
self.eventEngine.put(data)
|
||||
"""事件推送回调函数"""
|
||||
self.eventEngine.put(data) # 直接放入事件引擎中
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def init(self, eventEngine):
|
||||
""""""
|
||||
self.eventEngine = eventEngine
|
||||
"""初始化"""
|
||||
self.eventEngine = eventEngine # 绑定事件引擎对象
|
||||
|
||||
self.usePickle()
|
||||
self.subscribeTopic('')
|
||||
self.start()
|
||||
self.usePickle() # 使用cPickle序列化
|
||||
self.subscribeTopic('') # 订阅全部主题推送
|
||||
self.start() # 启动
|
||||
|
||||
|
||||
########################################################################
|
||||
class MainEngineProxy(object):
|
||||
""""""
|
||||
"""主引擎代理"""
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, eventEngine):
|
||||
"""Constructor"""
|
||||
self.eventEngine = eventEngine
|
||||
self.eventEngine.start(timer=False)
|
||||
self.eventEngine.start(timer=False) # 客户端不启动定时器
|
||||
|
||||
self.client = None
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def init(self, reqAddress, subAddress):
|
||||
""""""
|
||||
"""初始化"""
|
||||
self.client = RsClient(reqAddress, subAddress)
|
||||
self.client.init(self.eventEngine)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __getattr__(self, name):
|
||||
""""""
|
||||
"""获取未知属性"""
|
||||
# 生成属性名称层级列表
|
||||
nameList = [name]
|
||||
|
||||
# 生成属性代理对象
|
||||
proxy = AttributeProxy(nameList, self.client)
|
||||
proxy = ObjectProxy(nameList, self.client)
|
||||
|
||||
# 缓存属性代理对象,使得后续调用无需新建
|
||||
self.__dict__[name] = proxy
|
||||
@ -100,3 +100,8 @@ class MainEngineProxy(object):
|
||||
# 返回属性代理
|
||||
return proxy
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def getApp(self, name):
|
||||
"""获取应用引擎对象"""
|
||||
return self.__getattr__(name)
|
||||
|
@ -71,7 +71,7 @@ class RsEngine(object):
|
||||
|
||||
# 逐层寻找对象属性
|
||||
for name in nameTuple:
|
||||
obj = self.mainEngine.__getattribute__(name)
|
||||
obj = obj.__getattribute__(name)
|
||||
|
||||
# 缓存结果
|
||||
self.functionDict[nameTuple] = obj
|
||||
|
30
vnpy/trader/app/rpcService/uiRsWidget.py
Normal file
30
vnpy/trader/app/rpcService/uiRsWidget.py
Normal file
@ -0,0 +1,30 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
from vnpy.trader.uiQt import QtWidgets
|
||||
|
||||
|
||||
########################################################################
|
||||
class RsEngineManager(QtWidgets.QWidget):
|
||||
"""RPC服务组件管理"""
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, rsEngine, eventEngine, parent=None):
|
||||
"""Constructor"""
|
||||
super(RsEngineManager, self).__init__(parent)
|
||||
|
||||
self.initUi()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def initUi(self):
|
||||
"""初始化界面"""
|
||||
self.setWindowTitle(u'RPC服务')
|
||||
|
||||
label = QtWidgets.QLabel(u'该模块运行于服务端')
|
||||
vbox = QtWidgets.QVBoxLayout()
|
||||
vbox.addWidget(label)
|
||||
self.setLayout(vbox)
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -193,7 +193,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
try:
|
||||
self.widgetDict[appName].show()
|
||||
except KeyError:
|
||||
appEngine = self.mainEngine.appDict[appName]
|
||||
appEngine = self.mainEngine.getApp(appName)
|
||||
self.widgetDict[appName] = appDetail['appWidget'](appEngine, self.eventEngine)
|
||||
self.widgetDict[appName].show()
|
||||
|
||||
|
@ -81,6 +81,9 @@ class MainEngine(object):
|
||||
# 创建应用实例
|
||||
self.appDict[appName] = appModule.appEngine(self, self.eventEngine)
|
||||
|
||||
# 将应用引擎实例添加到主引擎的属性中
|
||||
self.__dict__[appName] = self.appDict[appName]
|
||||
|
||||
# 保存应用信息
|
||||
d = {
|
||||
'appName': appModule.appName,
|
||||
@ -284,6 +287,11 @@ class MainEngine(object):
|
||||
"""查询引擎中所有上层应用的信息"""
|
||||
return self.appDetailList
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def getApp(self, appName):
|
||||
"""获取APP引擎对象"""
|
||||
return self.appDict[appName]
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def initLogEngine(self):
|
||||
"""初始化日志引擎"""
|
||||
|
Loading…
Reference in New Issue
Block a user