예제 #1
0
 def __init__(self, host, address):
     super(Server, self).__init__()
     self.container = Container(self)
     self.conn = self.container.connect(host)
     self.receiver = self.container.create_receiver(self.conn, address)
     self.senders = {}
     self.relay = None
예제 #2
0
 def __init__(self, url, timeout=None, container=None, ssl_domain=None):
     self.timeout = timeout
     self.container = container or Container()
     self.url = Url(utf8(url)).defaults()
     self.conn = self.container.connect(url=self.url, handler=self, ssl_domain=ssl_domain)
     self.wait(lambda: not (self.conn.state & Endpoint.REMOTE_UNINIT),
               msg="Opening connection")
예제 #3
0
            self.container.declare_transaction(self.conn, handler=self)

    def on_disconnected(self, event):
        self.current_batch = 0


parser = optparse.OptionParser(
    usage="usage: %prog [options]",
    description="Send messages transactionally to the supplied address.")
parser.add_option("-a",
                  "--address",
                  default="localhost:5672/examples",
                  help="address to which messages are sent (default %default)")
parser.add_option("-m",
                  "--messages",
                  type="int",
                  default=100,
                  help="number of messages to send (default %default)")
parser.add_option(
    "-b",
    "--batch-size",
    type="int",
    default=10,
    help="number of messages in each transaction (default %default)")
opts, args = parser.parse_args()

try:
    Container(TxSend(opts.address, opts.messages, opts.batch_size)).run()
except KeyboardInterrupt:
    pass
예제 #4
0
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactors import Container


class HelloWorld(MessagingHandler):
    def __init__(self, url):
        super(HelloWorld, self).__init__()
        self.url = url

    def on_start(self, event):
        self.acceptor = event.container.listen(self.url)
        event.container.create_sender(self.url)

    def on_sendable(self, event):
        event.sender.send(Message(body=u"Hello World!"))
        event.sender.close()

    def on_message(self, event):
        print event.message.body

    def on_accepted(self, event):
        event.connection.close()

    def on_connection_closed(self, event):
        self.acceptor.close()


Container(HelloWorld("localhost:8888/examples")).run()
예제 #5
0
        event.container.create_receiver(self.url)

    def on_message(self, event):
        if self.expected == 0 or self.received < self.expected:
            print event.message.body
            self.received += 1
            if self.received == self.expected:
                event.connection.close()


parser = optparse.OptionParser(usage="usage: %prog [options]")
parser.add_option(
    "-a",
    "--address",
    default="localhost:5672/examples",
    help="address from which messages are received (default %default)")
parser.add_option(
    "-m",
    "--messages",
    type="int",
    default=100,
    help=
    "number of messages to receive; 0 receives indefinitely (default %default)"
)
opts, args = parser.parse_args()

try:
    Container(Recv(opts.address, opts.messages)).run()
except KeyboardInterrupt:
    pass
예제 #6
0
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from proton.reactors import Container, Selector
from proton.handlers import MessagingHandler


class Recv(MessagingHandler):
    def __init__(self):
        super(Recv, self).__init__()

    def on_start(self, event):
        conn = event.container.connect("localhost:5672")
        event.container.create_receiver(conn,
                                        "examples",
                                        options=Selector(u"colour = 'green'"))

    def on_message(self, event):
        print event.message.body


try:
    Container(Recv()).run()
except KeyboardInterrupt:
    pass
예제 #7
0
        self.transaction.commit()

    def on_abort(self, event):
        self.transaction.abort()

    def on_fetch(self, event):
        self.receiver.flow(1)

    def on_quit(self, event):
        c = self.receiver.connection
        self.receiver.close()
        c.close()


try:
    reactor = Container(TxRecv())
    events = reactor.get_event_trigger()
    thread = threading.Thread(target=reactor.run)
    thread.daemon = True
    thread.start()

    print "Enter 'fetch', 'commit' or 'abort'"
    while True:
        line = sys.stdin.readline()
        if line:
            events.trigger(ApplicationEvent(line.strip()))
        else:
            break
except KeyboardInterrupt:
    pass
