| Server IP : 61.19.30.66 / Your IP : 216.73.216.15 Web Server : Apache/2.2.22 (Ubuntu) System : Linux klw 3.11.0-15-generic #25~precise1-Ubuntu SMP Thu Jan 30 17:39:31 UTC 2014 x86_64 User : www-data ( 33) PHP Version : 5.3.10-1ubuntu3.48 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : ON | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/share/pyshared/landscape/broker/ |
Upload File : |
"""
Implementation of a lightweight exchange-triggering mechanism via
small HTTP requests asking if we should do a full exchange.
"""
import urllib
from logging import info
from twisted.python.failure import Failure
from twisted.internet import defer
from landscape.lib.bpickle import loads
from landscape.lib.fetch import fetch
from landscape.lib.log import log_failure
class PingClient(object):
"""An HTTP client which knows how to talk to the ping server."""
def __init__(self, reactor, get_page=None):
if get_page is None:
get_page = fetch
self._reactor = reactor
self.get_page = get_page
def ping(self, url, insecure_id):
"""Ask the question: are there messages for this computer ID?
@param url: The URL of the ping server to hit.
@param insecure_id: This client's insecure ID, if C{None} no HTTP
request will be performed and the result will be C{False}.
@return: A deferred resulting in True if there are messages
and False otherwise.
"""
if insecure_id is not None:
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = urllib.urlencode({"insecure_id": insecure_id})
page_deferred = defer.Deferred()
def errback(type, value, tb):
page_deferred.errback(Failure(value, type, tb))
self._reactor.call_in_thread(page_deferred.callback, errback,
self.get_page, url,
post=True, data=data,
headers=headers)
page_deferred.addCallback(self._got_result)
return page_deferred
return defer.succeed(False)
def _got_result(self, webtext):
"""
Given a response that came from a ping server, return True if
the response indicates that their are messages waiting for
this computer, False otherwise.
"""
if loads(webtext) == {"messages": True}:
return True
class Pinger(object):
"""
A plugin which pings the Landscape server with HTTP requests to
see if a full exchange should be initiated.
@param reactor: The reactor to schedule calls with.
@param identity: The L{Identity} holding the insecure ID used when pinging.
@param exchanger: The L{MessageExchange} to trigger exchanges with.
@param config: The L{BrokerConfiguration} to get the 'ping_url' and
'ping_interval' parameters from. The 'ping_url' specifies what URL
to hit when pinging, and 'ping_interval' how frequently to ping.
Changes in the configuration object will take effect from the next
scheduled ping.
"""
def __init__(self, reactor, identity, exchanger, config,
ping_client_factory=PingClient):
self._config = config
self._identity = identity
self._reactor = reactor
self._exchanger = exchanger
self._call_id = None
self._ping_client = None
self.ping_client_factory = ping_client_factory
reactor.call_on("message", self._handle_set_intervals)
def get_url(self):
return self._config.ping_url
def get_interval(self):
return self._config.ping_interval
def start(self):
"""Start pinging."""
self._ping_client = self.ping_client_factory(self._reactor)
self._schedule()
def ping(self):
"""Perform a ping; if there are messages, fire an exchange."""
deferred = self._ping_client.ping(
self._config.ping_url, self._identity.insecure_id)
deferred.addCallback(self._got_result)
deferred.addErrback(self._got_error)
deferred.addBoth(lambda _: self._schedule())
def _got_result(self, exchange):
if exchange:
info("Ping indicates message available. "
"Scheduling an urgent exchange.")
self._exchanger.schedule_exchange(urgent=True)
def _got_error(self, failure):
log_failure(failure,
"Error contacting ping server at %s" %
(self._ping_client.url,))
def _schedule(self):
"""Schedule a new ping using the current ping interval."""
self._call_id = self._reactor.call_later(self._config.ping_interval,
self.ping)
def _handle_set_intervals(self, message):
if message["type"] == "set-intervals" and "ping" in message:
self._config.ping_interval = message["ping"]
self._config.write()
info("Ping interval set to %d seconds." %
self._config.ping_interval)
if self._call_id is not None:
self._reactor.cancel_call(self._call_id)
self._schedule()
def stop(self):
"""Stop pinging the message server."""
if self._call_id is not None:
self._reactor.cancel_call(self._call_id)
self._call_id = None
class FakePinger(object):
def __init__(self, *args, **kwargs):
pass
def start(self):
pass