# Path: groundstation/settings.py
# PORT=1248 # Port to listen on (udp+tcp)
# BEACON_TIMEOUT=5 # 5 second timeout between beacons
# DEFAULT_BUFSIZE=8192 # 8k bytes
# DEFAULT_CACHE_LIFETIME=30 # 30 Seconds
# STORAGE_BACKEND="git"
# LISTALLOBJECTS_CHUNK_THRESHOLD=1024
# LISTALLCHANNELS_RETRY_TIMEOUT=5
# CLOSEWAIT_TIMEOUT=240 # 2MSL
# 
# Path: groundstation/transfer/response.py
# class Response(object):
#     def __init__(self, response_to, verb, payload, station=None, stream=None, origin=None):
#         self.type = "RESPONSE"
#         self.id = response_to
#         self.station = station
#         self.stream = stream
#         self.verb = verb
#         self.payload = payload
#         # if origin:
#         #     self.origin = uuid.UUID(origin)
#         self.origin = origin
# 
#     def _Request(self, *args, **kwargs):
#         kwargs['station'] = self.station
#         req = groundstation.transfer.request.Request(*args, **kwargs)
#         self.station.register_request(req)
#         return req
# 
#     @classmethod
#     def from_gizmo(klass, gizmo, station, stream):
#         log.debug("Hydrating a response from gizmo")
#         return Response(gizmo.id, gizmo.verb, gizmo.payload, station, stream, gizmo.stationid)
# 
#     def SerializeToString(self):
#         gizmo = self.station.gizmo_factory.gizmo()
#         gizmo.id = str(self.id)
#         gizmo.type = Gizmo.RESPONSE
#         gizmo.verb = self.verb
#         if self.payload:
#             gizmo.payload = self.payload
#         return gizmo.SerializeToString()
# 
#     def process(self):
#         if self.verb not in self.VALID_RESPONSES:
#             raise Exception("Invalid Response verb: %s" % (self.verb))
# 
#         self.VALID_RESPONSES[self.verb](self)
# 
#     VALID_RESPONSES = {
#             "TRANSFER": response_handlers.handle_transfer,
#             "DESCRIBEOBJECTS": response_handlers.handle_describeobjects,
#             "DESCRIBECHANNELS": response_handlers.handle_describechannels,
#             "TERMINATE": response_handlers.handle_terminate,
#     }
# 
#     @property
#     def payload(self):
#         return self._payload
#     @payload.setter
#     def payload(self, value):
#         self._payload = str(value)


import socket
import errno
import groundstation.logger
from groundstation import settings
from groundstation.transfer.response import Response
from socket_closed_exception import SocketClosedException

        tmp_buffer = self.buffer
        iterations = 0
        while True:
            if not tmp_buffer: # Catch having emptied our buffer
                break
            # Keep the unmolested buffer
            segment_length, _, payload_buffer = tmp_buffer.partition(chr(0))
            segment_length = int(segment_length)
            if len(payload_buffer) >= segment_length:
                iterations += 1
                # We have the whole buffer
                data = payload_buffer[:segment_length]
                payload_buffer = payload_buffer[segment_length:]
                log.debug("RECV %i bytes from %s" %
                        # XXX Ignore, subclasses set .peer
                        (len(data), self.peer))
                self.packet_queue.insert(0, data)
                tmp_buffer = payload_buffer
                # Bail if we emptied the buffer
            else:
                # We haven't touched tmp_buffer, it is therefore safe to save
                # back to the classbuffer
                if iterations == 0:
                    log.warn("Didn't construct a single full payload!")
                break
        self.buffer = tmp_buffer

    @staticmethod
    def serialize(payload):

# Path: groundstation/settings.py
# PORT=1248 # Port to listen on (udp+tcp)
# BEACON_TIMEOUT=5 # 5 second timeout between beacons
# DEFAULT_BUFSIZE=8192 # 8k bytes
# DEFAULT_CACHE_LIFETIME=30 # 30 Seconds
# STORAGE_BACKEND="git"
# LISTALLOBJECTS_CHUNK_THRESHOLD=1024
# LISTALLCHANNELS_RETRY_TIMEOUT=5
# CLOSEWAIT_TIMEOUT=240 # 2MSL
# 
# Path: groundstation/transfer/response.py
# class Response(object):
#     def __init__(self, response_to, verb, payload, station=None, stream=None, origin=None):
#         self.type = "RESPONSE"
#         self.id = response_to
#         self.station = station
#         self.stream = stream
#         self.verb = verb
#         self.payload = payload
#         # if origin:
#         #     self.origin = uuid.UUID(origin)
#         self.origin = origin
# 
#     def _Request(self, *args, **kwargs):
#         kwargs['station'] = self.station
#         req = groundstation.transfer.request.Request(*args, **kwargs)
#         self.station.register_request(req)
#         return req
# 
#     @classmethod
#     def from_gizmo(klass, gizmo, station, stream):
#         log.debug("Hydrating a response from gizmo")
#         return Response(gizmo.id, gizmo.verb, gizmo.payload, station, stream, gizmo.stationid)
# 
#     def SerializeToString(self):
#         gizmo = self.station.gizmo_factory.gizmo()
#         gizmo.id = str(self.id)
#         gizmo.type = Gizmo.RESPONSE
#         gizmo.verb = self.verb
#         if self.payload:
#             gizmo.payload = self.payload
#         return gizmo.SerializeToString()
# 
#     def process(self):
#         if self.verb not in self.VALID_RESPONSES:
#             raise Exception("Invalid Response verb: %s" % (self.verb))
# 
#         self.VALID_RESPONSES[self.verb](self)
# 
#     VALID_RESPONSES = {
#             "TRANSFER": response_handlers.handle_transfer,
#             "DESCRIBEOBJECTS": response_handlers.handle_describeobjects,
#             "DESCRIBECHANNELS": response_handlers.handle_describechannels,
#             "TERMINATE": response_handlers.handle_terminate,
#     }
# 
#     @property
#     def payload(self):
#         return self._payload
#     @payload.setter
#     def payload(self, value):
#         self._payload = str(value)


# Path: groundstation/sockets/stream_socket.py
import socket
import errno
import groundstation.logger
from groundstation import settings
from groundstation.transfer.response import Response
from socket_closed_exception import SocketClosedException


        tmp_buffer = self.buffer
        iterations = 0
        while True:
            if not tmp_buffer: # Catch having emptied our buffer
                break
            # Keep the unmolested buffer
            segment_length, _, payload_buffer = tmp_buffer.partition(chr(0))
            segment_length = int(segment_length)
            if len(payload_buffer) >= segment_length:
                iterations += 1
                # We have the whole buffer
                data = payload_buffer[:segment_length]
                payload_buffer = payload_buffer[segment_length:]
                log.debug("RECV %i bytes from %s" %
                        # XXX Ignore, subclasses set .peer
                        (len(data), self.peer))
                self.packet_queue.insert(0, data)
                tmp_buffer = payload_buffer
                # Bail if we emptied the buffer
            else:
                # We haven't touched tmp_buffer, it is therefore safe to save
                # back to the classbuffer
                if iterations == 0:
                    log.warn("Didn't construct a single full payload!")
                break
        self.buffer = tmp_buffer

    @staticmethod
    def serialize(payload):

        if isinstance(payload, Response):