Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Feb 21, 2012
1 parent e2c6acd commit 710b552
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions stomp4py/frame.py
Expand Up @@ -9,6 +9,7 @@ def __init__(self, command, headers=None, body=None):
if self.headers is None: if self.headers is None:
self.headers = {} self.headers = {}
self.body = body or '' self.body = body or ''
self.conn_id = 0
self.error = None self.error = None


@property @property
Expand Down Expand Up @@ -51,6 +52,9 @@ def receipt_id(self):
""" RECEIPT, ERROR """ """ RECEIPT, ERROR """
return self.headers.get('receipt-id') return self.headers.get('receipt-id')


def __repr__(self):
return "<StompFrame:%s %s \"%s\">" % (self.command, self.headers, self.body)

def pack(self): def pack(self):
"""Pack the frame as a string """Pack the frame as a string
Expand Down
13 changes: 7 additions & 6 deletions stomp4py/server/base.py
Expand Up @@ -4,10 +4,10 @@
import socket import socket


class Subscription(object): class Subscription(object):
def __init__(self, handler, id, destination): def __init__(self, handler, frame):
self.handler = handler self.handler = handler
self.id = id self.id = frame.id or uuid.uuid4().hex
self.destination = destination self.destination = frame.destination
self.active = True self.active = True


def send(self, body, headers=None): def send(self, body, headers=None):
Expand All @@ -20,6 +20,7 @@ def cancel(self):
self.active = False self.active = False


class BaseHandler(object): class BaseHandler(object):
subscription_class = Subscription


def __init__(self, app=None): def __init__(self, app=None):
if app: if app:
Expand Down Expand Up @@ -65,6 +66,7 @@ def _send_message(self, subscription, body, headers=None):
self._send("MESSAGE", headers, body) self._send("MESSAGE", headers, body)


def _dispatch(self, frame): def _dispatch(self, frame):
frame.conn_id = id(self)
handler = 'handle_%s' % frame.command.lower() handler = 'handle_%s' % frame.command.lower()
if hasattr(self, handler): if hasattr(self, handler):
getattr(self, handler)(frame) getattr(self, handler)(frame)
Expand Down Expand Up @@ -95,9 +97,8 @@ def handle_send(self, frame):


def handle_subscribe(self, frame): def handle_subscribe(self, frame):
if frame.ack == 'auto': if frame.ack == 'auto':
id = frame.id or uuid.uuid4().hex subscription = self.subscription_class(self, frame)
subscription = Subscription(self, id, frame.destination) self.subscriptions[subscription.id] = subscription
self.subscriptions[id] = subscription
self.app(frame, subscription) self.app(frame, subscription)
else: else:
frame.error = "Ack mode 'auto' is the only supported mode" frame.error = "Ack mode 'auto' is the only supported mode"
Expand Down

0 comments on commit 710b552

Please sign in to comment.