简体   繁体   中英

How can I get the list of clients connected to a server in RPyC?

I want to have a connection between two client and one server in RPyC, and I want to call a method of server from client1 that in the method of server call a method of client 2, this is my code:

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)

I have 3 separate files that I want to connect 2 clients by server.

Update

I try to explain more my situation and add codes of the 3 module that I use... please explain more for me and give me advice about ID of any client.getting IDs and other stuff you know, I have one server and two client,one client run a simple PyQt program that get a maya python command and with clicking in its button I want to run command on client 2 that is run on the maya, ok? But, I want to connect both client together and call their methods from each other as peer to peer connection. But I don't know how to call the run_command of client 2(maya) from client1(PyQt)

Server side:

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

Client1 (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()

Client2 (maya):

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)

Here are some issues to address:

Add a line to instantiate the ServerService . Assuming you already have that but didn't show it (please update your question), then the following may apply.

The line conn.root.command(self._cmd) is not part of an object, it is part of the client 1 script, so "self" doesn't refer to anything. This should probably be something like conn.root.command("run_command") .

Then in your ServerService.exposed_command(self, cmd), should receive cmd = "run_command". Print it out to make sure.

Then in ServerService.exposed_command(self, cmd), you have

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

It's been a while since I've used rpyc so I'm not familiar with its details anymore, but how does it know which client to issue the command to? I would guess that the client 2 either has to have an ID, and the ServerService should send the command to the client with given ID, or you are assuming some sort of broadcast mechanism (where server sends commands it receives to all clients).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM