Fri, 17 Dec 2010 01:24:59 +0000
net.httpserver: Default 'ports' = {5280}
3619 | 1 | module("pubsub", package.seeall); |
2 | ||
3 | local service = {}; | |
4 | local service_mt = { __index = service }; | |
5 | ||
6 | function new(cb) | |
7 | return setmetatable({ cb = cb or {}, nodes = {} }, service_mt); | |
8 | end | |
9 | ||
10 | function service:add_subscription(node, actor, jid) | |
11 | local node_obj = self.nodes[node]; | |
12 | if not node_obj then | |
13 | return false, "item-not-found"; | |
14 | end | |
15 | node_obj.subscribers[jid] = true; | |
16 | return true; | |
17 | end | |
18 | ||
19 | function service:remove_subscription(node, actor, jid) | |
3821
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
20 | local node_obj = self.nodes[node]; |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
21 | if not node_obj then |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
22 | return false, "item-not-found"; |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
23 | end |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
24 | if not node_obj.subscribers[jid] then |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
25 | return false, "not-subscribed"; |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
26 | end |
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
27 | node_obj.subscribers[jid] = nil; |
3619 | 28 | return true; |
29 | end | |
30 | ||
3626
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
31 | function service:get_subscription(node, actor, jid) |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
32 | local node_obj = self.nodes[node]; |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
33 | if node_obj then |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
34 | return node_obj.subscribers[jid]; |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
35 | end |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
36 | end |
444f965baed8
util.pubsub: Add :get_subscription() to return the current subscription for a JID, if any
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
37 | |
3796
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
38 | function service:create(node, actor) |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
39 | if not self.nodes[node] then |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
40 | self.nodes[node] = { name = node, subscribers = {}, config = {}, data = {} }; |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
41 | return true; |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
42 | end |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
43 | return false, "conflict"; |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
44 | end |
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
45 | |
3619 | 46 | function service:publish(node, actor, id, item) |
47 | local node_obj = self.nodes[node]; | |
48 | if not node_obj then | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
49 | node_obj = { name = node, subscribers = {}, config = {}, data = {} }; |
3619 | 50 | self.nodes[node] = node_obj; |
51 | end | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
52 | node_obj.data[id] = item; |
3619 | 53 | self.cb.broadcaster(node, node_obj.subscribers, item); |
54 | return true; | |
55 | end | |
56 | ||
3822
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
57 | function service:retract(node, actor, id, retract) |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
58 | local node_obj = self.nodes[node]; |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
59 | if (not node_obj) or (not node_obj.data[id]) then |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
60 | return false, "item-not-found"; |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
61 | end |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
62 | node_obj.data[id] = nil; |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
63 | if retract then |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
64 | self.cb.broadcaster(node, node_obj.subscribers, retract); |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
65 | end |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
66 | return true |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
67 | end |
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
68 | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
69 | function service:get(node, actor, id) |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
70 | local node_obj = self.nodes[node]; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
71 | if node_obj then |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
72 | if id then |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
73 | return { node_obj.data[id] }; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
74 | else |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
75 | return node_obj.data; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
76 | end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
77 | end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
78 | end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3626
diff
changeset
|
79 | |
3619 | 80 | return _M; |