def send(self): ''' Send the latest line in the open file to the specified port at localhost. If the next line's timestamp is the same, that line will also be sent immediately. If the next line does not contain the same timestamp, the program will seek back to the last line read and then break for a new loop. If the line contains ``TERM``, the program will set ``self.alive = False`` and prepare to exit. ''' l = self.f.readline() if ('TERM' in l.decode('utf-8')) or (l.decode('utf-8') == ''): printM('End of file.', self.sender) self.alive = False else: ts = rs.getTIME(l) self.sock.sendto(l, (self.addr, self.port)) while True: self.pos = self.f.tell() l = self.f.readline() if 'TERM' in l.decode('utf-8'): break if rs.getTIME(l) == ts: self.sock.sendto(l, (self.addr, self.port)) else: self.f.seek(self.pos) break
def run(printFREQ=60, port=8888): ''' Initialize stream and print constants, then process data for packet loss. :param int printFREQ: Value in seconds denoting the frequency with which this program will report packets lost :param int port: Local port to listen on ''' global DPtime, DPttlLoss printM("Initializing...") raspberryshake.initRSlib( dport=port, rsstn='Z0000' ) # runs in quiet mode; suppresses needless output but shows errors add_debug_handler() # now start console output # initialize data stream constants printM('Opened data port successfully.') DP = raspberryshake.getDATA() CHAN = raspberryshake.getCHN( DP) # first channel - doesn't matter which, used to stop looping TR = raspberryshake.tf # transmission rate - in milliseconds TRE = (TR + TR * .5) / 1000. # time diff / error to identify a missed packet SR = raspberryshake.sps # sample / second ttlCHN = raspberryshake.getTTLCHN() # total number of channels printM(" Total Channels: %s" % ttlCHN) printM(" Sample Rate: %s samples / second" % SR) printM(" TX Rate: Every %s milliseconds" % TR) # start processing data packets for packet loss detection # initialize chnNum = 0 while chnNum < ttlCHN: DP = raspberryshake.getDATA() CHAN = raspberryshake.getCHN(DP) DPtime[CHAN] = raspberryshake.getTIME(DP) timeStart[CHAN] = DPtime[CHAN] DPttlLoss[CHAN] = 0 chnNum += 1 printM('Data Packet reading begun.') printM( 'Will report any DP loss as it happens and totals every %s seconds.' % printFREQ) while 1: # loop forever DP = raspberryshake.getDATA() CHAN = raspberryshake.getCHN(DP) timeS = raspberryshake.getTIME(DP) timeD = timeS - DPtime[CHAN] if abs(timeD) > TRE: printM("DP loss of %s second(s) Current TS: %s, Previous TS: %s" % (round(timeD, 3), timeS, DPtime[CHAN])) DPttlLoss[CHAN] += abs(int(timeD * TR)) DPtime[CHAN] = timeS if int(timeS) % printFREQ == 0: if printTTLS(CHAN, TR): timeStart[CHAN] = timeS DPttlLoss[CHAN] = 0
def run(self): ''' Start the thread. First, opens a file, determines the speed of data flow, then opens a socket and begins sending data at that transmission rate. Continues sending data until an ``ENDTEST`` packet arrives on the queue, or until the reader reaches the end of the file. Then, sends a ``TERM`` message to the localhost port and exits. ''' self.f = open(self.data_file, 'rb') self.f.seek(0) l = self.f.readline() l2 = self.f.readline() while (rs.getTIME(l2) == rs.getTIME(l)): l2 = self.f.readline() self.f.seek(0) self.speed = rs.getTIME(l2) - rs.getTIME(l) printW('Opening test socket...', sender=self.sender, announce=False) socket_type = s.SOCK_DGRAM if os.name in 'nt' else s.SOCK_DGRAM | s.SO_REUSEADDR self.sock = s.socket(s.AF_INET, socket_type) printW('Sending data to %s:%s every %s seconds' % (self.addr, self.port, self.speed), sender=self.sender, announce=False) while self.alive: try: q = self._getq() if q.decode('utf-8') in 'ENDTEST': self.alive = False break except Empty: self.send() time.sleep(self.speed) TEST['x_send'][1] = True self.f.close() self.sock.sendto(helpers.msg_term(), (self.addr, self.port)) printW('Exiting.', self.sender, announce=False) sys.exit()