From 09e0f7af7fd513d7976c7e7af0df171357f29fea Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 3 Sep 2017 14:51:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0RPC=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E7=9A=84appEngine=E4=BB=A3=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CtaTrading/runCtaTrading.py | 2 +- examples/ServerClient/VT_setting.json | 2 +- examples/ServerClient/server.py | 29 +++++++++++------- vnpy/trader/app/rpcService/__init__.py | 7 ++--- vnpy/trader/app/rpcService/rsClient.py | 39 +++++++++++++----------- vnpy/trader/app/rpcService/rsEngine.py | 2 +- vnpy/trader/app/rpcService/uiRsWidget.py | 30 ++++++++++++++++++ vnpy/trader/uiMainWindow.py | 2 +- vnpy/trader/vtEngine.py | 10 +++++- 9 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 vnpy/trader/app/rpcService/uiRsWidget.py diff --git a/examples/CtaTrading/runCtaTrading.py b/examples/CtaTrading/runCtaTrading.py index 37e1e22c..5922bb79 100644 --- a/examples/CtaTrading/runCtaTrading.py +++ b/examples/CtaTrading/runCtaTrading.py @@ -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策略载入成功') diff --git a/examples/ServerClient/VT_setting.json b/examples/ServerClient/VT_setting.json index 7f634434..ef3bb771 100644 --- a/examples/ServerClient/VT_setting.json +++ b/examples/ServerClient/VT_setting.json @@ -9,7 +9,7 @@ "darkStyle": true, "language": "chinese", - "logActive": true, + "logActive": false, "logLevel": "debug", "logConsole": true, "logFile": true diff --git a/examples/ServerClient/server.py b/examples/ServerClient/server.py index 2cd5bdc9..7337fff7 100644 --- a/examples/ServerClient/server.py +++ b/examples/ServerClient/server.py @@ -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() diff --git a/vnpy/trader/app/rpcService/__init__.py b/vnpy/trader/app/rpcService/__init__.py index ec044378..52bb573e 100644 --- a/vnpy/trader/app/rpcService/__init__.py +++ b/vnpy/trader/app/rpcService/__init__.py @@ -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' \ No newline at end of file diff --git a/vnpy/trader/app/rpcService/rsClient.py b/vnpy/trader/app/rpcService/rsClient.py index f61e3e34..b4b78ed0 100644 --- a/vnpy/trader/app/rpcService/rsClient.py +++ b/vnpy/trader/app/rpcService/rsClient.py @@ -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,49 +54,54 @@ 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 # 返回属性代理 return proxy - \ No newline at end of file + + #---------------------------------------------------------------------- + def getApp(self, name): + """获取应用引擎对象""" + return self.__getattr__(name) + \ No newline at end of file diff --git a/vnpy/trader/app/rpcService/rsEngine.py b/vnpy/trader/app/rpcService/rsEngine.py index 07eccf51..5340bec1 100644 --- a/vnpy/trader/app/rpcService/rsEngine.py +++ b/vnpy/trader/app/rpcService/rsEngine.py @@ -71,7 +71,7 @@ class RsEngine(object): # 逐层寻找对象属性 for name in nameTuple: - obj = self.mainEngine.__getattribute__(name) + obj = obj.__getattribute__(name) # 缓存结果 self.functionDict[nameTuple] = obj diff --git a/vnpy/trader/app/rpcService/uiRsWidget.py b/vnpy/trader/app/rpcService/uiRsWidget.py new file mode 100644 index 00000000..485d9d51 --- /dev/null +++ b/vnpy/trader/app/rpcService/uiRsWidget.py @@ -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) + + + + + \ No newline at end of file diff --git a/vnpy/trader/uiMainWindow.py b/vnpy/trader/uiMainWindow.py index c5ceafdd..9d97f60d 100644 --- a/vnpy/trader/uiMainWindow.py +++ b/vnpy/trader/uiMainWindow.py @@ -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() diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index cf99ec4e..bc5f9cf1 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -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): """初始化日志引擎""" @@ -313,7 +321,7 @@ class MainEngine(object): # 注册事件监听 self.eventEngine.register(EVENT_LOG, self.logEngine.processLogEvent) - + ######################################################################## class DataEngine(object):