Ejemplo n.º 1
0
def test_get_frame(sim_server, protocol_version):
    c = Client(sim_server.endpoint)
    data, metadata = c.next()
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in data
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in metadata
    if protocol_version == '1.0':
        assert all('metadata' in src for src in data.values())
Ejemplo n.º 2
0
def test_pickle(sim_server_pickle):
    c = Client(sim_server_pickle, ser='pickle')
    data, metadata = c.next()
    assert isinstance(data, dict)
    assert isinstance(metadata, dict)
    image = data['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf']['image.data']
    assert isinstance(image, np.ndarray)
Ejemplo n.º 3
0
def test_serve_files(mock_fxe_raw_run):
    args = [
        'karabo-bridge-serve-files', '-z', 'PUSH',
        str(mock_fxe_raw_run),
        str(44444)
    ]
    interface = None

    with Popen(args,
               stdin=PIPE,
               stdout=PIPE,
               stderr=PIPE,
               env=dict(os.environ, PYTHONUNBUFFERED='1')) as p:
        for line in p.stdout:
            line = line.decode('utf-8')
            if line.startswith('Streamer started on:'):
                interface = line.partition(':')[2].strip()
                break

        print('interface:', interface)
        assert interface is not None, p.stderr.read().decode()

        with Client(interface, sock='PULL', timeout=5) as c:
            data, meta = c.next()

        tid = next(m['timestamp.tid'] for m in meta.values())
        sources = RunDirectory(mock_fxe_raw_run).select_trains(
            by_id[[tid]]).all_sources
        assert frozenset(data) == sources

        p.kill()
        rc = p.wait(timeout=2)
        assert rc == -9  # process terminated by kill signal
Ejemplo n.º 4
0
def test_serve_files(mock_fxe_raw_run, tmp_path):
    src = 'FXE_XAD_GEC/CAM/CAMERA:daqOutput'
    args = [
        'karabo-bridge-serve-files', '-z', 'PUSH',
        str(mock_fxe_raw_run), f'ipc://{tmp_path}/socket', '--source', src
    ]
    interface = None

    p = Popen(args,
              stdin=PIPE,
              stdout=PIPE,
              stderr=PIPE,
              env=dict(os.environ, PYTHONUNBUFFERED='1'))
    try:
        for line in p.stdout:
            line = line.decode('utf-8')
            if line.startswith('Streamer started on:'):
                interface = line.partition(':')[2].strip()
                break

        print('interface:', interface)
        assert interface is not None, p.stderr.read().decode()

        with Client(interface, sock='PULL', timeout=30) as c:
            data, meta = c.next()

        tid = next(m['timestamp.tid'] for m in meta.values())
        assert tid == 10000
        assert set(data) == {src}
    finally:
        if p.poll() is None:
            p.kill()
            rc = p.wait(timeout=2)
            assert rc == -9  # process terminated by kill signal
Ejemplo n.º 5
0
    def connect(self, endpoints):
        """Connect the backend to one or more endpoints.

        :param str/list/tuple endpoints: addresses of endpoints.
        """
        if isinstance(endpoints, str):
            endpoints = [endpoints]
        elif not isinstance(endpoints, (tuple, list)):
            raise ValueError("Endpoints must be either a string or "
                             "a tuple/list of string!")

        context = zmq.Context()

        for end in endpoints:
            backend = context.socket(zmq.DEALER)
            backend.connect(end)
            self._backend[end] = backend

        frontendpoint = "inproc://frontend"
        self._frontend = context.socket(zmq.ROUTER)
        self._frontend.bind(frontendpoint)

        self._client = Client(frontendpoint,
                              context=context,
                              timeout=config['BRIDGE_TIMEOUT'])

        self._context = context
Ejemplo n.º 6
0
def test_req_rep(server, data):
    for _ in range(3):
        server.feed(data)

    with Client(server.endpoint) as client:
        for _ in range(3):
            d, m = client.next()
            compare_nested_dict(data, d)
Ejemplo n.º 7
0
 def run(self):
     self._running = True
     with Client(self._bind_address) as client:
         while self._running:
             data = client.next()
             try:
                 self._daq_queue.put(data)
             except queue.Full:
                 continue
Ejemplo n.º 8
0
def test_timeout():
    no_server = 'ipc://nodata'
    with Client(no_server, timeout=0.2) as c:
        for _ in range(3):
            with pytest.raises(TimeoutError) as info:
                tid, data = c.next()

    assert 'No data received from ipc://nodata in the last 200 ms' in str(
        info.value)
