Example #1
0
 def receive(self, message):
     if message == ('_forward', ANY):
         _, payload = message
         self.actor << payload
     elif message == ('terminated', self.actor):
         _, actor = message
         if self.keep_running:
             after(1.0).do(self._do_spawn)
         else:
             log("actor terminated but not re-spawning actor; pass --keepruning/-k to change this behaviour")
             self.stop()
     else:
         Events.log(Message("Contained actor sent a message to parent: %r" % (message,)))
Example #2
0
    def connect(self, delayed=False):
        if self.connecting and not self.connecting.called:
            self.connecting.cancel()

        self.connecting = reactor.callLater(CLIENT_RECONNECT_INTERVAL, self.connect)

        def do_connect():
            self.watch(self.monitor)
            assert self.monitor in self._Actor__cell.watchees
            self.monitor << ('connect', self.ref)

        if delayed:
            after(1.0).do(do_connect)
        else:
            do_connect()
Example #3
0
    def receive(self, msg):
        if ('publish', ANY, ANY) == msg:
            _, file_path, pub_id = msg
            if not os.path.exists(file_path) and not os.path.isdir(file_path):
                err("attempt to publish a file that does not exist")
                return

            if pub_id in self.published:
                raise FileAlreadyPublished("Attempt to publish %r with ID %r but a file already exists with that ID" % (file_path, pub_id))
            else:
                self.published[pub_id] = (file_path, datetime.datetime.now())

        elif ('get-file', ANY, ANY) == msg:
            _, pub_id, send_to = msg

            if pub_id not in self.published:
                err("attempt to get a file with ID %r which has not been published or is not available anymore" % (pub_id,))
                return

            self._touch_file(pub_id)

            file_path, time_added = self.published[pub_id]
            sender = self.watch(_Sender.using(service=self.ref, pub_id=pub_id, file=file_path, send_to=send_to))
            self.senders[sender] = pub_id
            # self.senders[sender] = pub_id
            send_to << ('take-file', sender)

        elif ('touch-file', ANY) == msg:
            _, pub_id = msg
            if pub_id not in self.published:
                err("attempt to touch a file with ID %r which has not been published or is not available anymore" % (pub_id,))
                return
            _, pub_id = msg
            self._touch_file(pub_id)

        elif 'purge-old' == msg:
            after(60.0).do(self.send, 'purge-old')

            t = datetime.datetime.now()

            for pub_id, (file_path, time_added) in self.published.items():
                if (t - time_added).total_seconds() > FILE_MAX_LIFETIME and pub_id not in self.senders.values():
                    dbg("purging file %r at %r" % (pub_id, file_path))
                    del self.published[pub_id]

        elif ('terminated', IN(self.senders)) == msg:
            _, sender = msg
            del self.senders[sender]
Example #4
0
    def connect(self, delayed=False):
        if self.connecting and not self.connecting.called:
            self.connecting.cancel()

        self.connecting = reactor.callLater(CLIENT_RECONNECT_INTERVAL,
                                            self.connect)

        def do_connect():
            self.watch(self.monitor)
            assert self.monitor in self._Actor__cell.watchees
            self.monitor << ('connect', self.ref)

        if delayed:
            after(1.0).do(do_connect)
        else:
            do_connect()
Example #5
0
    def expect_none(self):
        """If the queue is not empty, returns False immediately, otherwise a Deferred that fires a bit later and whose
        result is True or False depending on whether the queue is still empty when the Deferred fires or not.

        The Deferred mechanism is needed to allow other coroutines to perform tasks which can potentially affect the
        state of the queue.

        """
        def check_queue(_=None):
            if self.queue:
                raise AssertionError("Negative expectation failed")

        check_queue()
        return after(0.01).do(check_queue)
Example #6
0
 def receive(self, msg):
     if ('terminated', ANY) == msg:
         _, client = msg
         after(10.0).do(lambda: (
             self.watch(DemoClient.using(monitor=self.monitor), name=client.uri.name),
         )).onerror(err)
Example #7
0
 def receive(self, msg):
     if ('terminated', ANY) == msg:
         _, client = msg
         after(10.0).do(lambda:
                        (self.watch(DemoClient.using(monitor=self.monitor),
                                    name=client.uri.name), )).onerror(err)