annotate nodes.js @ 0:9ee956af41e3

Initial commit
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 27 Jun 2010 22:05:12 +0200
parents
children c2954a9e5665
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 /*
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 *
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 * This file is part of PSĜS, a PubSub server written in JavaScript.
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 *
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 * PSĜS is free software: you can redistribute it and/or modify
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 * it under the terms of the GNU Affero General Public License as
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 * License.
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 *
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 * PSĜS is distributed in the hope that it will be useful,
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 * GNU Affero General Public License for more details.
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 *
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 * You should have received a copy of the GNU Affero General Public License
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>.
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 */
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 require('./iso8601');
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 var errors = require('./errors');
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 var config = require('./configuration');
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 var service_configuration = config.service_configuration;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 var Configuration = config.Configuration;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 var makeRandomId = require('./util').makeRandomId;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 exports.Item = function() {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 this.content = null;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 this.date = new Date();
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 return this;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 };
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 exports.Node = function(params) {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 this.items = {};
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 if (config.enabled('subscribe'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 this.subscribers = {};
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 this.owner = ['lm@slam'];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 if (config.enabled('publisher-affiliation'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 this.publisher = [];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 if (config.enabled('publish-only-affiliation'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 this.publishOnly = [];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 if (config.enabled('member-affiliation'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 this.member = [];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 if (config.enabled('outcast-affiliation'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 this.outcast = [];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 if (config.enabled('meta-data'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 this.metadata = new Configuration(service_configuration.node_metadata, params);
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 if (config.enabled('config-node'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 this.configuration = new Configuration(service_configuration.node_config, params);
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 if (config.enabled('subscription-options'))
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 this.subsConfig = new Configuration(service_configuration.subscribe_options, params);
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 return this;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 };
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 exports.Node.prototype = {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 setItem: function(name, content) {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 if (typeof content == 'undefined') {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 if (this.items[name]) {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 delete this.items[name];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 return errors.success;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 }
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 return 42; //XXX
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 }
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 if (!this.items[name])
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 this.items[name] = new exports.Item();
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 this.items[name].content = content;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 return errors.success;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 },
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 setSubscriber: function(jid, type, subid, params) {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 if (type == 'none') {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 delete this.subscribers[jid];
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 this.metadata['pubsub#num_subscribers']--;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 return errors.success;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 }
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 if (!subid)
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 subid = makeRandomId();
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 this.subscribers[jid] = {
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 type: type,
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 subid: subid,
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 options: new Configuration(service_configuration.subscribe_options, params),
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 this.metadata['pubsub#num_subscribers']++;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 return subid;
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 },
9ee956af41e3 Initial commit
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 };