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)