实现RPC客户端的appEngine代理功能

This commit is contained in:
vn.py 2017-09-03 14:51:17 +08:00
parent 286a8f72a1
commit 09e0f7af7f
9 changed files with 86 additions and 37 deletions

View File

@ -42,7 +42,7 @@ def runChildProcess():
sleep(5) # 等待CTP接口初始化 sleep(5) # 等待CTP接口初始化
cta = me.appDict[ctaStrategy.appName] cta = me.getApp(ctaStrategy.appName)
cta.loadSetting() cta.loadSetting()
le.info(u'CTA策略载入成功') le.info(u'CTA策略载入成功')

View File

@ -9,7 +9,7 @@
"darkStyle": true, "darkStyle": true,
"language": "chinese", "language": "chinese",
"logActive": true, "logActive": false,
"logLevel": "debug", "logLevel": "debug",
"logConsole": true, "logConsole": true,
"logFile": true "logFile": true

View File

@ -5,26 +5,32 @@ import sys
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
# 判断操作系统 from time import sleep
import platform
system = platform.system()
# vn.trader模块 # vn.trader模块
from vnpy.event import EventEngine2 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.gateway import ctpGateway
# 加载上层应用 # 加载上层应用
from vnpy.trader.app import (riskManager, ctaStrategy, rpcService) from vnpy.trader.app import ctaStrategy, rpcService
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def main(): def main():
"""主程序入口""" """主程序入口"""
le = LogEngine()
le.setLogLevel(le.LEVEL_INFO)
le.addConsoleHandler()
le.addFileHandler()
le.info(u'服务器进程启动')
# 创建事件引擎 # 创建事件引擎
ee = EventEngine2() ee = EventEngine2()
le.info(u'事件引擎创建成功')
# 创建主引擎 # 创建主引擎
me = MainEngine(ee) me = MainEngine(ee)
@ -33,14 +39,15 @@ def main():
me.addGateway(ctpGateway) me.addGateway(ctpGateway)
# 添加上层应用 # 添加上层应用
#me.addApp(riskManager) me.addApp(ctaStrategy)
#me.addApp(ctaStrategy) me.addApp(rpcService)
#me.addApp(rpcService)
le.info(u'主引擎创建成功')
# 阻塞运行 # 阻塞运行
cmd = '' le.info(u'服务器启动成功')
while cmd != 'exit': while 1:
cmd = raw_input() sleep(1)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,11 +1,10 @@
# encoding: UTF-8 # encoding: UTF-8
from rsEngine import RsEngine from .rsEngine import RsEngine
#from uiRmWidget import RmEngineManager from .uiRsWidget import RsEngineManager
from vnpy.trader.uiQt import QtWidgets
appName = 'RpcService' appName = 'RpcService'
appDisplayName = u'RPC服务' appDisplayName = u'RPC服务'
appEngine = RsEngine appEngine = RsEngine
appWidget = QtWidgets.QWidget appWidget = RsEngineManager
appIco = 'rs.ico' appIco = 'rs.ico'

View File

