Annotate

core/portmanager.lua @ 4608:01d52f31b6b3

portmanager: Support item-added/net-provider (global and shared modules only!)
author Matthew Wild <mwild1@gmail.com>
date Wed, 14 Mar 2012 21:39:45 +0000
parent 4607:7f45b2cb3c03
child 4609:83a5377ffea2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local multitable = require "util.multitable";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local fire_event = prosody.events.fire_event;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 --- Config
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local default_interfaces = { "*" };
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local default_local_interfaces = { "127.0.0.1" };
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if config.get("*", "use_ipv6") then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 table.insert(default_interfaces, "::");
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 table.insert(default_local_interfaces, "::1");
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 --- Private state
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
16 -- service_name -> { service_info, ... }
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
17 local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end });
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- service_name, interface (string), port (number)
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local active_services = multitable.new();
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 --- Private helpers
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
4546
c686860ef410 portmanager: Pass port to friendly_error_message()
Matthew Wild <mwild1@gmail.com>
parents: 4542
diff changeset
24 local function error_to_friendly_message(service_name, port, err)
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local friendly_message = err;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if err:match(" in use") then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- FIXME: Use service_name here
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if port == 5222 or port == 5223 or port == 5269 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 friendly_message = "check that Prosody or another XMPP server is "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 .."not already running and using this port";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 elseif port == 80 or port == 81 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 friendly_message = "check that a HTTP server is not already using "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 .."this port";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 elseif port == 5280 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 friendly_message = "check that Prosody or a BOSH connection manager "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 .."is not already running";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 else
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 friendly_message = "this port is in use by another application";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 elseif err:match("permission") then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 friendly_message = "Prosody does not have sufficient privileges to use this port";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 elseif err == "no ssl context" then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 if not config.get("*", "core", "ssl") then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 friendly_message = "there is no 'ssl' config under Host \"*\" which is "
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 .."require for legacy SSL ports";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 else
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 friendly_message = "initializing SSL support failed, see previous log entries";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return friendly_message;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 module("portmanager", package.seeall);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
4608
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
55 prosody.events.add_handler("item-added/net-provider", function (event)
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
56 local item = event.item;
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
57 register_service(item.name, item);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
58 end);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
59 prosody.events.add_handler("item-removed/net-provider", function (event)
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
60 local item = event.item;
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
61 unregister_service(item.name, item);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
62 end);
01d52f31b6b3 portmanager: Support item-added/net-provider (global and shared modules only!)
Matthew Wild <mwild1@gmail.com>
parents: 4607
diff changeset
63
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 --- Public API
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function activate_service(service_name)
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
67 local service_info = services[service_name][1];
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 if not service_info then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 return nil, "Unknown service: "..service_name;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9
4583
6f2789939d35 core.portmanager: Make sure the private flag takes precedence over global interfaces
Kim Alvefur <zash@zash.se>
parents: 4546
diff changeset
74 or (service_info.private and default_local_interfaces)
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 or config.get("*", "interfaces")
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 or config.get("*", "interface") -- COMPAT w/pre-0.9
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 or service_info.default_interface -- COMPAT w/pre0.9
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 or default_interfaces);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local bind_ports = set.new(config.get("*", service_name.."_ports")
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 or (service_info.multiplex and config.get("*", "ports"))
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 or service_info.default_ports
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 or {service_info.default_port});
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 local listener = service_info.listener;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local mode = listener.default_mode or "*a";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local ssl;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if service_info.encryption == "ssl" then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 ssl = prosody.global_ssl_ctx;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if not ssl then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return nil, "global-ssl-context-required";
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 for interface in bind_interfaces do
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 for port in bind_ports do
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 else
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local handler, err = server.addserver(interface, port, listener, mode, ssl);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 if not handler then
4546
c686860ef410 portmanager: Pass port to friendly_error_message()
Matthew Wild <mwild1@gmail.com>
parents: 4542
diff changeset
102 log("error", "Failed to open server port %d on %s, %s", port, interface, error_to_friendly_message(service_name, port, err));
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 else
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 log("debug", "Added listening service %s to [%s]:%d", service_name, interface, port);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 active_services:add(service_name, interface, port, {
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 server = handler;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 service = service_info;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 });
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 log("info", "Activated service '%s'", service_name);
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
114 return true;
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 function deactivate(service_name)
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local active = active_services:search(service_name)[1];
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if not active then return; end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 for interface, ports in pairs(active) do
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 for port, active_service in pairs(ports) do
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 active_service:close();
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 active_services:remove(service_name, interface, port, active_service);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 log("debug", "Removed listening service %s from [%s]:%d", service_name, interface, port);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 log("info", "Deactivated service '%s'", service_name);
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 function register_service(service_name, service_info)
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
131 table.insert(services[service_name], service_info);
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
133 if not active_services:get(service_name) then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
134 log("debug", "No active service for %s, activating...", service_name);
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
135 local ok, err = activate_service(service_name);
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
136 if not ok then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
137 log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error");
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
138 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 fire_event("service-added", { name = service_name, service = service_info });
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return true;
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
4607
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
145 function unregister_service(service_name, service_info)
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
146 local service_info_list = services[service_name];
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
147 for i, service in ipairs(service_info_list) do
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
148 if service == service_info then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
149 table.remove(service_info_list, i);
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
150 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
151 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
152 if active_services[service_name] == service_info then
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
153 deactivate(service_name);
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
154 if #service_info_list > 0 then -- Other services registered with this name
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
155 activate(service_name); -- Re-activate with the next available one
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
156 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
157 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
158 end
7f45b2cb3c03 portmanager: Add unregister_service(), and allow multiple services with the same name (they get queued)
Matthew Wild <mwild1@gmail.com>
parents: 4598
diff changeset
159
4597
25d89c7d6aee portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents: 4583
diff changeset
160 function get_service(service_name)
25d89c7d6aee portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents: 4583
diff changeset
161 return services[service_name];
25d89c7d6aee portmanager: Add get_service()
Matthew Wild <mwild1@gmail.com>
parents: 4583
diff changeset
162 end
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
4598
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
164 function get_active_services(...)
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
165 return active_services;
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
166 end
d2bcb959d713 portmanager: Add get_active_services()
Matthew Wild <mwild1@gmail.com>
parents: 4597
diff changeset
167
4542
50aca1e0bfbd portmanager: One manager to, in the darkness, bind them
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 return _M;