Apache/2.4.7 (Ubuntu) Linux sman1baleendah 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) safemode : OFF MySQL: ON | Perl: ON | cURL: OFF | WGet: ON > / usr / lib / python2.7 / dist-packages / landscape / broker / | server ip : 104.21.89.46 your ip : 172.71.1.155 H O M E |
Filename | /usr/lib/python2.7/dist-packages/landscape/broker/amp.py |
Size | 3.44 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 27-Apr-2025 09:56 |
Last modified | 20-Feb-2014 23:01 |
Last accessed | 06-Jul-2025 20:12 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
from twisted.internet.defer import maybeDeferred, execute, succeed
from landscape.lib.amp import RemoteObject, MethodCallArgument
from landscape.amp import ComponentConnector, get_remote_methods
from landscape.broker.server import BrokerServer
from landscape.broker.client import BrokerClient
from landscape.monitor.monitor import Monitor
from landscape.manager.manager import Manager
class RemoteBroker(RemoteObject):
def call_if_accepted(self, type, callable, *args):
"""Call C{callable} if C{type} is an accepted message type."""
deferred_types = self.get_accepted_message_types()
def got_accepted_types(result):
if type in result:
return callable(*args)
deferred_types.addCallback(got_accepted_types)
return deferred_types
def call_on_event(self, handlers):
"""Call a given handler as soon as a certain event occurs.
@param handlers: A dictionary mapping event types to callables, where
an event type is string (the name of the event). When the first of
the given event types occurs in the broker reactor, the associated
callable will be fired.
"""
result = self.listen_events(handlers.keys())
return result.addCallback(lambda event_type: handlers[event_type]())
class FakeRemoteBroker(object):
"""Looks like L{RemoteBroker}, but actually talks to local objects."""
def __init__(self, exchanger, message_store, broker_server):
self.exchanger = exchanger
self.message_store = message_store
self.broker_server = broker_server
def __getattr__(self, name):
"""
Pass attributes through to the real L{BrokerServer}, after checking
that they're encodable with AMP.
"""
original = getattr(self.broker_server, name, None)
if (name in get_remote_methods(self.broker_server)
and original is not None
and callable(original)):
def method(*args, **kwargs):
for arg in args:
assert MethodCallArgument.check(arg)
for k, v in kwargs.iteritems():
assert MethodCallArgument.check(v)
return execute(original, *args, **kwargs)
return method
else:
raise AttributeError(name)
def call_if_accepted(self, type, callable, *args):
if type in self.message_store.get_accepted_types():
return maybeDeferred(callable, *args)
return succeed(None)
class RemoteBrokerConnector(ComponentConnector):
"""Helper to create connections with the L{BrokerServer}."""
remote = RemoteBroker
component = BrokerServer
class RemoteClientConnector(ComponentConnector):
"""Helper to create connections with the L{BrokerServer}."""
component = BrokerClient
class RemoteMonitorConnector(RemoteClientConnector):
"""Helper to create connections with the L{Monitor}."""
component = Monitor
class RemoteManagerConnector(RemoteClientConnector):
"""Helper for creating connections with the L{Monitor}."""
component = Manager
def get_component_registry():
"""Get a mapping of component name to connectors, for all components."""
all_connectors = [
RemoteBrokerConnector,
RemoteClientConnector,
RemoteMonitorConnector,
RemoteManagerConnector
]
return dict(
(connector.component.name, connector)
for connector in all_connectors)
from landscape.lib.amp import RemoteObject, MethodCallArgument
from landscape.amp import ComponentConnector, get_remote_methods
from landscape.broker.server import BrokerServer
from landscape.broker.client import BrokerClient
from landscape.monitor.monitor import Monitor
from landscape.manager.manager import Manager
class RemoteBroker(RemoteObject):
def call_if_accepted(self, type, callable, *args):
"""Call C{callable} if C{type} is an accepted message type."""
deferred_types = self.get_accepted_message_types()
def got_accepted_types(result):
if type in result:
return callable(*args)
deferred_types.addCallback(got_accepted_types)
return deferred_types
def call_on_event(self, handlers):
"""Call a given handler as soon as a certain event occurs.
@param handlers: A dictionary mapping event types to callables, where
an event type is string (the name of the event). When the first of
the given event types occurs in the broker reactor, the associated
callable will be fired.
"""
result = self.listen_events(handlers.keys())
return result.addCallback(lambda event_type: handlers[event_type]())
class FakeRemoteBroker(object):
"""Looks like L{RemoteBroker}, but actually talks to local objects."""
def __init__(self, exchanger, message_store, broker_server):
self.exchanger = exchanger
self.message_store = message_store
self.broker_server = broker_server
def __getattr__(self, name):
"""
Pass attributes through to the real L{BrokerServer}, after checking
that they're encodable with AMP.
"""
original = getattr(self.broker_server, name, None)
if (name in get_remote_methods(self.broker_server)
and original is not None
and callable(original)):
def method(*args, **kwargs):
for arg in args:
assert MethodCallArgument.check(arg)
for k, v in kwargs.iteritems():
assert MethodCallArgument.check(v)
return execute(original, *args, **kwargs)
return method
else:
raise AttributeError(name)
def call_if_accepted(self, type, callable, *args):
if type in self.message_store.get_accepted_types():
return maybeDeferred(callable, *args)
return succeed(None)
class RemoteBrokerConnector(ComponentConnector):
"""Helper to create connections with the L{BrokerServer}."""
remote = RemoteBroker
component = BrokerServer
class RemoteClientConnector(ComponentConnector):
"""Helper to create connections with the L{BrokerServer}."""
component = BrokerClient
class RemoteMonitorConnector(RemoteClientConnector):
"""Helper to create connections with the L{Monitor}."""
component = Monitor
class RemoteManagerConnector(RemoteClientConnector):
"""Helper for creating connections with the L{Monitor}."""
component = Manager
def get_component_registry():
"""Get a mapping of component name to connectors, for all components."""
all_connectors = [
RemoteBrokerConnector,
RemoteClientConnector,
RemoteMonitorConnector,
RemoteManagerConnector
]
return dict(
(connector.component.name, connector)
for connector in all_connectors)