示例#1
0
    def run(self):
        """
        Runs the receiver.
        """

        # set a timeout to the socket so we can regularly check that we are
        # still to be active (see self._recv());
        if self._sock.gettimeout() is None:
            self._sock.settimeout(0.4)

        log.info("_Receiver running")
        self._active = True

        # set up pipeline
        if self._ooi_digi:
            # no need to parse for timestamps or PD0 ensembles:
            pipeline = self.sink()
        else:
            pipeline = timestamp_filter(pd0_filter(self.sink()))

        # and read in and push received data into the pipeline:
        while self._active:
            recv = self._recv()
            if recv is not None:
                pipeline.send(({}, recv))

        pipeline.close()
        self._end_outfile()
        log.info("_Receiver ended.")
示例#2
0
def receive(sock, bufsize, pd0_file, out_file):
    """
    The routine for the receiving thread.
    Sets up and runs the processing of the incoming stream.
    The sink of the pipeline simply prints received timestamps,
    PD0 ensembles, and info about unprocessed fragments.

    @param sock sock.recv(bufsize) is called repeatily to read in the
           stream and push each read buffer into the pipeline.
    @param bufsize used for the sock read operation. Different values of
           this parameter allow to exercise the parsing algorithm.
    @param pd0_file If not None, the first received PD0 ensemble
                is written to this file.
    @param out_file If not None, all received data is written to this file.
    """

    global show_string

    @coroutine
    def sink(pd0_file, prefix=''):
        """
        @param pd0_file First received PD0 will be written to this file
        @param prefix String to use as prefix in some of the reported info.
        """
        while True:
            xelems, buffer = (yield)
            ts = xelems.get('latest_ts', None)
            if ts:
                print "{%sTIMESTAMP=%s}" % (prefix, ts)

            pd0 = xelems.get('pd0', None)
            if pd0:
                print "{%sPD0=\n\t|%s}" % (prefix, str(pd0).replace(
                    '\n', '\n\t|'))
                if pd0_file:
                    pd0_file.write(pd0.data)
                    pd0_file.flush()
                    pd0_file.close()
                    pd0_file = None
                    print "\n*** First received ensemble written to file ***\n"

            if buffer:
                # show something about the unprocessed buffer
                if 's' in show_string:
                    sys.stdout.write(buffer)
                    sys.stdout.flush()

                elif 'r' in show_string:
                    print '%s%r' % (prefix, buffer)

    pipeline = timestamp_filter(pd0_filter(sink(pd0_file)))

    # note that the order of the filters in the pipeline should no matter:
    # uncomment this line and the one in the loop below to compare the outputs:
    #    pipeline2 = pd0_filter(timestamp_filter(sink(pd0_file, 'B:')))

    # read and push received data into the pipeline:
    while True:
        recv = sock.recv(bufsize)
        pipeline.send(({}, recv))
        #        pipeline2.send(({}, recv))

        if out_file:
            out_file.write(recv)
            out_file.flush()
def receive(sock, bufsize, pd0_file, out_file):
    """
    The routine for the receiving thread.
    Sets up and runs the processing of the incoming stream.
    The sink of the pipeline simply prints received timestamps,
    PD0 ensembles, and info about unprocessed fragments.

    @param sock sock.recv(bufsize) is called repeatily to read in the
           stream and push each read buffer into the pipeline.
    @param bufsize used for the sock read operation. Different values of
           this parameter allow to exercise the parsing algorithm.
    @param pd0_file If not None, the first received PD0 ensemble
                is written to this file.
    @param out_file If not None, all received data is written to this file.
    """

    global show_string

    @coroutine
    def sink(pd0_file, prefix=''):
        """
        @param pd0_file First received PD0 will be written to this file
        @param prefix String to use as prefix in some of the reported info.
        """
        while True:
            xelems, buffer = (yield)
            ts = xelems.get('latest_ts', None)
            if ts:
                print "{%sTIMESTAMP=%s}" % (prefix, ts)

            pd0 = xelems.get('pd0', None)
            if pd0:
                print "{%sPD0=\n\t|%s}" % (prefix,
                                           str(pd0).replace('\n', '\n\t|'))
                if pd0_file:
                    pd0_file.write(pd0.data)
                    pd0_file.flush()
                    pd0_file.close()
                    pd0_file = None
                    print "\n*** First received ensemble written to file ***\n"

            if buffer:
                # show something about the unprocessed buffer
                if 's' in show_string:
                    sys.stdout.write(buffer)
                    sys.stdout.flush()

                elif 'r' in show_string:
                    print '%s%r' % (prefix, buffer)

    pipeline = timestamp_filter(pd0_filter(sink(pd0_file)))

    # note that the order of the filters in the pipeline should no matter:
    # uncomment this line and the one in the loop below to compare the outputs:
#    pipeline2 = pd0_filter(timestamp_filter(sink(pd0_file, 'B:')))

    # read and push received data into the pipeline:
    while True:
        recv = sock.recv(bufsize)
        pipeline.send(({}, recv))
#        pipeline2.send(({}, recv))

        if out_file:
            out_file.write(recv)
            out_file.flush()