133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# Copyright 2012 the V8 project authors. All rights reserved.
233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# Redistribution and use in source and binary forms, with or without
333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# modification, are permitted provided that the following conditions are
433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# met:
533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#
633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Redistributions of source code must retain the above copyright
733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       notice, this list of conditions and the following disclaimer.
833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Redistributions in binary form must reproduce the above
933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       copyright notice, this list of conditions and the following
1033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       disclaimer in the documentation and/or other materials provided
1133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       with the distribution.
1233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Neither the name of Google Inc. nor the names of its
1333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       contributors may be used to endorse or promote products derived
1433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       from this software without specific prior written permission.
1533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#
1633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
2833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
2933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport cStringIO as StringIO
3033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgtry:
3133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  import ujson as json
3233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgexcept ImportError:
3333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  import json
3433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport os
3533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport struct
3633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport zlib
3733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
3833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom . import constants
3933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
4033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgdef Send(obj, sock):
4133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  """
4233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  Sends a JSON encodable object over the specified socket (zlib-compressed).
4333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  """
4433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  obj = json.dumps(obj)
4533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  compression_level = 2  # 1 = fastest, 9 = best compression
4633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  compressed = zlib.compress(obj, compression_level)
4733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  payload = struct.pack('>i', len(compressed)) + compressed
4833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  sock.sendall(payload)
4933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgclass Receiver(object):
5233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def __init__(self, sock):
5333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.sock = sock
5433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data = StringIO.StringIO()
5533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.datalength = 0
5633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self._next = self._GetNext()
5733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def IsDone(self):
5933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    return self._next == None
6033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
6133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def Current(self):
6233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    return self._next
6333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
6433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def Advance(self):
6533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    try:
6633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      self._next = self._GetNext()
6733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    except:
6833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      raise
6933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
7033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def _GetNext(self):
7133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    try:
7233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      while self.datalength < constants.SIZE_T:
7333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        try:
7433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          chunk = self.sock.recv(8192)
7533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        except:
7633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          raise
7733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        if not chunk: return None
7833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        self._AppendData(chunk)
7933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      size = self._PopData(constants.SIZE_T)
8033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      size = struct.unpack(">i", size)[0]
8133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      while self.datalength < size:
8233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        try:
8333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          chunk = self.sock.recv(8192)
8433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        except:
8533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          raise
8633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        if not chunk: return None
8733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        self._AppendData(chunk)
8833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      result = self._PopData(size)
8933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      result = zlib.decompress(result)
9033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      result = json.loads(result)
9133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      if result == constants.END_OF_STREAM:
9233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        return None
9333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      return result
9433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    except:
9533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      raise
9633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
9733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def _AppendData(self, new):
9833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data.seek(0, os.SEEK_END)
9933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data.write(new)
10033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.datalength += len(new)
10133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
10233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def _PopData(self, length):
10333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data.seek(0)
10433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    chunk = self.data.read(length)
10533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    remaining = self.data.read()
10633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data.close()
10733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data = StringIO.StringIO()
10833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.data.write(remaining)
10933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    assert self.datalength - length == len(remaining)
11033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.datalength = len(remaining)
11133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    return chunk
112