aboutsummaryrefslogtreecommitdiffstats
path: root/core/portmanager.lua
diff options
context:
space:
mode:
Diffstat (limited to 'core/portmanager.lua')
-rw-r--r--core/portmanager.lua69
1 files changed, 54 insertions, 15 deletions
diff --git a/core/portmanager.lua b/core/portmanager.lua
index 103c5d44..914a8e2f 100644
--- a/core/portmanager.lua
+++ b/core/portmanager.lua
@@ -13,8 +13,8 @@ end
--- Private state
--- service_name -> service_info
-local services = {};
+-- service_name -> { service_info, ... }
+local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end });
-- service_name, interface (string), port (number)
local active_services = multitable.new();
@@ -52,28 +52,42 @@ end
module("portmanager", package.seeall);
+prosody.events.add_handler("item-added/net-provider", function (event)
+ local item = event.item;
+ register_service(item.name, item);
+end);
+prosody.events.add_handler("item-removed/net-provider", function (event)
+ local item = event.item;
+ unregister_service(item.name, item);
+end);
+
--- Public API
function activate_service(service_name)
- local service_info = services[service_name];
+ local service_info = services[service_name][1];
if not service_info then
return nil, "Unknown service: "..service_name;
end
+
+ local listener = service_info.listener;
+
+ local config_prefix = (service_info.config_prefix or service_name).."_";
+ if config_prefix == "_" then
+ config_prefix = "";
+ end
- local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
- or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9
+ local bind_interfaces = set.new(config.get("*", config_prefix.."interfaces")
+ or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9
or (service_info.private and default_local_interfaces)
or config.get("*", "interfaces")
or config.get("*", "interface") -- COMPAT w/pre-0.9
- or service_info.default_interface -- COMPAT w/pre0.9
+ or listener.default_interface -- COMPAT w/pre0.9
or default_interfaces);
- local bind_ports = set.new(config.get("*", service_name.."_ports")
- or (service_info.multiplex and config.get("*", "ports"))
+ local bind_ports = set.new(config.get("*", config_prefix.."ports")
or service_info.default_ports
- or {service_info.default_port});
+ or {listener.default_port}); -- COMPAT w/pre-0.9
- local listener = service_info.listener;
local mode = listener.default_mode or "*a";
local ssl;
if service_info.encryption == "ssl" then
@@ -85,8 +99,8 @@ function activate_service(service_name)
for interface in bind_interfaces do
for port in bind_ports do
- if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then
- log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name);
+ if #active_services:search(nil, interface, port) > 0 then
+ log("error", "Multiple services configured to listen on the same port ([%s]:%d): %s, %s", interface, port, active_services:search(nil, interface, port)[1][1].service.name or "<unnamed>", service_name or "<unnamed>");
else
local handler, err = server.addserver(interface, port, listener, mode, ssl);
if not handler then
@@ -102,6 +116,7 @@ function activate_service(service_name)
end
end
log("info", "Activated service '%s'", service_name);
+ return true;
end
function deactivate(service_name)
@@ -118,16 +133,36 @@ function deactivate(service_name)
end
function register_service(service_name, service_info)
- services[service_name] = service_info;
+ table.insert(services[service_name], service_info);
- if not active_services[service_name] then
- activate_service(service_name);
+ if not active_services:get(service_name) then
+ log("debug", "No active service for %s, activating...", service_name);
+ local ok, err = activate_service(service_name);
+ if not ok then
+ log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error");
+ end
end
fire_event("service-added", { name = service_name, service = service_info });
return true;
end
+function unregister_service(service_name, service_info)
+ local service_info_list = services[service_name];
+ for i, service in ipairs(service_info_list) do
+ if service == service_info then
+ table.remove(service_info_list, i);
+ end
+ end
+ if active_services[service_name] == service_info then
+ deactivate(service_name);
+ if #service_info_list > 0 then -- Other services registered with this name
+ activate(service_name); -- Re-activate with the next available one
+ end
+ end
+ fire_event("service-removed", { name = service_name, service = service_info });
+end
+
function get_service(service_name)
return services[service_name];
end
@@ -136,4 +171,8 @@ function get_active_services(...)
return active_services;
end
+function get_registered_services()
+ return services;
+end
+
return _M;