Qpid Connection Manager
@ 2008-10-10 21:45:48
Filed under: Code Python Tech
I didn't like how you have to connect to the Qpid service so I wrapped it in a manager of sorts and put it in the same license as the rest of the python code (ASL2.0) ...
digg it
seed it
del.icio.us
ma.gnolia
Filed under: Code Python Tech
I didn't like how you have to connect to the Qpid service so I wrapped it in a manager of sorts and put it in the same license as the rest of the python code (ASL2.0) ...
# 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.
"""
Qpid related classes.
"""
from qpid.util import connect
from qpid.connection import Connection
from qpid.datatypes import uuid4
from qpid.queue import Empty
class QPIDConnectionManager(object):
"""
QPID Connection manager.
"""
def __init__(self, host, port, user,
password, uuid=uuid4(), auto_connect=False):
"""
Constructor.
:host is the hostname to connect to.
:port is the port to connect to.
:user is the username to authenticate with.
:password is the password to authenticate with.
:uuid is the uuid4 to use.
:auto_connect lets you skip using the connect method yourself.
"""
# Set up variables
self.host = host
self.port = int(port)
self.user = user
self.password = password
self.uuid = str(uuid)
# Defaults for internal variables
self._socket = None
self._connection = None
self._session = None
# Honor autoconnect
if auto_connect:
self.connect()
def __str__(self):
"""
String representation of the object.
"""
return '<QPID "%s@%s:%s">' % (self.user, self.host, self.port)
def _is_closed(self):
"""
Internal check to see if the connection is closed.
"""
if self._session == None:
return True
return False
def __del__(self):
"""
Clean up after your own mess, boy!
"""
if not self.closed:
self.disconnect()
def connect(self):
"""
Connects based on the information inside the object.
"""
self._socket = connect(self.host, self.port)
self._connection = Connection(sock=self._socket)
self._connection.start()
self._session = self._connection.session(self.uuid)
def disconnect(self, timeout=10):
"""
Cleanly disconnect from the server.
:timeout is the amount of seconds to wait for disconnection.
"""
self._session.close(timeout=timeout)
self._connection.close()
self._socket.close()
self._session = None
# All properties
session = property(lambda self: self._session)
closed = property(lambda self: self._is_closed())
digg it
seed it
del.icio.us
ma.gnolia


