예제 #1
0
 def __init__(self, url, count):
     super(Recv, self).__init__(auto_accept=False)
     self.url = url
     self.delay = 0
     self.last_id = None
     self.expected = count
     self.received = 0
     self.accepted = 0
     self.db = Db("dst_db", EventInjector())
예제 #2
0
 def __init__(self, url, count):
     super(Send, self).__init__()
     self.url = url
     self.delay = 0
     self.sent = 0
     self.confirmed = 0
     self.load_count = 0
     self.records = Queue.Queue(maxsize=50)
     self.target = count
     self.db = Db("src_db", EventInjector())
예제 #3
0
 def __init__(self, url, count):
     super(Recv, self).__init__(auto_accept=False)
     self.url = url
     self.delay = 0
     self.last_id = None
     self.expected = count
     self.received = 0
     self.accepted = 0
     self.db = Db("dst_db", EventInjector())
예제 #4
0
 def __init__(self, url, count):
     super(Send, self).__init__()
     self.url = url
     self.delay = 0
     self.sent = 0
     self.confirmed = 0
     self.load_count = 0
     self.records = Queue.Queue(maxsize=50)
     self.target = count
     self.db = Db("src_db", EventInjector())
예제 #5
0
class Recv(MessagingHandler):
    def __init__(self, url, count):
        super(Recv, self).__init__(auto_accept=False)
        self.url = url
        self.delay = 0
        self.last_id = None
        self.expected = count
        self.received = 0
        self.accepted = 0
        self.db = Db("dst_db", EventInjector())

    def on_start(self, event):
        event.container.selectable(self.db.injector)
        e = ApplicationEvent("id_loaded")
        e.container = event.container
        self.db.get_id(e)

    def on_id_loaded(self, event):
        self.last_id = event.id
        event.container.create_receiver(self.url)

    def on_record_inserted(self, event):
        self.accept(event.delivery)
        self.accepted += 1
        if self.accepted == self.expected:
            event.connection.close()
            self.db.close()

    def on_message(self, event):
        id = int(event.message.id)
        if (not self.last_id) or id > self.last_id:
            if self.expected == 0 or self.received < self.expected:
                self.received += 1
                self.last_id = id
                self.db.insert(
                    id, event.message.body,
                    ApplicationEvent("record_inserted",
                                     delivery=event.delivery))
                print("inserted message %s" % id)
            else:
                self.release(event.delivery)
        else:
            self.accept(event.delivery)
예제 #6
0
class Recv(MessagingHandler):
    def __init__(self, url, count):
        super(Recv, self).__init__(auto_accept=False)
        self.url = url
        self.delay = 0
        self.last_id = None
        self.expected = count
        self.received = 0
        self.accepted = 0
        self.db = Db("dst_db", EventInjector())

    def on_start(self, event):
        event.container.selectable(self.db.injector)
        e = ApplicationEvent("id_loaded")
        e.container = event.container
        self.db.get_id(e)

    def on_id_loaded(self, event):
        self.last_id = event.id
        event.container.create_receiver(self.url)

    def on_record_inserted(self, event):
        self.accept(event.delivery)
        self.accepted += 1
        if self.accepted == self.expected:
            event.connection.close()
            self.db.close()

    def on_message(self, event):
        id = int(event.message.id)
        if (not self.last_id) or id > self.last_id:
            if self.received < self.expected:
                self.received += 1
                self.last_id = id
                self.db.insert(id, event.message.body, ApplicationEvent("record_inserted", delivery=event.delivery))
                print("inserted message %s" % id)
            else:
                self.release(event.delivery)
        else:
            self.accept(event.delivery)
예제 #7
0
class Send(MessagingHandler):
    def __init__(self, url, count):
        super(Send, self).__init__()
        self.url = url
        self.delay = 0
        self.sent = 0
        self.confirmed = 0
        self.load_count = 0
        self.records = Queue.Queue(maxsize=50)
        self.target = count
        self.db = Db("src_db", EventInjector())

    def keep_sending(self):
        return self.target == 0 or self.sent < self.target

    def on_start(self, event):
        self.container = event.container
        self.container.selectable(self.db.injector)
        self.sender = self.container.create_sender(self.url)

    def on_records_loaded(self, event):
        if self.records.empty():
            if event.subject == self.load_count:
                print("Exhausted available data, waiting to recheck...")
                # check for new data after 5 seconds
                self.container.schedule(5, self)
        else:
            self.send()

    def request_records(self):
        if not self.records.full():
            print("loading records...")
            self.load_count += 1
            self.db.load(self.records, event=ApplicationEvent(
                "records_loaded", link=self.sender, subject=self.load_count))

    def on_sendable(self, event):
        self.send()

    def send(self):
        while self.sender.credit and not self.records.empty():
            if not self.keep_sending():
                return
            record = self.records.get(False)
            id = record['id']
            self.sender.send(Message(id=id, durable=True, body=record['description']), tag=str(id))
            self.sent += 1
            print("sent message %s" % id)
        self.request_records()

    def on_settled(self, event):
        id = int(event.delivery.tag)
        self.db.delete(id)
        print("settled message %s" % id)
        self.confirmed += 1
        if self.confirmed == self.target:
            event.connection.close()
            self.db.close()

    def on_disconnected(self, event):
        self.db.reset()
        self.sent = self.confirmed

    def on_timer_task(self, event):
        print("Rechecking for data...")
        self.request_records()
예제 #8
0
class Send(MessagingHandler):
    def __init__(self, url, count):
        super(Send, self).__init__()
        self.url = url
        self.delay = 0
        self.sent = 0
        self.confirmed = 0
        self.load_count = 0
        self.records = Queue.Queue(maxsize=50)
        self.target = count
        self.db = Db("src_db", EventInjector())

    def keep_sending(self):
        return self.target == 0 or self.sent < self.target

    def on_start(self, event):
        self.container = event.container
        self.container.selectable(self.db.injector)
        self.sender = self.container.create_sender(self.url)

    def on_records_loaded(self, event):
        if self.records.empty():
            if event.subject == self.load_count:
                print("Exhausted available data, waiting to recheck...")
                # check for new data after 5 seconds
                self.container.schedule(5, self)
        else:
            self.send()

    def request_records(self):
        if not self.records.full():
            print("loading records...")
            self.load_count += 1
            self.db.load(self.records, event=ApplicationEvent("records_loaded", link=self.sender, subject=self.load_count))

    def on_sendable(self, event):
        self.send()

    def send(self):
        while self.sender.credit and not self.records.empty():
            if not self.keep_sending(): return
            record = self.records.get(False)
            id = record['id']
            self.sender.send(Message(id=id, durable=True, body=record['description']), tag=str(id))
            self.sent += 1
            print("sent message %s" % id)
        self.request_records()

    def on_settled(self, event):
        id = int(event.delivery.tag)
        self.db.delete(id)
        print("settled message %s" % id)
        self.confirmed += 1
        if self.confirmed == self.target:
            event.connection.close()
            self.db.close()

    def on_disconnected(self, event):
        self.db.reset()
        self.sent = self.confirmed

    def on_timer_task(self, event):
        print("Rechecking for data...")
        self.request_records()
예제 #9
0
 def on_start(self, event):
     self.container = event.container
     self.db = Db("src_db", self.container.get_event_trigger())
     self.sender = self.container.create_sender(self.url)
예제 #10
0
 def on_start(self, event):
     self.db = Db("dst_db", event.container.get_event_trigger())
     e = ApplicationEvent("id_loaded")
     e.container = event.container
     self.db.get_id(e)