~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/message.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-11 21:41:24 UTC
  • mto: This revision was merged to the branch mainline in revision 3543.
  • Revision ID: john@arbash-meinel.com-20080711214124-qi09irlj7pd5cuzg
Shortcut the case when one revision is in the ancestry of the other.

At the cost of a heads() check, when one parent supersedes, we don't have to extract
the text for the other. Changes merge time from 3m37s => 3m21s. Using a
CachingParentsProvider would drop the time down to 3m11s.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
    "Conventional" is used in the sense described in
85
85
    doc/developers/network-protocol.txt: a simple message with arguments and an
86
86
    optional body.
87
 
 
88
 
    Possible states:
89
 
     * args: expecting args
90
 
     * body: expecting body (terminated by receiving a post-body status)
91
 
     * error: expecting post-body error
92
 
     * end: expecting end of message
93
 
     * nothing: finished
94
87
    """
95
88
 
96
89
    def __init__(self, request_handler, responder):
97
90
        MessageHandler.__init__(self)
98
91
        self.request_handler = request_handler
99
92
        self.responder = responder
100
 
        self.expecting = 'args'
101
 
        self._should_finish_body = False
102
 
        self._response_sent = False
 
93
        self.args_received = False
103
94
 
104
95
    def protocol_error(self, exception):
105
96
        if self.responder.response_sent:
109
100
        self.responder.send_error(exception)
110
101
 
111
102
    def byte_part_received(self, byte):
112
 
        if self.expecting == 'body':
113
 
            if byte == 'S':
114
 
                # Success.  Nothing more to come except the end of message.
115
 
                self.expecting = 'end'
116
 
            elif byte == 'E':
117
 
                # Error.  Expect an error structure.
118
 
                self.expecting = 'error'
119
 
            else:
120
 
                raise errors.SmartProtocolError(
121
 
                    'Non-success status byte in request body: %r' % (byte,))
122
 
        else:
123
 
            raise errors.SmartProtocolError(
124
 
                'Unexpected message part: byte(%r)' % (byte,))
 
103
        raise errors.SmartProtocolError(
 
104
            'Unexpected message part: byte(%r)' % (byte,))
125
105
 
126
106
    def structure_part_received(self, structure):
127
 
        if self.expecting == 'args':
128
 
            self._args_received(structure)
129
 
        elif self.expecting == 'error':
130
 
            self._error_received(structure)
131
 
        else:
 
107
        if self.args_received:
132
108
            raise errors.SmartProtocolError(
133
109
                'Unexpected message part: structure(%r)' % (structure,))
134
 
 
135
 
    def _args_received(self, args):
136
 
        self.expecting = 'body'
137
 
        self.request_handler.dispatch_command(args[0], args[1:])
 
110
        self.args_received = True
 
111
        self.request_handler.dispatch_command(structure[0], structure[1:])
138
112
        if self.request_handler.finished_reading:
139
 
            self._response_sent = True
140
113
            self.responder.send_response(self.request_handler.response)
141
 
            self.expecting = 'end'
142
 
 
143
 
    def _error_received(self, error_args):
144
 
        self.expecting = 'end'
145
 
        self.request_handler.post_body_error_received(error_args)
146
114
 
147
115
    def bytes_part_received(self, bytes):
148
 
        if self.expecting == 'body':
149
 
            self._should_finish_body = True
150
 
            self.request_handler.accept_body(bytes)
151
 
        else:
152
 
            raise errors.SmartProtocolError(
153
 
                'Unexpected message part: bytes(%r)' % (bytes,))
154
 
 
155
 
    def end_received(self):
156
 
        if self.expecting not in ['body', 'end']:
157
 
            raise errors.SmartProtocolError(
158
 
                'End of message received prematurely (while expecting %s)'
159
 
                % (self.expecting,))
160
 
        self.expecting = 'nothing'
161
 
        self.request_handler.end_received()
 
116
        # Note that there's no intrinsic way to distinguish a monolithic body
 
117
        # from a chunk stream.  A request handler knows which it is expecting
 
118
        # (once the args have been received), so it should be able to do the
 
119
        # right thing.
 
120
        self.request_handler.accept_body(bytes)
 
121
        self.request_handler.end_of_body()
162
122
        if not self.request_handler.finished_reading:
163
 
            raise errors.SmartProtocolError(
164
 
                "Complete conventional request was received, but request "
165
 
                "handler has not finished reading.")
166
 
        if not self._response_sent:
167
 
            self.responder.send_response(self.request_handler.response)
 
123
            raise SmartProtocolError(
 
124
                "Conventional request body was received, but request handler "
 
125
                "has not finished reading.")
 
126
        self.responder.send_response(self.request_handler.response)
168
127
 
169
128
 
170
129
class ResponseHandler(object):
243
202
        self._bytes_parts.append(bytes)
244
203
 
245
204
    def structure_part_received(self, structure):
246
 
        if type(structure) is not tuple:
 
205
        if type(structure) is not list:
247
206
            raise errors.SmartProtocolError(
248
207
                'Args structure is not a sequence: %r' % (structure,))
 
208
        structure = tuple(structure)
249
209
        if not self._body_started:
250
210
            if self.args is not None:
251
211
                raise errors.SmartProtocolError(
280
240
            if 'hpss' in debug.debug_flags:
281
241
                mutter(
282
242
                    'decoder state: buf[:10]=%r, state_accept=%s',
283
 
                    self._protocol_decoder._get_in_buffer()[:10],
 
243
                    self._protocol_decoder._in_buffer[:10],
284
244
                    self._protocol_decoder.state_accept.__name__)
285
245
            raise errors.ConnectionReset(
286
246
                "please check connectivity and permissions",