2014-01-22 33 views
3

我想有兩個客戶端,並在RPyC一個服務器之間的連接,我想從CLIENT1在服務器調用客戶端2的方法的方法調用服務器的方法,這是我的代碼:如何獲取連接到RPyC服務器的客戶端列表?

import rpyc 
#server: 
class ServerService(rpyc.Service): 
    def on_connect(self): 
     print "Connected To Server\n" 
    def on_disconnect(self): 
     print "Disconnected From Server\n" 
    def exposed_command(self, cmd): 
     self._cmd = cmd 
     self._conn.root.run_command(self._cmd) 
#client1: 
class AppService(rpyc.Service): 
    def exposed_foo(): 
     return "foo" 
conn = rpyc.connect("localhost", 2014, service = AppService) 
conn.root.command(self._cmd) 
#client2: 
class ClientService(rpyc.Service): 
    def exposed_run_command(self, cmd): 
     eval(cmd) 
# connect to server 
conn = rpyc.connect("localhost", 2014, service = ClientService) 

我有3個單獨的文件,我想通過服務器連接2個客戶端。

更新

我試着解釋更多我的情況,並補充說,我用3個模塊的代碼...... 請爲我解釋一遍 和給我建議任何client.getting ID的ID和其他的東西 你知道,我有一個服務器和兩個客戶端,一個客戶端運行一個簡單的PyQt程序,得到一個瑪雅python命令,並單擊其按鈕,我想運行命令客戶端2上運行的瑪雅,確定?但是,我想將兩個客戶端連接起來,並將它們的方法作爲對等連接進行調用。但我不知道該怎麼稱呼從客戶端1(PyQt的)客戶端2(瑪雅)的run_command

服務器端:

import rpyc  
class ServerService(rpyc.Service): 
    def on_connect(self): 
     print "Connected To Server\n"  
    def on_disconnect(self): 
     print "Disconnected From Server\n"  
    def exposed_command(self, cmd): 
     self._cmd = cmd 
     self._conn.root.run_command(self._cmd) 

if __name__ == "__main__": 
    from rpyc.utils.server import ThreadedServer 
    t = ThreadedServer(ServerService, port = 2014) 
    print "Server is ready ..." 
    t.start() 

客戶端1(PyQt的):

import sys 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
import rpyc 
class ClientApp(QDialog): 
    def __init__(self, parent = None): 
     super(ClientApp, self).__init__(parent) 

     self.label = QLabel("CMD: ") 
     self.textBox = QLineEdit() 
     self.button = QPushButton("Run Command")  
     layout = QHBoxLayout(self) 
     layout.addWidget(self.label) 
     layout.addWidget(self.textBox) 
     layout.addWidget(self.button)  
     self.setWindowTitle("Client App") 
     # SIGNALS 
     self.button.clicked.connect(self.sendCommand)   
     self._cmd = str(self.textBox.text()) 
     self.sendCommand()  
    def sendCommand(self): 
     print "Printed Command : " 
     self._cmd = str(self.textBox.text()) 
     conn.root.command(self._cmd)  
class AppService(rpyc.Service): 
    def exposed_foo2(): 
     return "foo2"  
conn = rpyc.connect("localhost", 2014, service = AppService)  
if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    window = ClientApp() 
    window.show() 

客戶端2(瑪雅):

import rpyc 
from maya import cmds  
class ClientService(rpyc.Service): 
    def exposed_run_command(self, cmd): 
     eval(cmd)  
# connect to server 
conn = rpyc.connect("localhost", 2014, service = ClientService) 

回答

1

這裏有一些問題需要解決:

添加一條線來實例化ServerService。假設你已經有了,但沒有顯示它(請更新你的問題),那麼以下內容可能適用。

conn.root.command(self._cmd)不是對象的一部分,它是客戶端1腳本的一部分,所以「self」不引用任何內容。這應該是類似於conn.root.command("run_command")

然後在您的ServerService.exposed_command(self,cmd)中,應該接收cmd =「run_command」。打印出來以確保。

然後在ServerService.exposed_command(個體經營,CMD),你有

self._conn.root.run_command(self._cmd) 

這已經有一段時間,因爲我用rpyc所以我不熟悉它的細節了,但它是如何知道哪個客戶端發出命令?我猜測客戶端2必須有一個ID,並且ServerService應該將該命令發送給具有給定ID的客戶端,或者您正在假設某種廣播機制(服務器將接收到的命令發送給所有客戶端)。

+0

我認爲最後一段可能是最相關的。看起來好像OP已經獲得了讓一個客戶端與服務器交談的基本概念。但是沒有廣播消息的概念,也沒有將它們從一個特定的客戶端路由到另一個特定的客戶端。 – jdi

相關問題