~ubuntu-branches/ubuntu/lucid/landscape-client/lucid-updates

« back to all changes in this revision

Viewing changes to landscape/broker/transport.py

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-04-10 14:28:48 UTC
  • mfrom: (1.1.27)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: package-import@ubuntu.com-20120410142848-7xsy4g2xii7y7ntc
ImportĀ upstreamĀ versionĀ 12.04.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Low-level server communication."""
2
 
import os
3
2
import time
4
3
import logging
5
4
import pprint
7
6
import pycurl
8
7
 
9
8
from landscape.lib.fetch import fetch
10
 
from landscape.lib.fs import create_file
11
9
from landscape.lib import bpickle
12
10
from landscape.log import format_delta
13
11
from landscape import SERVER_API, VERSION
14
12
 
15
13
 
16
14
class HTTPTransport(object):
17
 
    """Transport makes a request to exchange message data over HTTP."""
18
 
 
19
 
    def __init__(self, url, pubkey=None, payload_recorder=None):
20
 
        """
21
 
        @param url: URL of the remote Landscape server message system.
22
 
        @param pubkey: SSH public key used for secure communication.
23
 
        @param payload_recorder: PayloadRecorder used for recording exchanges
24
 
            with the server.  If C{None}, exchanges will not be recorded.
25
 
        """
 
15
    """Transport makes a request to exchange message data over HTTP.
 
16
 
 
17
    @param url: URL of the remote Landscape server message system.
 
18
    @param pubkey: SSH public key used for secure communication.
 
19
    """
 
20
 
 
21
    def __init__(self, reactor, url, pubkey=None):
 
22
        self._reactor = reactor
26
23
        self._url = url
27
24
        self._pubkey = pubkey
28
 
        self._payload_recorder = payload_recorder
29
25
 
30
26
    def get_url(self):
31
27
        """Get the URL of the remote message system."""
60
56
 
61
57
        """
62
58
        spayload = bpickle.dumps(payload)
63
 
        if self._payload_recorder is not None:
64
 
            self._payload_recorder.save(spayload)
65
59
        try:
66
60
            start_time = time.time()
67
61
            if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
68
62
                logging.debug("Sending payload:\n%s", pprint.pformat(payload))
69
 
 
70
63
            curly, data = self._curl(spayload, computer_id, message_api)
71
64
            logging.info("Sent %d bytes and received %d bytes in %s.",
72
65
                         len(spayload), len(data),
92
85
        return response
93
86
 
94
87
 
95
 
class PayloadRecorder(object):
96
 
    """
97
 
    L{PayloadRecorder} records client exchanges with the server to disk for
98
 
    later playback.
99
 
 
100
 
    Exchange payloads will be stored one per file, where the file name is
101
 
    the elapsed time since the client was started.
102
 
    """
103
 
 
104
 
    def __init__(self, destination_dir):
105
 
        """
106
 
        @param destination_dir - The directory to record exchanges in.
107
 
        """
108
 
        self._time_offset = time.time()
109
 
        self._destination_dir = destination_dir
110
 
        self._last_payload_time = -1
111
 
        if self._destination_dir is not None:
112
 
            self._create_destination_dir(self._destination_dir)
113
 
            self._delete_old_payloads()
114
 
 
115
 
    def _create_destination_dir(self, destination_dir):
116
 
        """Create the destination directory if it does not exist.
117
 
 
118
 
        @param destination_dir: The directory to be created.
119
 
        """
120
 
        if not os.path.exists(destination_dir):
121
 
            os.mkdir(destination_dir)
122
 
 
123
 
    def _delete_old_payloads(self):
124
 
        """Delete payloads lying around from a previous session."""
125
 
        for filename in os.listdir(self._destination_dir):
126
 
            file_path = os.path.join(self._destination_dir, filename)
127
 
            if os.path.isfile(file_path):
128
 
                os.unlink(file_path)
129
 
 
130
 
    def save(self, payload):
131
 
        """Persist the given payload to disk.
132
 
 
133
 
        @param payload: The payload to be persisted.
134
 
        """
135
 
        payload_name = self.get_payload_filename()
136
 
        create_file(os.path.join(self._destination_dir, payload_name),
137
 
                    payload)
138
 
 
139
 
    def get_payload_filename(self):
140
 
        """
141
 
        Generate a payload filename.  This method ensures that payloads
142
 
        will have a unique name.
143
 
        """
144
 
        payload_time = time.time() - self._time_offset
145
 
        last_payload_time = '%.3f' % self._last_payload_time
146
 
        this_payload_time = '%.3f' % payload_time
147
 
        if last_payload_time == this_payload_time:
148
 
            payload_time = payload_time + 0.001
149
 
        self._last_payload_time = payload_time
150
 
        return '%.3f' % payload_time
151
 
 
152
 
 
153
88
class FakeTransport(object):
154
89
    """Fake transport for testing purposes."""
155
90
 
156
 
    def __init__(self, url=None, pubkey=None, payload_recorder=None):
 
91
    def __init__(self, reactor=None, url=None, pubkey=None):
157
92
        self._pubkey = pubkey
158
 
        self._payload_recorder = payload_recorder
159
93
        self.payloads = []
160
94
        self.responses = []
161
95
        self._current_response = 0
164
98
        self.message_api = None
165
99
        self.extra = {}
166
100
        self._url = url
 
101
        self._reactor = reactor
167
102
 
168
103
    def get_url(self):
169
104
        return self._url