Exemple #1
0
    def receive(self, timeout=10):
        """
        Wait for a message to arrive or until timeout period is exceeded.
        Throws: An exception on failure.

        :param timeout: timeout in seconds.
        :type timeout: `int`
        :return: received message.
        :rtype: `dict`
        """
        start = time.time()

        global participant_list

        while time.time() - start < timeout:
            r = requests.get(self.path + 'aggregator_receive', params={})

            if r.status_code == requests.codes.ok:
                result = r.json()['message']

                if fflabc.Notification.is_participant_joined(result['notification']):
                    participant_list.append(result['notification']['participant'])
                    return fflabc.Response(result['notification'], None)

                else:
                    result['params'] = self.serializer.deserialize(result['params'])
                    return fflabc.Response(result['notification'], result['params'])

        raise TimedOutException('Timeout when receiving data (%f over %f seconds)' % ((time.time() - start), timeout))
Exemple #2
0
    def task_notification(self, timeout: int = 0, flavours: list = None) -> dict:
        """
        Wait for a message to arrive or until timeout.
        If message is received, check whether its notification type matches
        element in given list of notification flavours.
        Throws: An exception on failure
        :param timeout: timeout in seconds
        :type timeout: `int`
        :param flavours: expected notification types
        :type flavours: `list`
        :return: received message
        :rtype: `dict`
        """
        msg = self.receive(timeout)

        if 'notification' not in msg:
            raise fflabc.BadNotificationException(f"Malformed object: {msg}")

        if 'type' not in msg['notification']:
            raise fflabc.BadNotificationException(f"Malformed object: {msg['notification']}")

        try:
            if fflabc.Notification(msg['notification']['type']) not in flavours:
                raise ValueError
        except Exception as exc:
            raise fflabc.BadNotificationException(f"Unexpected notification " \
                f"{msg['notification']['type']}, expecting {flavours}") from exc

        if 'params' not in msg:
            raise fflabc.BadNotificationException(f"Malformed payload: {msg}")

        model = self._download_model(msg['params'])
        return fflabc.Response(msg['notification'], model)
Exemple #3
0
    def task_notification(self, timeout: int = 0, flavours: list = None) -> dict:
        """
        Wait for a message to arrive or until timeout.
        If message is received, check whether its notification type matches
        element in given list of notification flavours.
        Throws: An exception on failure
        :param timeout: timeout in seconds
        :type timeout: `int`
        :param flavours: expected notification types
        :type flavours: `list`
        :return: received message
        :rtype: `dict`
        """
        msg = self.receive(timeout)

        if 'notification' not in msg:
            raise fflabc.BadNotificationException(f"Malformed object: {msg}")

        if 'type' not in msg['notification']:
            raise fflabc.BadNotificationException(f"Malformed object: {msg['notification']}")

        try:
            if fflabc.Notification(msg['notification']['type']) not in flavours:
                raise ValueError
        except:
            raise fflabc.BadNotificationException(f"Unexpected notification " \
                f"{msg['notification']['type']}, expecting {flavours}")

        if 'params' not in msg:
            raise fflabc.BadNotificationException(f"Malformed payload: {msg}")

        model = None

        if msg['params']:
            model = ModelWrapper.unwrap(msg['params'], self.context.model_serializer())

            if model.blob:
                #Embedded model
                model = model.blob
            else:
                #Download from bin store
                url = model.wrapping.get('url', None)
                if not url:
                    raise fflabc.MalformedResponseException(f"Malformed wrapping: {model.wrapping}")

                #Download from bin store
                if self.context.download_models():
                    self.model_files.append(utils.FileDownloader(url))

                    with open(self.model_files[-1].name(), 'rb') as model_file:
                        buff = model_file.read()
                        model = self.context.model_serializer().deserialize(buff)
                else:
                    #Let user decide what to do
                    model = model.wrapping

        return fflabc.Response(msg['notification'], model)
Exemple #4
0
    def receive(self, timeout=10):
        """
        Wait for a message to arrive or until timeout period is exceeded.
        Throws: An exception on failure.

        :param timeout: timeout in seconds.
        :type timeout: `int`
        :return: received message.
        :rtype: `dict`
        """
        start = time.time()

        while time.time() - start < timeout:
            r = requests.get(self.path + 'participant_receive', params={'user': self.user})

            if r.status_code == requests.codes.ok:
                result = self.serializer.deserialize(r.json()['message'])
                return fflabc.Response(None, result)

        raise TimedOutException('Timeout when receiving data (%f over %f seconds)' % ((time.time() - start), timeout))