Comparison

core/portmanager.lua @ 4607:7f45b2cb3c03

portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
author Matthew Wild <mwild1@gmail.com>
date Wed, 14 Mar 2012 21:39:02 +0000
parent 4598:d2bcb959d713
child 4608:01d52f31b6b3
comparison
equal deleted inserted replaced
4606:17785dbd9d58 4607:7f45b2cb3c03
11 table.insert(default_local_interfaces, "::1"); 11 table.insert(default_local_interfaces, "::1");
12 end 12 end
13 13
14 --- Private state 14 --- Private state
15 15
16 -- service_name -> service_info 16 -- service_name -> { service_info, ... }
17 local services = {}; 17 local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end });
18 18
19 -- service_name, interface (string), port (number) 19 -- service_name, interface (string), port (number)
20 local active_services = multitable.new(); 20 local active_services = multitable.new();
21 21
22 --- Private helpers 22 --- Private helpers
53 module("portmanager", package.seeall); 53 module("portmanager", package.seeall);
54 54
55 --- Public API 55 --- Public API
56 56
57 function activate_service(service_name) 57 function activate_service(service_name)
58 local service_info = services[service_name]; 58 local service_info = services[service_name][1];
59 if not service_info then 59 if not service_info then
60 return nil, "Unknown service: "..service_name; 60 return nil, "Unknown service: "..service_name;
61 end 61 end
62 62
63 local bind_interfaces = set.new(config.get("*", service_name.."_interfaces") 63 local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
100 end 100 end
101 end 101 end
102 end 102 end
103 end 103 end
104 log("info", "Activated service '%s'", service_name); 104 log("info", "Activated service '%s'", service_name);
105 return true;
105 end 106 end
106 107
107 function deactivate(service_name) 108 function deactivate(service_name)
108 local active = active_services:search(service_name)[1]; 109 local active = active_services:search(service_name)[1];
109 if not active then return; end 110 if not active then return; end
116 end 117 end
117 log("info", "Deactivated service '%s'", service_name); 118 log("info", "Deactivated service '%s'", service_name);
118 end 119 end
119 120
120 function register_service(service_name, service_info) 121 function register_service(service_name, service_info)
121 services[service_name] = service_info; 122 table.insert(services[service_name], service_info);
122 123
123 if not active_services[service_name] then 124 if not active_services:get(service_name) then
124 activate_service(service_name); 125 log("debug", "No active service for %s, activating...", service_name);
126 local ok, err = activate_service(service_name);
127 if not ok then
128 log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error");
129 end
125 end 130 end
126 131
127 fire_event("service-added", { name = service_name, service = service_info }); 132 fire_event("service-added", { name = service_name, service = service_info });
128 return true; 133 return true;
134 end
135
136 function unregister_service(service_name, service_info)
137 local service_info_list = services[service_name];
138 for i, service in ipairs(service_info_list) do
139 if service == service_info then
140 table.remove(service_info_list, i);
141 end
142 end
143 if active_services[service_name] == service_info then
144 deactivate(service_name);
145 if #service_info_list > 0 then -- Other services registered with this name
146 activate(service_name); -- Re-activate with the next available one
147 end
148 end
129 end 149 end
130 150
131 function get_service(service_name) 151 function get_service(service_name)
132 return services[service_name]; 152 return services[service_name];
133 end 153 end