aboutsummaryrefslogtreecommitdiffstats
path: root/python-twisted/tests/subscribe-test.py
blob: 2f4c2088312c2cbbd74fc6b505481317b1455de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
## 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 datetime
from pubnub import PubnubTwisted as Pubnub
from functools import partial
from threading import current_thread
import threading
publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
cipher_key = len(sys.argv) > 4 and sys.argv[4] or None
ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False

## -----------------------------------------------------------------------
## Initiate Pubnub State
## -----------------------------------------------------------------------
#pubnub = Pubnub( publish_key, subscribe_key, secret_key, cipher_key, ssl_on )
pubnub = Pubnub(publish_key, subscribe_key, secret_key, ssl_on)
crazy = 'hello_world'

current = -1

errors = 0
received = 0

## -----------------------------------------------------------------------
## Subscribe Example
## -----------------------------------------------------------------------


def message_received(message):
    print message


def check_received(message):
    global current
    global errors
    global received
    print message
    print current
    if message <= current:
        print 'ERROR'
        #sys.exit()
        errors += 1
    else:
        received += 1
    print 'active thread count : ', threading.activeCount()
    print 'errors = ', errors
    print current_thread().getName(), ' , ', 'received = ', received

    if received != message:
        print '********** MISSED **************** ', message - received
    current = message


def connected_test(ch):
    print 'Connected', ch


def connected(ch):
    pass


'''
pubnub.subscribe({
    'channel'  : 'abcd1',
    'connect'  : connected,
    'callback' : message_received
})
'''


def cb1():
    pubnub.subscribe({
        'channel': 'efgh1',
        'connect': connected,
        'callback': message_received
    })


def cb2():
    pubnub.subscribe({
        'channel': 'dsm-test',
        'connect': connected_test,
        'callback': check_received
    })


def cb3():
    pubnub.unsubscribe({'channel': 'efgh1'})


def cb4():
    pubnub.unsubscribe({'channel': 'abcd1'})


def subscribe(channel):
    pubnub.subscribe({
        'channel': channel,
        'connect': connected,
        'callback': message_received
    })


print threading.activeCount()


pubnub.timeout(15, cb1)

pubnub.timeout(30, cb2)


pubnub.timeout(45, cb3)

pubnub.timeout(60, cb4)

#'''
for x in range(1, 1000):
    #print x
    def y(t):
        subscribe('channel-' + str(t))

    def z(t):
        pubnub.unsubscribe({'channel': 'channel-' + str(t)})

    pubnub.timeout(x + 5, partial(y, x))
    pubnub.timeout(x + 25, partial(z, x))
    x += 10
#'''

'''
for x in range(1,1000):
    def cb(r): print r , ' : ', threading.activeCount()
    def y(t):
        pubnub.publish({
            'message' : t,
            'callback' : cb,
            'channel' : 'dsm-test'
        })


    pubnub.timeout(x + 1, partial(y,x))
    x += 1
'''


pubnub.start()