|
| 1 | +""" |
| 2 | + Copyright 2006-2008 SpringSource (http://springsource.com), All Rights Reserved |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +""" |
| 16 | +import logging |
| 17 | +import threading |
| 18 | +import time |
| 19 | +import Pyro4 |
| 20 | + |
| 21 | +from socket import getaddrinfo, gethostbyname |
| 22 | + |
| 23 | +pyro_threads = {} |
| 24 | +serviceList = {} |
| 25 | +logger = logging.getLogger("springpython.remoting.pyro.Pyro4DaemonHolder") |
| 26 | + |
| 27 | +def resolve(host, port): |
| 28 | + canonhost = gethostbyname(host) |
| 29 | + canonport = getaddrinfo(host, port)[0][4][1] |
| 30 | + |
| 31 | + return canonhost, canonport |
| 32 | + |
| 33 | +def register(pyro_obj, service_name, host, port): |
| 34 | + """ |
| 35 | + Register the Pyro4 object and its service name with the daemon. |
| 36 | + Also add the service to a dictionary of objects. This allows the |
| 37 | + PyroDaemonHolder to intelligently know when to start and stop the |
| 38 | + daemon thread. |
| 39 | + """ |
| 40 | + logger.debug("Registering %s at %s:%s with the Pyro4 server" % (service_name, host, port)) |
| 41 | + |
| 42 | + host, port = resolve(host, port) |
| 43 | + |
| 44 | + serviceList[(service_name, host, port)] = pyro_obj |
| 45 | + |
| 46 | + if (host, port) not in pyro_threads: |
| 47 | + |
| 48 | + logger.debug("Pyro4 thread needs to be started at %s:%s" % (host, port)) |
| 49 | + |
| 50 | + pyro_threads[(host, port)] = _Pyro4Thread(host, port) |
| 51 | + pyro_threads[(host, port)].start() |
| 52 | + |
| 53 | + if not hasattr(pyro_obj, "_pyroId"): |
| 54 | + uri = pyro_threads[(host, port)].pyro_daemon.register(pyro_obj, service_name) |
| 55 | + |
| 56 | +def deregister(service_name, host, port): |
| 57 | + """ |
| 58 | + Deregister the named service by removing it from the list of |
| 59 | + managed services and also disconnect from the daemon. |
| 60 | + """ |
| 61 | + logger.debug("Deregistering %s at %s:%s with the Pyro4 server" % (service_name, host, port)) |
| 62 | + |
| 63 | + host, port = resolve(host, port) |
| 64 | + |
| 65 | + if (host, port) in pyro_threads: |
| 66 | + pyro_threads[(host, port)].pyro_daemon.unregister(serviceList[(service_name, host, port)]) |
| 67 | + del(serviceList[(service_name, host, port)]) |
| 68 | + |
| 69 | + def get_address((service_name, host, port)): |
| 70 | + return (host, port) |
| 71 | + |
| 72 | + if len([True for x in serviceList.keys() if get_address(x) == (host, port)]) == 0: |
| 73 | + shutdown(host, port) |
| 74 | + |
| 75 | +def shutdown(daemon_host, daemon_port): |
| 76 | + """This provides a hook so an application can deliberately shutdown a |
| 77 | + daemon thread.""" |
| 78 | + logger.debug("Shutting down Pyro4 daemon at %s:%s" % (daemon_host, daemon_port)) |
| 79 | + |
| 80 | + daemon_host, daemon_port = resolve(daemon_host, daemon_port) |
| 81 | + |
| 82 | + try: |
| 83 | + pyro_threads[(daemon_host, daemon_port)].shutdown() |
| 84 | + time.sleep(1.0) |
| 85 | + del(pyro_threads[(daemon_host, daemon_port)]) |
| 86 | + except Exception, e: |
| 87 | + logger.debug("Failed to shutdown %s:%s => %s" % (daemon_host, daemon_port, e)) |
| 88 | + |
| 89 | +class _Pyro4Thread(threading.Thread): |
| 90 | + """ |
| 91 | + This is a thread that runs the Pyro4 daemon. It is instantiated automatically |
| 92 | + from within Pyro4ServiceExporter. |
| 93 | + """ |
| 94 | + |
| 95 | + def __init__(self, host, port): |
| 96 | + """ |
| 97 | + When this class is created, it also created a Pyro4 core daemon to manage. |
| 98 | + """ |
| 99 | + threading.Thread.__init__(self) |
| 100 | + self.host = host |
| 101 | + self.port = port |
| 102 | + self.logger = logging.getLogger("springpython.remoting.pyro.Pyro4DaemonHolder._Pyro4Thread") |
| 103 | + |
| 104 | + self.logger.debug("Creating Pyro4 daemon") |
| 105 | + self.pyro_daemon = Pyro4.Daemon(host=host, port=port) |
| 106 | + |
| 107 | + def run(self): |
| 108 | + """ |
| 109 | + When this thread starts up, it initializes the Pyro4 server and then puts the |
| 110 | + daemon into listen mode so it can process remote requests. |
| 111 | + """ |
| 112 | + self.logger.debug("Starting up Pyro4 server thread for %s:%s" % (self.host, self.port)) |
| 113 | + self.pyro_daemon.requestLoop() |
| 114 | + |
| 115 | + def shutdown(self): |
| 116 | + """ |
| 117 | + This is a hook in order to signal the thread that its time to shutdown |
| 118 | + the Pyro4 daemon. |
| 119 | + """ |
| 120 | + self.logger.debug("Signaling shutdown of Pyro4 server thread for %s:%s" % (self.host, self.port)) |
| 121 | + class ShutdownThread(threading.Thread): |
| 122 | + def __init__(self, pyro_daemon): |
| 123 | + threading.Thread.__init__(self) |
| 124 | + self.pyro_daemon = pyro_daemon |
| 125 | + self.logger = logging.getLogger("springpython.remoting.pyro.Pyro4DaemonHolder.ShutdownThread") |
| 126 | + def run(self): |
| 127 | + self.logger.debug("Sending shutdown signal...") |
| 128 | + self.pyro_daemon.shutdown() |
| 129 | + |
| 130 | + ShutdownThread(self.pyro_daemon).start() |
| 131 | + |
| 132 | + |
0 commit comments