Author: rhs
Date: Sun Oct 11 20:39:01 2009
New Revision: 824145

URL: http://svn.apache.org/viewvc?rev=824145&view=rev
Log:
added ability to specify reply-to and message properties for ping; also added 
server example

Added:
    qpid/trunk/qpid/python/examples/api/server   (with props)
Modified:
    qpid/trunk/qpid/python/examples/api/ping

Modified: qpid/trunk/qpid/python/examples/api/ping
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/python/examples/api/ping?rev=824145&r1=824144&r2=824145&view=diff
==============================================================================
--- qpid/trunk/qpid/python/examples/api/ping (original)
+++ qpid/trunk/qpid/python/examples/api/ping Sun Oct 11 20:39:01 2009
@@ -22,6 +22,16 @@
 from qpid.messaging import *
 from qpid.util import URL
 
+def nameval(st):
+  idx = st.find("=")
+  if idx >= 0:
+    name = st[0:idx]
+    value = st[idx+1:]
+  else:
+    name = st
+    value = None
+  return name, value
+
 parser = optparse.OptionParser(usage="usage: %prog [options] ADDRESS [ CONTENT 
... ]",
                                description="Drain messages from the supplied 
address.")
 parser.add_option("-b", "--broker", default="localhost",
@@ -30,9 +40,12 @@
                   help="stop after count messages have been sent, zero 
disables (default %default)")
 parser.add_option("-t", "--timeout", type=float, default=None,
                   help="exit after the specified time")
-parser.add_option("-m", "--map", action="store_true",
-                  help="interpret content as map")
 parser.add_option("-i", "--id", help="use the supplied id instead of 
generating one")
+parser.add_option("-r", "--reply-to", help="specify reply-to address")
+parser.add_option("-P", "--property", dest="properties", action="append", 
default=[],
+                  help="specify message property")
+parser.add_option("-M", "--map", dest="entries", action="append", default=[],
+                  help="specify map entry for message body")
 
 opts, args = parser.parse_args()
 
@@ -45,12 +58,23 @@
   addr = args.pop(0)
 else:
   parser.error("address is required")
+
+content = None
+
 if args:
-  content = " ".join(args)
-  if opts.map:
-    content = eval(content)
+  text = " ".join(args)
+else:
+  text = None
+
+if opts.entries:
+  content = {}
+  if text:
+    content["text"] = text
+  for e in opts.entries:
+    name, val = nameval(e)
+    content[name] = val
 else:
-  content = None
+  content = text
 
 # XXX: should make URL default the port for us
 conn = Connection.open(url.host, url.port or AMQP_PORT,
@@ -62,8 +86,11 @@
 start = time.time()
 while (opts.count == 0 or count < opts.count) and \
       (opts.timeout is None or time.time() - start < opts.timeout):
-  msg = Message(content)
+  msg = Message(content, reply_to=opts.reply_to)
   msg.properties["ping-id"] = "%s:%s" % (ping_id, count)
+  for p in opts.properties:
+    name, val = nameval(p)
+    msg.properties[name] = val
 
   try:
     snd.send(msg)

Added: qpid/trunk/qpid/python/examples/api/server
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/python/examples/api/server?rev=824145&view=auto
==============================================================================
--- qpid/trunk/qpid/python/examples/api/server (added)
+++ qpid/trunk/qpid/python/examples/api/server Sun Oct 11 20:39:01 2009
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.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.
+#
+
+import optparse, sys, traceback
+from qpid.messaging import *
+from qpid.util import URL
+from subprocess import Popen, STDOUT, PIPE
+
+parser = optparse.OptionParser(usage="usage: %prog [options] ADDRESS ...",
+                               description="handle requests from the supplied 
address.")
+parser.add_option("-b", "--broker", default="localhost",
+                  help="connect to specified BROKER (default %default)")
+
+opts, args = parser.parse_args()
+
+url = URL(opts.broker)
+if args:
+  addr = args.pop(0)
+else:
+  parser.error("address is required")
+
+# XXX: should make URL default the port for us
+conn = Connection.open(url.host, url.port or AMQP_PORT,
+                       username=url.user, password=url.password)
+conn.reconnect = True
+ssn = conn.session()
+rcv = ssn.receiver(addr)
+
+def dispatch(msg):
+  msg_type = msg.properties.get("type")
+  if msg_type == "shell":
+    proc = Popen(msg.content, shell=True, stderr=STDOUT, stdin=PIPE, 
stdout=PIPE)
+    output, _ = proc.communicate()
+    result = Message(output)
+    result.properties["exit"] = proc.returncode
+  elif msg_type == "eval":
+    try:
+      content = eval(msg.content)
+    except:
+      content = traceback.format_exc()
+    result = Message(content)
+  else:
+    result = Message("unrecognized message type: %s" % msg_type)
+  return result
+
+while True:
+  try:
+    msg = rcv.fetch()
+    response = dispatch(msg)
+    snd = ssn.sender(msg.reply_to)
+    try:
+      snd.send(response)
+    except SendError, e:
+      print e
+    snd.close()
+    ssn.acknowledge()
+  except Empty:
+    break
+  except ReceiveError, e:
+    print e
+    break
+
+conn.close()

Propchange: qpid/trunk/qpid/python/examples/api/server
------------------------------------------------------------------------------
    svn:executable = *



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to