diff options
| author | Devendra | 2013-07-05 22:14:19 +0530 |
|---|---|---|
| committer | Devendra | 2013-07-05 22:14:19 +0530 |
| commit | e5f767f964b651698812f19d33f5223b9a2ed508 (patch) | |
| tree | f38eb5b4e7a47952dc68092fd8ff9f0a64ad7296 /PubnubCoreAsync.py | |
| parent | 5a47adb56bfe80c34ed5b04a4824e517a3cde16d (diff) | |
| download | pubnub-python-e5f767f964b651698812f19d33f5223b9a2ed508.tar.bz2 | |
dir structure reorg
Diffstat (limited to 'PubnubCoreAsync.py')
| -rw-r--r-- | PubnubCoreAsync.py | 190 |
1 files changed, 0 insertions, 190 deletions
diff --git a/PubnubCoreAsync.py b/PubnubCoreAsync.py deleted file mode 100644 index 9c53c6e..0000000 --- a/PubnubCoreAsync.py +++ /dev/null @@ -1,190 +0,0 @@ -## www.pubnub.com - PubNub Real-time push service in the cloud. -# coding=utf8 - -## PubNub Real-time Push APIs and Notifications Framework -## Copyright (c) 2010 Stephen Blum -## http://www.pubnub.com/ - -## ----------------------------------- -## PubNub 3.1 Real-time Push Cloud API -## ----------------------------------- -import sys -import json -import time -import hashlib -import urllib2 -import uuid -try: - from hashlib import sha256 - digestmod = sha256 -except ImportError: - import Crypto.Hash.SHA256 as digestmod - sha256 = digestmod.new -import hmac -from PubnubCrypto import PubnubCrypto -from PubnubBase import PubnubBase - -class PubnubCoreAsync(PubnubBase): - - def start(self): pass - def stop(self): pass - def timeout( self, delay, callback ): - pass - - def __init__( - self, - publish_key, - subscribe_key, - secret_key = False, - cipher_key = False, - ssl_on = False, - origin = 'pubsub.pubnub.com', - uuid = None - ) : - """ - #** - #* Pubnub - #* - #* Init the Pubnub Client API - #* - #* @param string publish_key required key to send messages. - #* @param string subscribe_key required key to receive messages. - #* @param string secret_key required key to sign messages. - #* @param boolean ssl required for 2048 bit encrypted messages. - #* @param string origin PUBNUB Server Origin. - #** - - ## Initiat Class - pubnub = Pubnub( 'PUBLISH-KEY', 'SUBSCRIBE-KEY', 'SECRET-KEY', False ) - - """ - super(PubnubCoreAsync, self).__init__( - publish_key=publish_key, - subscribe_key=subscribe_key, - secret_key=secret_key, - cipher_key=cipher_key, - ssl_on=ssl_on, - origin=origin, - UUID=uuid - ) - - self.subscriptions = {} - self.timetoken = 0 - self.version = '3.4' - self.accept_encoding = 'gzip' - - def subscribe( self, args ) : - """ - #** - #* Subscribe - #* - #* This is NON-BLOCKING. - #* Listen for a message on a channel. - #* - #* @param array args with channel and message. - #* @return false on fail, array on success. - #** - - ## Subscribe Example - def receive(message) : - print(message) - return True - - ## On Connect Callback - def connected() : - pubnub.publish({ - 'channel' : 'hello_world', - 'message' : { 'some_var' : 'text' } - }) - - ## Subscribe - pubnub.subscribe({ - 'channel' : 'hello_world', - 'connect' : connected, - 'callback' : receive - }) - - """ - ## Fail if missing channel - if not 'channel' in args : - return 'Missing Channel.' - - ## Fail if missing callback - if not 'callback' in args : - return 'Missing Callback.' - - ## Capture User Input - channel = str(args['channel']) - callback = args['callback'] - connectcb = args['connect'] - - if 'errorback' in args: - errorback = args['errorback'] - else: - errorback = lambda x: x - - ## New Channel? - if not (channel in self.subscriptions) : - self.subscriptions[channel] = { - 'first' : False, - 'connected' : False, - } - - ## Ensure Single Connection - if self.subscriptions[channel]['connected'] : - return "Already Connected" - - self.subscriptions[channel]['connected'] = 1 - ## SUBSCRIPTION RECURSION - def _subscribe(): - ## STOP CONNECTION? - if not self.subscriptions[channel]['connected']: - return - - def sub_callback(response): - print response - response = json.loads(response) - ## STOP CONNECTION? - if not self.subscriptions[channel]['connected']: - return - - ## CONNECTED CALLBACK - if not self.subscriptions[channel]['first'] : - self.subscriptions[channel]['first'] = True - connectcb() - - ## PROBLEM? - if not response: - def time_callback(_time): - if not _time: - self.timeout( 1, _subscribe ) - return errorback("Lost Network Connection") - else: - self.timeout( 1, _subscribe) - - ## ENSURE CONNECTED (Call Time Function) - return self.time({ 'callback' : time_callback }) - - self.timetoken = response[1] - _subscribe() - - pc = PubnubCrypto() - out = [] - for message in response[0]: - callback(self.decrypt(message)) - - ## CONNECT TO PUBNUB SUBSCRIBE SERVERS - try: - self._request( { "urlcomponents" : [ - 'subscribe', - self.subscribe_key, - channel, - '0', - str(self.timetoken) - ], "urlparams" : {"uuid":self.uuid} }, sub_callback ) - except : - self.timeout( 1, _subscribe) - return - - ## BEGIN SUBSCRIPTION (LISTEN FOR MESSAGES) - _subscribe() |
