Comparison

util/pubsub.lua @ 8211:5cbbe825d9d1

util.pubsub: Add a node parameter to itemstore(). This allows the backend to know the node name, when it needs to differentiate it at storage for example.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 02 Apr 2017 19:08:54 +0100
parent 7727:0011889c928f
child 8212:66173e4b355a
comparison
equal deleted inserted replaced
8210:352d605b1178 8211:5cbbe825d9d1
3 3
4 local service = {}; 4 local service = {};
5 local service_mt = { __index = service }; 5 local service_mt = { __index = service };
6 6
7 local default_config = { __index = { 7 local default_config = { __index = {
8 itemstore = function (config) return cache.new(tonumber(config["pubsub#max_items"])) end; 8 itemstore = function (config, _) return cache.new(tonumber(config["pubsub#max_items"])) end;
9 broadcaster = function () end; 9 broadcaster = function () end;
10 get_affiliation = function () end; 10 get_affiliation = function () end;
11 capabilities = {}; 11 capabilities = {};
12 } }; 12 } };
13 local default_node_config = { __index = { 13 local default_node_config = { __index = {
221 name = node; 221 name = node;
222 subscribers = {}; 222 subscribers = {};
223 config = setmetatable(options or {}, {__index=self.node_defaults}); 223 config = setmetatable(options or {}, {__index=self.node_defaults});
224 affiliations = {}; 224 affiliations = {};
225 }; 225 };
226 self.data[node] = self.config.itemstore(self.nodes[node].config); 226 self.data[node] = self.config.itemstore(self.nodes[node].config, node);
227 self.events.fire_event("node-created", { node = node, actor = actor }); 227 self.events.fire_event("node-created", { node = node, actor = actor });
228 local ok, err = self:set_affiliation(node, true, actor, "owner"); 228 local ok, err = self:set_affiliation(node, true, actor, "owner");
229 if not ok then 229 if not ok then
230 self.nodes[node] = nil; 230 self.nodes[node] = nil;
231 self.data[node] = nil; 231 self.data[node] = nil;
306 -- 306 --
307 local node_obj = self.nodes[node]; 307 local node_obj = self.nodes[node];
308 if not node_obj then 308 if not node_obj then
309 return false, "item-not-found"; 309 return false, "item-not-found";
310 end 310 end
311 self.data[node] = self.config.itemstore(self.nodes[node].config); 311 self.data[node] = self.config.itemstore(self.nodes[node].config, node);
312 self.events.fire_event("node-purged", { node = node, actor = actor }); 312 self.events.fire_event("node-purged", { node = node, actor = actor });
313 if notify then 313 if notify then
314 self.config.broadcaster("purge", node, node_obj.subscribers); 314 self.config.broadcaster("purge", node, node_obj.subscribers);
315 end 315 end
316 return true 316 return true
422 end 422 end
423 423
424 for k,v in pairs(new_config) do 424 for k,v in pairs(new_config) do
425 node_obj.config[k] = v; 425 node_obj.config[k] = v;
426 end 426 end
427 local new_data = self.config.itemstore(self.nodes[node].config); 427 local new_data = self.config.itemstore(self.nodes[node].config, node);
428 for key, value in self.data[node]:items() do 428 for key, value in self.data[node]:items() do
429 new_data:set(key, value); 429 new_data:set(key, value);
430 end 430 end
431 self.data[node] = new_data; 431 self.data[node] = new_data;
432 return true; 432 return true;