This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
quantmind
committed
Sep 13, 2012
1 parent
deeb138
commit c3c4b63
Showing
9 changed files
with
206 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,76 @@ | ||
import logging | ||
|
||
from inspect import isclass | ||
from collections import deque | ||
|
||
from stdnet import getdb, AsyncObject | ||
from stdnet.utils.encoders import Json | ||
from stdnet.utils import is_string | ||
|
||
|
||
logger = logging.getLogger('stdnet.pubsub') | ||
|
||
|
||
class PubSubBase(object): | ||
pickler = Json | ||
|
||
|
||
class Publisher(object): | ||
'''Class which publish messages to message queues.''' | ||
def __init__(self, server = None, pickler = Json): | ||
def __init__(self, server=None, pickler=None): | ||
pickler = pickler or self.pickler | ||
if isclass(pickler): | ||
pickler = pickler() | ||
self.pickler = pickler | ||
self.client = getdb(server).client | ||
self.server = getdb(server) | ||
|
||
|
||
class Publisher(PubSubBase): | ||
'''Class which publish messages to message queues.''' | ||
def publish(self, channel, data): | ||
data = self.pickler.dumps(data) | ||
#return self.backend.publish(channel, data) | ||
return self.client.execute_command('PUBLISH', channel, data) | ||
return self.server.publish(channel, data) | ||
|
||
|
||
class Subscriber(AsyncObject): | ||
'''Subscribe to ''' | ||
def __init__(self, server = None, pickler = Json): | ||
if isclass(pickler): | ||
pickler = pickler() | ||
self.pickler = pickler | ||
self.client = getdb(server).subscriber() | ||
class Subscriber(PubSubBase): | ||
'''A subscriber to channels''' | ||
def __init__(self, server=None, pickler=None): | ||
super(Subscriber, self).__init__(server, pickler) | ||
self.channels = {} | ||
self.patterns = {} | ||
self._subscriber = self.server.subscriber( | ||
message_callback=self.message_callback) | ||
|
||
def disconnect(self): | ||
self.client.disconnect() | ||
self._subscriber.disconnect() | ||
|
||
def subscription_count(self): | ||
return self.client.subscription_count() | ||
return self._subscriber.subscription_count() | ||
|
||
def subscribe(self, channels): | ||
return self.client.subscribe(channels) | ||
def subscribe(self, *channels): | ||
return self._subscriber.subscribe(self.channel_list(channels)) | ||
|
||
def unsubscribe(self, channels = None): | ||
return self.client.unsubscribe(channels) | ||
|
||
def psubscribe(self, channels): | ||
return self.client.psubscribe(channels) | ||
def unsubscribe(self, *channels): | ||
return self._subscriber.unsubscribe(self.channel_list(channels)) | ||
|
||
def punsubscribe(self, channels = None): | ||
return self.client.punsubscribe(channels) | ||
def psubscribe(self, *channels): | ||
return self._subscriber.psubscribe(self.channel_list(channels)) | ||
|
||
def pull(self, timeout = None, count = None): | ||
'''Retrieve new messages from the subscribed channels. | ||
def punsubscribe(self, *channels): | ||
return self._subscriber.punsubscribe(self.channel_list(channels)) | ||
|
||
:parameter timeout: Optional timeout in seconds. | ||
:parameter count: Optional number of messages to retrieve.''' | ||
return self.client.pull(timeout, count, self.pickler.loads) | ||
|
||
def message_callback(self, command, channel, message=None): | ||
if command == 'subscribe': | ||
self.channels[channel] = deque() | ||
elif command == 'unsubscribe': | ||
self.channels.pop(channel, None) | ||
elif channel in self.channels: | ||
self.channels.append(message) | ||
else: | ||
logger.warn('Got message for unsubscribed channel "%s"' % channel) | ||
|
||
def channel_list(self, channels): | ||
ch = [] | ||
for channel in channels: | ||
if not isinstance(channel, (list, tuple)): | ||
ch.append(channel) | ||
else: | ||
ch.extend(channel) | ||
return ch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.