Beispiel #1
0
 def rct(self):
     if self._rct is not None:
         return self._rct
     else:
         self.io_manager = ioloop_manager.IOLoopManager()
         self.io_wrapper = resource_client.IOLoopThreadWrapper(
             self.io_manager.get_ioloop())
         LOGGER.info('Cleanup function \'self.io_manager\': File: %s line: %s' % (
             getframeinfo(currentframe()).filename.split('/')[-1],
             getframeinfo(currentframe()).lineno))
         add_cleanup(self.io_manager.stop)
         self.io_wrapper.default_timeout = _timeout
         self.io_manager.start()
         self.rc = resource_client.KATCPClientResource(
             dict(name='{}'.format(self.katcp_clt),
                  address=('{}'.format(self.katcp_clt),
                           '7147'),
                  controlled=True))
         self.rc.set_ioloop(self.io_manager.get_ioloop())
         self._rct = (resource_client.ThreadSafeKATCPClientResourceWrapper(self.rc,
                                                                           self.io_wrapper))
         self._rct.start()
         LOGGER.info('Cleanup function \'self._rct\': File: %s line: %s' % (
             getframeinfo(currentframe()).filename.split('/')[-1],
             getframeinfo(currentframe()).lineno))
         add_cleanup(self._rct.stop)
         try:
             self._rct.until_synced(timeout=_timeout)
         except TimeoutError:
             self._rct.stop()
     return self._rct
def setup_resource_client():
    global rc
    print d.bind_address
    rc = resource_client.KATCPClientResource(dict(
        name='thething',
        address=d.bind_address,
        controlled=True
    ))
    rc.start()
Beispiel #3
0
 def rct(self):
     if self._rct is not None:
         return self._rct
     else:
         self.io_manager = ioloop_manager.IOLoopManager()
         self.io_wrapper = resource_client.IOLoopThreadWrapper(
             self.io_manager.get_ioloop())
         self.io_wrapper.default_timeout = self.timeout
         self.io_manager.start()
         self.rc = resource_client.KATCPClientResource(
             dict(name='{}'.format(self.katcp_client),
                  address=('{}'.format(self.katcp_client),
                           self.prim_port),
                  controlled=True))
         self.rc.set_ioloop(self.io_manager.get_ioloop())
         self._rct = (resource_client.ThreadSafeKATCPClientResourceWrapper(self.rc,
                                                                           self.io_wrapper))
         self._rct.start()
         try:
             self._rct.until_synced(timeout=self.timeout)
         except TimeoutError:
             self._rct.stop()
     return self._rct
Beispiel #4
0
 def __init__(self, addr, port):
     self._client = resource_client.KATCPClientResource(
         dict(name='packetiser-client',
              address=(addr, port),
              controlled=True))
     self._ioloop = IOLoop.current()
Beispiel #5
0
    def katcp_rct(self):
        try:
            katcp_prot = self.test_config['inst_param']['katcp_protocol']
        except TypeError:
            LOGGER.error('Failed to read katcp protocol from test config file')
        else:
            _major, _minor, _flags = katcp_prot.split(',')
            protocol_flags = ProtocolFlags(int(_major), int(_minor), _flags)
        multicast_ip = self.get_multicast_ips
        if not multicast_ip:
            LOGGER.error('Failed to calculate multicast IP\'s')
            self._katcp_rct = None
            return

        if self._katcp_rct is None:
            try:
                reply, informs = self.rct.req.subordinate_list(self.array_name)
            except TypeError:
                msg = 'Failed to list all arrays with name: %s' %self.array_name
                LOGGER.exception(msg)
            # If no sub-array present create one, but this could cause problems
            # if more than one sub-array is present. Update this to check for
            # required sub-array.
            if reply.reply_ok():
                self.katcp_array_port = int(informs[0].arguments[1])
            else:
                LOGGER.info('Array has not been assigned yet, will try to assign.'
                            ' File:%s Line:%s' % (
                                getframeinfo(currentframe()).filename.split('/')[-1],
                                getframeinfo(currentframe()).lineno))
                try:
                    reply, _informs = self.rct.req.subordinate_create(self.array_name,
                                                                *multicast_ip)
                    assert reply.reply_ok()
                except (ValueError, TypeError, AssertionError):
                    try:
                        reply, informs = self.rct.req.subordinate_list()
                        assert reply.reply_ok()
                        informs = informs[0]
                        if len(informs.arguments) >= 10 and self.array_name == informs.arguments[0]:
                            msg = 'Array assigned successfully: %s' %str(informs)
                            LOGGER.info(msg)
                        else:
                            LOGGER.error('Halting array.')
                            reply, informs = self.rct.req.subordinate_halt(self.array_name,
                                timeout=_timeout)
                            assert reply.reply_ok()
                    except AssertionError:
                        LOGGER.exception('Failed to assign multicast ip on array: %s: \n\nReply: %s' % (
                            self.array_name, str(reply)))
                else:
                    if len(reply.arguments) == 2:
                        try:
                            self.katcp_array_port = int(reply.arguments[-1])
                            LOGGER.info('Array %s assigned successfully' % (self.katcp_array_port))
                        except ValueError:
                            # self.rct.req.subordinate_halt(self.array_name)
                            # self.rct.stop()
                            # self.rct.start()
                            # self.rct.until_synced(timeout=_timeout)
                            # reply, informs = self.rct.req.subordinate_create(self.array_name,
                            # *multicast_ip)
                            errmsg = 'Investigate as to why this thing failed.'
                            LOGGER.exception(errmsg)
                            sys.exit(errmsg)

            katcp_rc = resource_client.KATCPClientResource(
                dict(name='{}'.format(self.katcp_clt),
                     address=('{}'.format(self.katcp_clt),
                              '{}'.format(self.katcp_array_port)),
                     preset_protocol_flags=protocol_flags, controlled=True))
            katcp_rc.set_ioloop(self.io_manager.get_ioloop())
            self._katcp_rct = (
                resource_client.ThreadSafeKATCPClientResourceWrapper(
                    katcp_rc, self.io_wrapper))
            self._katcp_rct.start()
            try:
                self._katcp_rct.until_synced(timeout=_timeout)
            except Exception as e:
                self._katcp_rct.stop()
                LOGGER.exception('Failed to connect to katcp due to %s' %str(e))
            LOGGER.info('Cleanup function \'self._katcp_rct\': File: %s line: %s' % (
                getframeinfo(currentframe()).filename.split('/')[-1],
                getframeinfo(currentframe()).lineno))
            # add_cleanup(self._katcp_rct.stop)
        else:
            self._katcp_rct.start()
            try:
                time.sleep(1)
                self._katcp_rct.until_synced(timeout=_timeout)
            except Exception as e:
                self._katcp_rct.stop()
                LOGGER.exception('Failed to connect to katcp due to %s' %str(e))
        return self._katcp_rct
