~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

  • Committer: Robert Collins
  • Date: 2009-12-16 22:29:31 UTC
  • mto: This revision was merged to the branch mainline in revision 4920.
  • Revision ID: robertc@robertcollins.net-20091216222931-wbbn5ey4mwmpatwd
Review feedback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
from cStringIO import StringIO
23
23
import struct
24
24
import sys
25
 
import thread
26
25
import threading
27
26
import time
28
27
 
62
61
 
63
62
def _encode_tuple(args):
64
63
    """Encode the tuple args to a bytestream."""
65
 
    joined = '\x01'.join(args) + '\n'
66
 
    if type(joined) is unicode:
67
 
        # XXX: We should fix things so this never happens!  -AJB, 20100304
68
 
        mutter('response args contain unicode, should be only bytes: %r',
69
 
               joined)
70
 
        joined = joined.encode('ascii')
71
 
    return joined
 
64
    return '\x01'.join(args) + '\n'
72
65
 
73
66
 
74
67
class Requester(object):
654
647
        """Make a remote call with a readv array.
655
648
 
656
649
        The body is encoded with one line per readv offset pair. The numbers in
657
 
        each pair are separated by a comma, and no trailing \\n is emitted.
 
650
        each pair are separated by a comma, and no trailing \n is emitted.
658
651
        """
659
652
        if 'hpss' in debug.debug_flags:
660
653
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
1073
1066
class _ProtocolThreeEncoder(object):
1074
1067
 
1075
1068
    response_marker = request_marker = MESSAGE_VERSION_THREE
1076
 
    BUFFER_SIZE = 1024*1024 # 1 MiB buffer before flushing
1077
1069
 
1078
1070
    def __init__(self, write_func):
1079
1071
        self._buf = []
1080
 
        self._buf_len = 0
1081
1072
        self._real_write_func = write_func
1082
1073
 
1083
1074
    def _write_func(self, bytes):
1090
1081
        #       Note that osutils.send_all always sends 64kB chunks anyway, so
1091
1082
        #       we might just push out smaller bits at a time?
1092
1083
        self._buf.append(bytes)
1093
 
        self._buf_len += len(bytes)
1094
 
        if self._buf_len > self.BUFFER_SIZE:
 
1084
        if len(self._buf) > 100:
1095
1085
            self.flush()
1096
1086
 
1097
1087
    def flush(self):
1098
1088
        if self._buf:
1099
1089
            self._real_write_func(''.join(self._buf))
1100
1090
            del self._buf[:]
1101
 
            self._buf_len = 0
1102
1091
 
1103
1092
    def _serialise_offsets(self, offsets):
1104
1093
        """Serialise a readv offset list."""
1154
1143
        self.response_sent = False
1155
1144
        self._headers = {'Software version': bzrlib.__version__}
1156
1145
        if 'hpss' in debug.debug_flags:
1157
 
            self._thread_id = thread.get_ident()
 
1146
            self._thread_id = threading.currentThread().get_ident()
1158
1147
            self._response_start_time = None
1159
1148
 
1160
1149
    def _trace(self, action, message, extra_bytes=None, include_time=False):
1231
1220
                    if first_chunk is None:
1232
1221
                        first_chunk = chunk
1233
1222
                    self._write_prefixed_body(chunk)
1234
 
                    self.flush()
1235
1223
                    if 'hpssdetail' in debug.debug_flags:
1236
1224
                        # Not worth timing separately, as _write_func is
1237
1225
                        # actually buffered
1332
1320
        """Make a remote call with a readv array.
1333
1321
 
1334
1322
        The body is encoded with one line per readv offset pair. The numbers in
1335
 
        each pair are separated by a comma, and no trailing \\n is emitted.
 
1323
        each pair are separated by a comma, and no trailing \n is emitted.
1336
1324
        """
1337
1325
        if 'hpss' in debug.debug_flags:
1338
1326
            mutter('hpss call w/readv: %s', repr(args)[1:-1])