Ejemplo n.º 9
0
def servedata(host, type='REQ'):
    '''
    Generator for the online data stream.
    Input: 
        host: ip address of data stream
        type: ???
    Output:
        dictionary of values for current event
    '''
    from karabo_bridge import Client
    # Generate a client to serve the data
    c = Client(host, type)

    # Return the newest event in the datastream using an iterator construct
    for ret in c:
        yield {'data':ret[0], 'meta':ret[1]} #it comes out as a dict so the we have a consistent datastream
Ejemplo n.º 10
0
        def run(self):
            """Receiving data from a Karabo bridge."""
            self._running = True

            from karabo_bridge import Client

            # TODO: make timeout configurable
            with Client(self._endpoint, timeout=1) as client:
                print(f"Binding to server: {self._endpoint}\n")
                while self._running:
                    t0 = time.perf_counter()
                    try:
                        self._queue.append(client.next())
                    except TimeoutError:
                        continue
                    print(
                        "Time for retrieving data from the server: {:.1f} ms".
                        format(1000 * (time.perf_counter() - t0)))
Ejemplo n.º 11
0
class Tester():
    def __init__(self, address, sigint):
        self.sigint = sigint
        self.client = Client(address)
        self.reset()

    def handler(self, signum, frame):
        signal.signal(signal.SIGINT, self.sigint)
        opt = input('Reset? [y/N/e] ("e" to exit): ').lower()
        if opt.startswith('y'):
            signal.signal(signal.SIGINT, self.handler)
            self.reset()
            self.loop()
        elif opt.startswith('e'):
            print('Exiting')
            sys.exit(0)
        else:
            signal.signal(signal.SIGINT, self.handler)
            self.loop()
    
    def reset(self):
        self.ind = 0
        self.old = 0
        self.stime = time.time()
        print('Train ID    Diff   Frame rate')
    
    def loop(self):
        while True:
            d, md = self.client.next()
            tid = d[list(d)[0]]['image.trainId'][0]
            npulses = d[list(d)[0]]['image.data'].shape[-1]
            self.ind += 1
            tnow = time.time()
            if self.ind > 1:
                print('%-10ld  %-6d %.3f Hz' % (tid, tid-self.old, self.ind*npulses/(tnow-self.stime)))
            else:
                print('-'*30)
            self.old = tid
Ejemplo n.º 12
0
def test_pair_socket(sim_server):
    with pytest.raises(NotImplementedError):
        c = Client(sim_server, sock='PAIR')
Ejemplo n.º 13
0
 def __init__(self, address, sigint):
     self.sigint = sigint
     self.client = Client(address)
     self.reset()
Ejemplo n.º 14
0
def test_pull_socket(sim_server):
    with pytest.raises(NotImplementedError) as info:
        c = Client(sim_server, sock='PULL')
Ejemplo n.º 15
0
def client():
    client = Client('tcp://localhost:5555')
    yield client
Ejemplo n.º 16
0
def test_get_frame(sim_server):
    c = Client(sim_server)
    data, metadata = c.next()
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in data
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in metadata
Ejemplo n.º 17
0
def test_iterator(sim_server):
    c = Client(sim_server.endpoint)
    for i, (data, metadata) in enumerate(islice(c, 3)):
        trainId = metadata['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf']['timestamp.tid']
        assert trainId == 10000000000 + i
Ejemplo n.º 18
0
def test_protocol_1(sim_server_version_1):
    c = Client(sim_server_version_1)
    data, metadata = c.next()
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in data
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in metadata
    assert all('metadata' in src for src in data.values())
Ejemplo n.º 19
0
def test_pull_socket(sim_push_server):
    c = Client(sim_push_server.endpoint, sock='PULL')
    data, metadata = c.next()
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in data
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in metadata
Ejemplo n.º 20
0
def test_context_manager(sim_server):
    with Client(sim_server.endpoint) as c:
        data, metadata = c.next()
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in data
    assert 'SPB_DET_AGIPD1M-1/DET/0CH0:xtdf' in metadata
    assert c._socket.closed
Ejemplo n.º 21
0
from karabo_bridge import Client

krb_client = Client("tcp://localhost:4545")

for i in range(10):
    data, metadata = krb_client.next()
    print("Client : received train ID {}".format(
        metadata['SPB_DET_AGIPD1M-1/DET/detector']['timestamp.tid']))
    det_data = data['SPB_DET_AGIPD1M-1/DET/detector']
    print("Client : - detector image shape is {}, {} Mbytes".format(
        det_data['image.data'].shape, det_data['image.data'].nbytes/1024**2))

print("Client : Client stops reading here")