Annotate

util/pubsub.lua @ 849:5049b4512df0

Adding initial util.pubsub
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Feb 2009 23:16:27 +0000
child 1519:2c9a650ff1b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
849
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local ipairs, pairs, setmetatable, type =
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 ipairs, pairs, setmetatable, type;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 module "pubsub"
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local pubsub_node_mt = { __index = _M };
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 function new_node(name)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 return setmetatable({ name = name, subscribers = {} }, pubsub_node_mt);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 function set_subscribers(node, subscribers_list, list_type)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if list_type == "array" then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 for _, jid in ipairs(subscribers_list) do
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if not subscribers[jid] then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 node:add_subscriber(jid);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 elseif (not list_type) or list_type == "set" then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 for jid in pairs(subscribers_list) do
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 if type(jid) == "string" then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 node:add_subscriber(jid);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function get_subscribers(node)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 return node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 function publish(node, item, dispatcher, data)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 for i = 1,#subscribers do
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 item.attr.to = subscribers[i];
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 dispatcher(data, item);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 function add_subscriber(node, jid)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if not subscribers[jid] then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local space = #subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 subscribers[space] = jid;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 subscribers[jid] = space;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 function remove_subscriber(node, subscriber)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 if subscribers[jid] then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 subscribers[subscribers[jid]] = nil;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 subscribers[jid] = nil;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return _M;