Beispiel #6
0
# Copyright 2016 National Research Foundation (South African Radio Astronomy Observatory)
# BSD license - see LICENSE for details

from __future__ import absolute_import, division, print_function

import tornado

from tornado.ioloop import IOLoop

from katcp import resource_client

ioloop = IOLoop.current()

client = resource_client.KATCPClientResource(
    dict(name='demo-client', address=('localhost', 5000), controlled=True))


@tornado.gen.coroutine
def demo():
    # Wait until the client has finished inspecting the device
    yield client.until_synced()
    help_response = yield client.req.help()
    print("device help:\n ", help_response)
    add_response = yield client.req.add(3, 6)
    print("3 + 6 response:\n", add_response)
    # By not yielding we are not waiting for the response
    pick_response_future = client.req.pick_fruit()
    # Instead we wait for the fruit.result sensor status to change to
    # nominal. Before we can wait on a sensor, a strategy must be set:
    client.sensor.fruit_result.set_strategy('event')
    # If the condition does not occur within the timeout (default 5s), we will
Beispiel #7
0
    def katcp_rct_sensor(self):
        if self._katcp_rct_sensor is None:
            try:
                katcp_prot = self.test_config['instrument_params'][
                    'katcp_protocol']
                _major, _minor, _flags = katcp_prot.split(',')
                protocol_flags = ProtocolFlags(int(_major), int(_minor),
                                               _flags)
                LOGGER.info('katcp protocol flags %s' % protocol_flags)

                LOGGER.info('Getting running array.')
                reply, informs = self.rct.req.subordinate_list(self.array_name)
                assert reply.reply_ok()
                # If no sub-array present create one, but this could cause problems
                # if more than one sub-array is present. Update this to check for
                # required sub-array.
            except Exception:
                LOGGER.exception('Failed to list all arrays with name: %s' %
                                 self.array_name)
            else:
                try:
                    try:
                        self.katcp_array_port = int(informs[0].arguments[1])
                        LOGGER.info(
                            'Current running array name: %s, port: %s' %
                            (self.array_name, self.katcp_array_port))
                    except ValueError:
                        self.katcp_array_port, self.katcp_sensor_port = informs[
                            0].arguments[1].split(',')
                        LOGGER.info(
                            'Current running array name: %s, port: %s, sensor port: %s'
                            % (self.array_name, self.katcp_array_port,
                               self.katcp_sensor_port))
                except Exception:
                    errmsg = (
                        'Failed to retrieve running array, ensure one has been created and running'
                    )
                    LOGGER.exception(errmsg)
                    sys.exit(errmsg)
                else:
                    katcp_rc = resource_client.KATCPClientResource(
                        dict(name='{}'.format(self.katcp_client),
                             address=('{}'.format(self.katcp_client),
                                      '{}'.format(self.katcp_sensor_port)),
                             preset_protocol_flags=protocol_flags,
                             controlled=True))
                    katcp_rc.set_ioloop(self.io_manager.get_ioloop())
                    self._katcp_rct_sensor = (
                        resource_client.ThreadSafeKATCPClientResourceWrapper(
                            katcp_rc, self.io_wrapper))
                    self._katcp_rct_sensor.start()
                    try:
                        self._katcp_rct_sensor.until_synced(timeout=_timeout)
                    except Exception as e:
                        self._katcp_rct_sensor.stop()
                        LOGGER.exception(
                            'Failed to connect to katcp due to %s' % str(e))
                    else:
                        return self._katcp_rct_sensor
        else:
            if not self._katcp_rct_sensor.is_active():
                LOGGER.info(
                    'katcp resource client wasnt running, hence we need to start it.'
                )
                self._katcp_rct_sensor.start()
                try:
                    time.sleep(1)
                    self._katcp_rct_sensor.until_synced(timeout=_timeout)
                    return self._katcp_rct_sensor
                except Exception:
                    self._katcp_rct_sensor.stop()
                    LOGGER.exception('Failed to connect to katcp')
            else:
                return self._katcp_rct_sensor