def kXR_attn_asynresp(self, streamid=None, status=None, dlen=None, actnum=None, reserved=None, rstreamid=None, rstatus=None, rlen=None, rdata=None): """Return a packed representation of a kXR_attn_asynresp response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asynresp') if not rdata: rdata = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(rdata) + 16, 'actnum' : actnum if actnum else get_attncode('kXR_asynresp'), 'reserved': reserved if reserved else (4 * '\0'), 'rsid' : rstreamid if rstreamid else 0, 'rstatus' : rstatus if rstatus else get_responseid('kXR_ok'), 'rlen' : rlen if rlen else len(rdata), 'rdata' : rdata} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncgo(self, streamid=None, status=None, dlen=None, actnum=None): """Return a packed representation of a kXR_attn_asyncgo response.""" if not actnum: actnum = get_attncode('kXR_asyncgo') return self.kXR_attn_asyncab(streamid, status, dlen, actnum, None)
def kXR_attn_asyncwt(self, streamid=None, status=None, dlen=None, actnum=None, wsec=None): """Return a packed representation of a kXR_attn_asyncwt response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asyncwt') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else 8, 'actnum' : actnum if actnum else get_attncode('kXR_asyncwt'), 'wsec' : wsec if wsec else 0} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncab(self, streamid=None, status=None, dlen=None, actnum=None, msg=None): """Return a packed representation of a kXR_attn_asyncab response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn') if not msg: msg = '' params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(msg) + 4, 'actnum' : actnum if actnum else get_attncode('kXR_asyncab'), 'parms' : msg} return self.mh.build_message(response_struct, params)
def kXR_attn_asyncrd(self, streamid=None, status=None, dlen=None, actnum=None, port=None, host=None, token=None): """Return a packed representation of a kXR_attn_asyncrd response.""" response_struct = get_struct('ServerResponseHeader') + \ get_struct('ServerResponseBody_Attn_asyncrd') if not host: host = '' else: host += (token if token else '') params = \ {'streamid': streamid if streamid else 0, 'status' : status if status else get_responseid('kXR_attn'), 'dlen' : dlen if dlen else len(host), 'actnum' : actnum if actnum else get_attncode('kXR_asyncrd'), 'port' : port if port else 0, 'host' : host} return self.mh.build_message(response_struct, params)
def unpack_response(self, response_raw, request_raw): """Return an unpacked named tuple representation of a server response.""" if not len(response_raw): return '' # Unpack the request that generated this response for reference request = self.unpack_request(request_raw)[0] requestid = get_requestid(request.type) # Unpack the response header to find the status and data length header_struct = get_struct('ServerResponseHeader') format = '>' for member in header_struct: format += member['type'] header = self.unpack(format + (str(len(response_raw) - 8) + 's'), response_raw) streamid = header[0] status = header[1] dlen = header[2] body = header[-1] # Check if this is a handshake response if requestid == XProtocol.XRequestTypes.handshake: body_struct = get_struct('ServerInitHandShake') # Check if this is an asynchronous response elif status == XProtocol.XResponseType.kXR_attn: # Extract the attn code attncode = self.unpack('>H', body[:2])[0] body_struct = get_struct('ServerResponseBody_Attn_' \ + get_attncode(attncode)[4:]) if not body_struct: body_struct = body_struct = get_struct('ServerResponseBody_Attn') # Check if this is more than a simple kXR_ok response elif status != XProtocol.XResponseType.kXR_ok: body_struct = get_struct('ServerResponseBody_' \ + get_responseid(status)[4:].title()) else: body_struct = get_struct('ServerResponseBody_' \ + request.type[4:].title()) if not body_struct: body_struct = list() response_struct = header_struct + body_struct # The number of params in a kXR_open response depends on the options that # were passed in the request. if requestid == XProtocol.XRequestTypes.kXR_open: # Remove members from the response struct if the option was not given in # the request. response_struct[:] = [m for m in response_struct if self.option_included(m, request, response_raw)] # Build complete format string format = '>' for member in response_struct: if member.has_key('size'): if member['size'] == 'dlen': if member.has_key('offset'): format += str(dlen - member['offset']) + member['type'] else: format += str(dlen) + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] if len(body_struct) == 0 and dlen > 0: format += (str(dlen) + 's') # Unpack to regular tuple response_tuple = self.unpack(format, response_raw) # Convert to named tuple response_struct.insert(0, {'name': 'type'}) type = get_responseid(status) response = namedtuple('response', ' '.join([m['name'] for m in response_struct])) return response(type, *response_tuple)
def unpack_response(self, response_raw, request_raw): """Return an unpacked named tuple representation of a server response.""" if not len(response_raw): return '' # Unpack the request that generated this response for reference request = self.unpack_request(request_raw)[0] requestid = get_requestid(request.type) # Unpack the response header to find the status and data length header_struct = getMessageStruct('ServerResponseHeader') format = '>' for member in header_struct: format += member['type'] header = self.unpack(format + (str(len(response_raw) - 8) + 's'), response_raw) streamid = header[0] status = header[1] dlen = header[2] body = header[-1] # Check if this is a handshake response if requestid == XProtocol.XRequestTypes.handshake: body_struct = getMessageStruct('ServerInitHandShake') # Check if this is an asynchronous response elif status == XProtocol.XResponseType.kXR_attn: # Extract the attn code attncode = self.unpack('>H', body[:2])[0] body_struct = getMessageStruct('ServerResponseBody_Attn_' \ + get_attncode(attncode)[4:]) if not body_struct: body_struct = body_struct = getMessageStruct('ServerResponseBody_Attn') # Check if this is more than a simple kXR_ok response elif status != XProtocol.XResponseType.kXR_ok: body_struct = getMessageStruct('ServerResponseBody_' \ + get_responseid(status)[4:].title()) else: body_struct = getMessageStruct('ServerResponseBody_' \ + request.type[4:].title()) if not body_struct: body_struct = list() response_struct = header_struct + body_struct # The number of params in a kXR_open response depends on the options that # were passed in the request. if requestid == XProtocol.XRequestTypes.kXR_open: # Remove members from the response struct if the option was not given in # the request. response_struct[:] = [m for m in response_struct if self.option_included(m, request, response_raw)] # Build complete format string format = '>' for member in response_struct: if member.has_key('size'): if member['size'] == 'dlen': if member.has_key('offset'): format += str(dlen - member['offset']) + member['type'] else: format += str(dlen) + member['type'] else: format += str(member['size']) + member['type'] else: format += member['type'] if len(body_struct) == 0 and dlen > 0: format += (str(dlen) + 's') # Unpack to regular tuple response_tuple = self.unpack(format, response_raw) # Convert to named tuple response_struct.insert(0, {'name': 'type'}) type = get_responseid(status) response = namedtuple('response', ' '.join([m['name'] for m in response_struct])) return response(type, *response_tuple)
def kXR_attn_asyncms(self, streamid=None, status=None, dlen=None, actnum=None, msg=None): """Return a packed representation of a kXR_attn_asyncms response.""" if not actnum: actnum = get_attncode('kXR_asyncms') return self.kXR_attn_asyncab(streamid, status, dlen, actnum, msg)