Annotate

util/broadcast.lua @ 2578:61e5eff54415

net.dns, net.adns: Changed dns:socket_wrapper_set to dns.socket_wrapper_set for consistency.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 10 Feb 2010 01:58:49 +0500
parent 1528:87c71e882437
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
1 -- Prosody IM
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1519
diff changeset
8
849
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local ipairs, pairs, setmetatable, type =
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 ipairs, pairs, setmetatable, type;
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 module "pubsub"
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local pubsub_node_mt = { __index = _M };
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 function new_node(name)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return setmetatable({ name = name, subscribers = {} }, pubsub_node_mt);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function set_subscribers(node, subscribers_list, list_type)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 if list_type == "array" then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for _, jid in ipairs(subscribers_list) do
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if not subscribers[jid] then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 node:add_subscriber(jid);
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 elseif (not list_type) or list_type == "set" then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for jid in pairs(subscribers_list) do
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if type(jid) == "string" then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 node:add_subscriber(jid);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function get_subscribers(node)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return node.subscribers;
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 publish(node, item, dispatcher, data)
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 for i = 1,#subscribers do
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 item.attr.to = subscribers[i];
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 dispatcher(data, item);
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
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
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function add_subscriber(node, jid)
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if not subscribers[jid] then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local space = #subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 subscribers[space] = jid;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 subscribers[jid] = space;
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
1519
2c9a650ff1b7 util.pubsub: Fix undefined global accesses
Matthew Wild <mwild1@gmail.com>
parents: 849
diff changeset
60 function remove_subscriber(node, jid)
849
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local subscribers = node.subscribers;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if subscribers[jid] then
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 subscribers[subscribers[jid]] = nil;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 subscribers[jid] = nil;
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
5049b4512df0 Adding initial util.pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return _M;