2 from socket
import gethostname
, gethostbyname
3 from threading
import Thread
7 try: Pyro
.core
.getProxyForURI(uri
)
11 def shutdown(obj
, daemon
, client
= False):
16 def run(obj
, name
, client
= False):
17 Pyro
.core
.initServer()
18 if client
: Pyro
.core
.initClient()
20 daemon
= Pyro
.core
.Daemon()
21 uri
= daemon
.connect(obj
, name
)
25 addr
= gethostbyname(gethostname())
26 if client
: obj
.register(addr
, obj
.get_core())
28 daemon
.requestLoop(lambda: obj
.looping
, 1)
29 except: log
.debug("Error starting: "+name
)
30 finally: shutdown(obj
, daemon
, client
)
32 class PyroBase(Thread
, Pyro
.core
.ObjBase
):
34 Pyro
.core
.ObjBase
.__init
__(self
)
43 def PyroClient(Client
, server
=None):
44 class PyroClient(PyroBase
, Client
):
45 def __init__(self
, uri
):
46 PyroBase
.__init
__(self
)
51 return Pyro
.core
.getProxyForURI(self
.uri
)
53 def register(self
, name
, core
):
54 core
.join(name
, self
.getProxy())
56 if not server
: server
= gethostname()
57 uri
= "PYROLOC://"+server
+":7766/tord"
59 run(PyroClient(uri
), "torc", True)
61 def PyroServer(Server
, name
):
62 class PyroServer(PyroBase
, Server
):
64 PyroBase
.__init
__(self
)
67 self
.publishMutex
= Pyro
.util
.getLockObject()
70 def join(self
, name
, callback
):
71 log
.debug(("joined:", name
, callback
))
72 self
.clients
.append((name
, callback
))
74 def publish(self
, msg
):
75 for name
, cb
in self
.clients
:
77 self
.publishMutex
.acquire()
82 self
.publishMutex
.release()
84 log
.debug(("removed:", name
, cb
))
85 if (name
, cb
) in self
.clients
:
86 self
.clients
.remove((name
, cb
))
88 run(PyroServer(), "tord")