@ -6,8 +6,8 @@ from vnpy.rpc import RpcClient
######################################################################## ########################################################################
class AttributeProxy(object): class ObjectProxy(object):
"""属性代理""" """对象代理"""
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self, nameList, client): def __init__(self, nameList, client):
@ -23,7 +23,7 @@ class AttributeProxy(object):
newNameList.append(name) newNameList.append(name)
# 创建代理对象 # 创建代理对象
proxy = AttributeProxy(newNameList, self.client) proxy = ObjectProxy(newNameList, self.client)
# 缓存代理对象 # 缓存代理对象
self.__dict__[name] = proxy self.__dict__[name] = proxy
@ -43,7 +43,7 @@ class AttributeProxy(object):
######################################################################## ########################################################################
class RsClient(RpcClient): class RsClient(RpcClient):
"""""" """RPC服务客户端"""
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self, reqAddress, subAddress): def __init__(self, reqAddress, subAddress):
@ -54,49 +54,54 @@ class RsClient(RpcClient):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def callback(self, topic, data): def callback(self, topic, data):
"""""" """事件推送回调函数"""
self.eventEngine.put(data) self.eventEngine.put(data) # 直接放入事件引擎中
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def init(self, eventEngine): def init(self, eventEngine):
"""""" """初始化"""
self.eventEngine = eventEngine self.eventEngine = eventEngine # 绑定事件引擎对象
self.usePickle() self.usePickle() # 使用cPickle序列化
self.subscribeTopic('') self.subscribeTopic('') # 订阅全部主题推送
self.start() self.start() # 启动
######################################################################## ########################################################################
class MainEngineProxy(object): class MainEngineProxy(object):
"""""" """主引擎代理"""
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self, eventEngine): def __init__(self, eventEngine):
"""Constructor""" """Constructor"""
self.eventEngine = eventEngine self.eventEngine = eventEngine
self.eventEngine.start(timer=False) self.eventEngine.start(timer=False) # 客户端不启动定时器
self.client = None self.client = None
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def init(self, reqAddress, subAddress): def init(self, reqAddress, subAddress):
"""""" """初始化"""
self.client = RsClient(reqAddress, subAddress) self.client = RsClient(reqAddress, subAddress)
self.client.init(self.eventEngine) self.client.init(self.eventEngine)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __getattr__(self, name): def __getattr__(self, name):
"""""" """获取未知属性"""
# 生成属性名称层级列表 # 生成属性名称层级列表
nameList = [name] nameList = [name]
# 生成属性代理对象 # 生成属性代理对象
proxy = AttributeProxy(nameList, self.client) proxy = ObjectProxy(nameList, self.client)
# 缓存属性代理对象,使得后续调用无需新建 # 缓存属性代理对象,使得后续调用无需新建
self.__dict__[name] = proxy self.__dict__[name] = proxy
# 返回属性代理 # 返回属性代理
return proxy return proxy
#----------------------------------------------------------------------
def getApp(self, name):
"""获取应用引擎对象"""
return self.__getattr__(name)

View File

@ -71,7 +71,7 @@ class RsEngine(object):
# 逐层寻找对象属性 # 逐层寻找对象属性
for name in nameTuple: for name in nameTuple:
obj = self.mainEngine.__getattribute__(name) obj = obj.__getattribute__(name)
# 缓存结果 # 缓存结果
self.functionDict[nameTuple] = obj self.functionDict[nameTuple] = obj

View 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)

View File

@ -193,7 +193,7 @@ class MainWindow(QtWidgets.QMainWindow):
try: try:
self.widgetDict[appName].show() self.widgetDict[appName].show()
except KeyError: except KeyError:
appEngine = self.mainEngine.appDict[appName] appEngine = self.mainEngine.getApp(appName)
self.widgetDict[appName] = appDetail['appWidget'](appEngine, self.eventEngine) self.widgetDict[appName] = appDetail['appWidget'](appEngine, self.eventEngine)
self.widgetDict[appName].show() self.widgetDict[appName].show()

View File

@ -81,6 +81,9 @@ class MainEngine(object):
# 创建应用实例 # 创建应用实例
self.appDict[appName] = appModule.appEngine(self, self.eventEngine) self.appDict[appName] = appModule.appEngine(self, self.eventEngine)
# 将应用引擎实例添加到主引擎的属性中
self.__dict__[appName] = self.appDict[appName]
# 保存应用信息 # 保存应用信息
d = { d = {
'appName': appModule.appName, 'appName': appModule.appName,
@ -284,6 +287,11 @@ class MainEngine(object):
"""查询引擎中所有上层应用的信息""" """查询引擎中所有上层应用的信息"""
return self.appDetailList return self.appDetailList
#----------------------------------------------------------------------
def getApp(self, appName):
"""获取APP引擎对象"""
return self.appDict[appName]
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def initLogEngine(self): def initLogEngine(self):
"""初始化日志引擎""" """初始化日志引擎"""
@ -313,7 +321,7 @@ class MainEngine(object):
# 注册事件监听 # 注册事件监听
self.eventEngine.register(EVENT_LOG, self.logEngine.processLogEvent) self.eventEngine.register(EVENT_LOG, self.logEngine.processLogEvent)
######################################################################## ########################################################################
class DataEngine(object): class DataEngine(object):