예제 #8
0
        self.receiver = event.container.create_receiver(
            self.conn, self.address)
        self.senders = {}
        self.relay = None

    def on_message(self, event):
        sender = self.relay
        if not sender:
            sender = self.senders.get(event.message.reply_to)
        if not sender:
            sender = self.container.create_sender(self.conn,
                                                  event.message.reply_to)
            self.senders[event.message.reply_to] = sender

        response = Message(address=event.message.reply_to,
                           body=event.message.body.upper())
        self.container.declare_transaction(self.conn,
                                           handler=TxRequest(
                                               response, sender,
                                               event.delivery))

    def on_connection_open(self, event):
        if event.connection.remote_offered_capabilities and 'ANONYMOUS-RELAY' in event.connection.remote_offered_capabilities:
            self.relay = self.container.create_sender(self.conn, None)


try:
    Container(TxServer("localhost:5672", "examples")).run()
except KeyboardInterrupt:
    pass
예제 #9
0
    def on_disconnected(self, event):
        self.db.reset()
        self.sent = self.confirmed

    def on_timer(self, event):
        if event.subject == "data":
            print "Rechecking for data..."
            self.request_records()


parser = optparse.OptionParser(
    usage="usage: %prog [options]",
    description="Send messages to the supplied address.")
parser.add_option("-a",
                  "--address",
                  default="localhost:5672/examples",
                  help="address to which messages are sent (default %default)")
parser.add_option(
    "-m",
    "--messages",
    type="int",
    default=0,
    help="number of messages to send; 0 sends indefinitely (default %default)")
opts, args = parser.parse_args()

try:
    Container(Send(opts.address, opts.messages)).run()
except KeyboardInterrupt:
    pass
예제 #10
0
 def run(self):
     Container(self).run()
예제 #11
0
# specific language governing permissions and limitations
# under the License.
#

from proton import Message
from proton.handlers import MessagingHandler
from proton.reactors import Container


class HelloWorld(MessagingHandler):
    def __init__(self, server, address):
        super(HelloWorld, self).__init__()
        self.server = server
        self.address = address

    def on_start(self, event):
        conn = event.container.connect(self.server)
        event.container.create_receiver(conn, self.address)
        event.container.create_sender(conn, self.address)

    def on_sendable(self, event):
        event.sender.send(Message(body=u"Hello World!"))
        event.sender.close()

    def on_message(self, event):
        print event.message.body
        event.connection.close()


Container(HelloWorld("localhost:5672", "examples")).run()
예제 #12
0
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

import time
from proton.reactors import Container, Handler


class Recurring(Handler):
    def __init__(self, period):
        self.period = period

    def on_start(self, event):
        self.container = event.container
        self.container.schedule(time.time() + self.period, subject=self)

    def on_timer(self, event):
        print "Tick..."
        self.container.schedule(time.time() + self.period, subject=self)


try:
    container = Container(Recurring(1.0))
    container.run()
except KeyboardInterrupt:
    container.stop()
    print
예제 #13
0
    def on_start(self, event):
        self.container = event.container
        self.acceptor = event.container.listen(self.url)

    def on_link_opening(self, event):
        if event.link.is_sender:
            if event.link.remote_source and event.link.remote_source.dynamic:
                event.link.source.address = str(generate_uuid())
                self.senders[event.link.source.address] = event.link
            elif event.link.remote_target and event.link.remote_target.address:
                event.link.target.address = event.link.remote_target.address
                self.senders[event.link.remote_target.address] = event.link
            elif event.link.remote_source:
                event.link.source.address = event.link.remote_source.address
        elif event.link.remote_target:
            event.link.target.address = event.link.remote_target.address

    def on_message(self, event):
        sender = self.senders.get(event.message.reply_to)
        if sender:
            sender.send(
                Message(address=event.message.reply_to,
                        body=event.message.body.upper()))


try:
    Container(Server("localhost:8888")).run()
except KeyboardInterrupt:
    pass
예제 #14
0
                          body=self.requests[0])
            self.sender.send(req)

    def on_link_opened(self, event):
        if event.receiver == self.receiver:
            self.next_request()

    def on_message(self, event):
        print "%s => %s" % (self.requests.pop(0), event.message.body)
        if self.requests:
            self.next_request()
        else:
            event.connection.close()


REQUESTS = [
    "Twas brillig, and the slithy toves", "Did gire and gymble in the wabe.",
    "All mimsy were the borogroves,", "And the mome raths outgrabe."
]

parser = optparse.OptionParser(
    usage="usage: %prog [options]",
    description="Send requests to the supplied address and print responses.")
parser.add_option("-a",
                  "--address",
                  default="localhost:5672/examples",
                  help="address to which messages are sent (default %default)")
opts, args = parser.parse_args()

Container(Client(opts.address, args or REQUESTS)).run()