def _make_auth_packet(self): pb = scp_pb.Msg() pb.mtype = pb.AUTH pb.auth.token = self.token pb.auth.appid = self.appid protobuf = pb.SerializeToString() return prot.ScpPacket(protobuf=protobuf)
def make_sfkcontent(uri, request_id, headers=None): """ headers: list of strings like "Accept-Encoding: gzip,deflate,sdch" """ pb = scp_pb2.Msg() pb.mtype = pb.SFKCONTENT pb.content.identifier = uri pb.content.request_id = request_id for header in headers: pb.content.headers.append(header) return ScpPacket(protobuf=pb.SerializeToString())
def __init__(self, flags='\x00', guid='\x00' * 16, protobuf='', data=''): super(ScpPacket, self).__init__() self.flags = flags self.guid = guid if not protobuf: pb = scp_pb2.Msg() pb.mtype = pb.MESSAGE self.protobuf = pb.SerializeToString() else: self.protobuf = protobuf self.data = data
def get_auth_info(self): # from sfk in very first packet """ extract appid, token from very first (auth) packet from sfk. return (None, None) if any errors happened """ pb = scp_pb2.Msg() try: pb.ParseFromString(self.protobuf) except Exception as e: LOGGER.error(str(e)) return None, None else: return pb.auth.appid, pb.auth.token
def get_sfkcontent(self): try: pb = scp_pb2.Msg() pb.ParseFromString(self.protobuf) if pb.mtype == pb.SFKCONTENT: uri = pb.content.identifier request_id = pb.content.request_id headers = [] for header in pb.content.headers: headers.append(header) return (uri, request_id, headers) else: raise FieldExtractionError( "Not SfkContent packet, try to extract uri and headers") except Exception as e: raise FieldExtractionError("Can't extract SkfContent field", e)
def get_msg_type(self): pb = scp_pb2.Msg() try: pb.ParseFromString(self.protobuf) except Exception as e: LOGGER.error("Cannot parse protobuf") return None msg_type = { pb.AUTH: 'auth', pb.PING: 'ping', pb.MESSAGE: 'msg', pb.PONG: 'pong', pb.SFKCONTENT: 'sfkcontent', pb.SESSION_DROPPED: 'session_dropped', }.get(pb.mtype, None) # if msg_type is None: # raise ProtocolError("bad mtype") # else: # no one catch this exception!!! return msg_type
def make_session_dropped(guid): pb = scp_pb2.Msg() pb.mtype = pb.SESSION_DROPPED return ScpPacket(guid=guid, protobuf=pb.SerializeToString())
def make_pong(self): pb = scp_pb2.Msg() pb.mtype = pb.PONG pb.pong.ctime = 0 pong = ScpPacket(guid=self.guid, protobuf=pb.SerializeToString()) return pong
def _make_ping_packet(self): pb = scp_pb.Msg() pb.mtype = pb.PING pb.ping.ctime = 0 protobuf = pb.SerializeToString() return prot.ScpPacket(protobuf=protobuf)