1-
21from gevent .monkey import patch_all
32patch_all ()
43
@@ -13,62 +12,93 @@ def get_unique(s):
1312 return 'str-' + rand + '-' + s
1413
1514# public channel
15+ # This is the channel all clients announce themselves on -- or more generally speaking, a channel you expect the client
16+ # to "check-in" on to announce his state
17+
1618channel_public = get_unique ("channel_public" )
1719
1820# server auth key
21+ # Only the server has/knows about this auth token. It will be used to grant read on the "check-in" Presence channel
22+
1923server_auth_token = get_unique ("server_auth_token" )
2024
21- #client auth key
25+ # client auth key
26+ # only clients will use this authey -- it does not provide presence channel read access
27+
2228client_auth_token = get_unique ("client_auth_token" )
2329
24- #client uuid
30+ # each client must have a unique id -- a UUID, for presence information/state to bind to
31+
32+ # client uuid
2533client_uuid = get_unique ("client_uuid" )
2634
27- #server uuid
35+ # server uuid
2836server_uuid = get_unique ("server_uuid" )
2937
38+ # For the demo, we'll implement a SERVER called server, who is the authoritative 'admin' entity in the system
39+ # We'll also implement a CLIENT called client, who is an arbitrary hardware device member of the network
3040
31- #init server object
41+ # Please swap out the default 'pam' demo keys with your own PAM-enabled keys
42+
43+ # init server object
3244server = Pubnub (publish_key = "pam" , subscribe_key = "pam" , secret_key = "pam" , auth_key = server_auth_token , uuid = server_uuid )
3345
34- #init client object
46+ # init client object
3547client = Pubnub (publish_key = "pam" , subscribe_key = "pam" , auth_key = client_auth_token , uuid = client_uuid )
3648
49+ # To access a Presence channel with PAM, its format is CHANNELNAME-pnpres
50+
3751# Grant permission to server auth keys
52+ # grant r/w to public, and r/w public Presence (public-pnpres)
53+
3854print (server .grant (channel = channel_public , auth_key = server_auth_token , read = True , write = True ))
3955print (server .grant (channel = channel_public + '-pnpres' , auth_key = server_auth_token , read = True , write = True ))
4056
4157# Grant permission to client auth keys
58+ # grant r/w to public, and w-only access to public Presence (public-pnpres)
4259print (server .grant (channel = channel_public , auth_key = client_auth_token , read = True , write = False ))
43- print (server .grant (channel = channel_public + '-pnpres' , auth_key = client_auth_token , read = True , write = False ))
60+ print (server .grant (channel = channel_public + '-pnpres' , auth_key = client_auth_token , read = False , write = False ))
4461
62+ # Now, we'll run it to watch it work as advertised...
4563
64+ # Define some simple callabcks for the Server and Client
4665
47- def _server_callback (message , channel ):
48- print (message )
66+ def _server_message_callback (message , channel ):
67+ print ("Server heard: " + message )
68+
69+ def _client_callback (channel , message ):
70+ print ("Client heard: " + message )
4971
5072def _error_callback (error ):
51- print (error )
73+ print ("Error: " + error )
5274
53- #server subscribes to public channel
54- server .subscribe (channels = channel_public , callback = _server_callback , error = _error_callback )
75+ def _server_presence_callback (message , channel ):
76+ print message
77+ if 'action' in message :
78+ if message ['action' ] == 'join' and message ['uuid' ] == client_uuid :
79+ print "Server can see that client with UUID " + message ['uuid' ] + " has a state of " + json .dumps (message ['data' ])
5580
81+ def _client_presence_callback (message , channel ):
82+ print message
83+ if 'action' in message :
84+ if message ['action' ] == 'join' and message ['uuid' ] == client_uuid :
85+ print "Client can see that client with UUID " + message ['uuid' ] + " has a state of " + json .dumps (message ['data' ])
5686
87+ # server subscribes to public channel
88+ server .subscribe (channels = channel_public , callback = _server_message_callback , error = _error_callback )
5789
58- def _server_presence_callback (message , channel ):
59- print message
60- if 'action' in message :
61- if message ['action' ] == 'join' and message ['uuid' ] == client_uuid :
62- print "Only I can see that client with UUID " + message ['uuid' ] + " has a state of " + json .dumps (message ['data' ])
90+ # server subscribes to presence events on public channel
91+ # presence() is a convienence method that subscribes to channel-pnpres with special logic for handling
92+ # presence-event formatted messages
6393
64- # server subscribes to presence events on public channel
94+ ## uncomment out to see server able to read on presence channel
6595server .presence (channel = channel_public , callback = _server_presence_callback , error = _error_callback )
6696
97+ # now if the client tried to subscribe on the presence channel, and therefore, get state info
98+ # he is explicitly denied!
6799
68-
69-
70- def _client_callback (channel , message ):
71- print (message )
100+ ## uncomment out to see client not able to read on presence channel
101+ #client.presence(channel=channel_public, callback=_client_presence_callback, error=_error_callback)
72102
73103# client subscribes to public channel
74104client .subscribe (channels = channel_public , state = { "myKey" : get_unique ("foo" )}, callback = _client_callback , error = _error_callback )
0 commit comments