1 import Pyro
.core
, Pyro
.errors
2 from socket
import gethostname
, gethostbyname
3 from threading
import Thread
4 from common
import log
, test
7 try: Pyro
.core
.getProxyForURI(uri
)
11 def shutdown(obj
, daemon
, client
= False):
16 def run(obj
, name
, client
= False, daemon
= None):
17 Pyro
.core
.initServer()
18 if client
: Pyro
.core
.initClient()
20 daemon
= Pyro
.core
.Daemon()
21 uri
= daemon
.connect(obj
, name
)
22 addr
= gethostbyname(gethostname())
23 if client
: obj
.register(addr
, obj
.get_core())
26 daemon
.requestLoop(lambda: obj
.looping
, 1)
27 except: log
.debug("Exception in: " + name
)
29 if daemon
: shutdown(obj
, daemon
, client
)
31 class PyroBase(Thread
, Pyro
.core
.ObjBase
):
33 Pyro
.core
.ObjBase
.__init
__(self
)
42 def PyroClient(Client
, server
=None):
43 class PyroClient(PyroBase
, Client
):
44 def __init__(self
, uri
):
45 PyroBase
.__init
__(self
)
50 return Pyro
.core
.getProxyForURI(self
.uri
)
52 def register(self
, name
, core
):
53 core
.join(name
, self
.getProxy())
55 if not server
: server
= gethostname()
56 uri
= "PYROLOC://"+server
+":7766/tord1"
58 run(PyroClient(uri
), "torc", True)
59 else: log
.error("unable to find server")
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
, cb
):
71 log
.debug("PYRO joined: " + name
)
72 self
.clients
.append((name
, cb
))
75 def publish(self
, msg
):
76 for name
, cb
in self
.clients
:
78 self
.publishMutex
.acquire()
83 self
.publishMutex
.release()
84 except Pyro
.errors
.ConnectionClosedError
, x
:
85 log
.debug("PYRO removed: %s %s" % (name
, x
))
86 if (name
, cb
) in self
.clients
:
87 self
.clients
.remove((name
, cb
))
88 except Pyro
.errors
.ProtocolError
, x
:
89 log
.error("Pyro.protoco: " + str(x
))
90 except RuntimeError, x
:
91 log
.error("runtime: " + str(x
))
94 run(PyroServer(), "tord1")