def prepare(vfeeds): """ Take a list of VidFeed objects and return a bytes object representing those objects in a format suitable to send to the QoS server. """ s = str(datetime.today()) + '\n' for vf in vfeeds: s += vf.feed_url + ';' + vf.stream_url + ';\n' # record outgoing traffic s = '>>> To QoS Server\n' + s transcribe(s) return s.encode()
def reply_to_QoS(self, fail=False): """ Send a response back to the QoS server, so it knows we received correct data and that we are ready to receive more updates. """ if fail: reply = 'FAIL\n'.encode() transcribe('>>> To QoS Server:\nFAIL\n') else: reply = 'OK\n'.encode() transcribe('>>> To QoS Server:\nOK\n') try: self.QoS_server.send(reply) except socket.error as e: log('socket.error sending reply to server: ' + str(e)) self.reconnect_QoS()
def parse(in_lines): """ Takes input from the QoS server and returns a list of VidFeed objects corresponding to the server input. This function returns a 3-tuple (timestamp, add/update feeds list, delete feeds list). This function throws an exception if the message could not be parsed. """ # record incoming traffic s = '>>> From QoS Server\n' for ln in in_lines: s += ln + '\n' transcribe(s) if len(in_lines) < 2 or in_lines[1] != '': raise Exception('could not parse QoS message') try: timestamp = parse_timestamp(in_lines[0]) except ValueError: raise Exception('could not parse QoS message') if len(in_lines) == 2: return (timestamp, [], []) addrobots = [] rmrobots = [] for ln in in_lines[2:]: delmode = ln.startswith('DELETE') if delmode: ln = ln[7:] rbt = get_robot(ln) if rbt != None: if delmode: rmrobots.append(rbt) else: addrobots.append(rbt) addfeeds = [] for r in addrobots: addfeeds.append(make_vfeed(r)) rmfeeds = [] for r in rmrobots: rmfeeds.append(make_vfeed(r)) return (timestamp, addfeeds, rmfeeds)