def new_connection(self, params): """Returns (file): A new configured stream. The returned object implements (minimally) `write` and `close`. Creates a new LogDog stream with the specified parameters. Args: params (StreamParams): The parameters to use with the new connection. Raises: ValueError if the stream name has already been used, or if the parameters are not valid. """ self._register_new_stream(params.name) params_json = params.to_json() fd = self._connect_raw() fd.write(BUTLER_MAGIC) varint.write_uvarint(fd, len(params_json)) fd.write(params_json) return fd
def testVarintEncodingRaw(self): for base, exp in ( (0, b'\x00'), (1, b'\x01'), (0x7F, b'\x7f'), (0x80, b'\x80\x01'), (0x81, b'\x81\x01'), (0x18080, b'\x80\x81\x06'), ): sio = StringIO.StringIO() count = varint.write_uvarint(sio, base) act = sio.getvalue() self.assertEqual(act, exp, "Encoding for %d (%r) doesn't match expected (%r)" % (base, act, exp)) self.assertEqual(count, len(act), "Length of %d (%d) doesn't match encoded length (%d)" % ( base, len(act), count))
def testVarintEncodeDecode(self): seed = (b'\x00', b'\x01', b'\x55', b'\x7F', b'\x80', b'\x81', b'\xff') for perm in itertools.permutations(seed): perm = ''.join(perm).encode('hex') while len(perm) > 0: exp = int(perm.encode('hex'), 16) sio = StringIO.StringIO() count = varint.write_uvarint(sio, exp) sio.seek(0) act, count = varint.read_uvarint(sio) self.assertEqual(act, exp, "Decoded %r (%d) doesn't match expected (%d)" % ( sio.getvalue().encode('hex'), act, exp)) self.assertEqual(count, len(sio.getvalue()), "Decoded length (%d) doesn't match expected (%d)" % ( count, len(sio.getvalue()))) if perm == 0: break perm = perm[1:]
def send(self, data): varint.write_uvarint(self._fd, len(data)) self._fd.write(data)