#!/usr/bin/env python

from qpid import delegates
from qpid.connection010 import Connection
from qpid.util import connect, listen
from qpid.spec010 import load
from qpid.session import Client
from qpid.datatypes import Message
from qpid.log import enable, DEBUG, WARN

import sys

if "-v" in sys.argv:
  level = DEBUG
else:
  level = WARN

enable("qpid", level)

spec = load("../specs/amqp.0-10.xml")

class Server:

  def connection(self, connection):
    return delegates.Server(connection, self.session)

  def session(self, session):
    session.auto_sync = False
    return SessionDelegate(session)

class SessionDelegate(Client):

  def __init__(self, session):
    self.session = session

  def queue_declare(self, qd):
    print "Queue %s declared..." % qd.queue

  def queue_query(self, qq):
    return qq._type.result.type.new((qq.queue,), {})

  def message_transfer(self, cmd, headers, body):
    m = Message(body)
    m.headers = headers
    self.session.message_transfer(cmd.destination, cmd.accept_mode, cmd.acquire_mode, m)

  def message_accept(self, messages):
    print "ACCEPT %s" % messages

server = Server()

for s in listen("0.0.0.0", spec.port):
  conn = Connection(s, spec, server.connection)
  conn.start(5)
