Comparison

net/server.lua @ 6480:37b12475f648

net/server: Split up different backends in a nicer way. Add global config option 'server'
author daurnimator <quae@daurnimator.com>
date Fri, 17 Oct 2014 17:30:21 -0400
parent 5776:bd0ff8ae98a8
child 6482:410067cdeb2f
comparison
equal deleted inserted replaced
6479:d016437e01bf 6480:37b12475f648
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local use_luaevent = prosody and require "core.configmanager".get("*", "use_libevent"); 9 local server_type = prosody and require "core.configmanager".get("*", "server") or "select";
10 if prosody and require "core.configmanager".get("*", "use_libevent") then
11 server_type = "event";
12 end
10 13
11 if use_luaevent then 14 if server_type == "event" then
12 use_luaevent = pcall(require, "luaevent.core"); 15 if not pcall(require, "luaevent.core") then
13 if not use_luaevent then 16 print(log)
14 log("error", "libevent not found, falling back to select()"); 17 log("error", "libevent not found, falling back to select()");
18 server_type = "select"
15 end 19 end
16 end 20 end
17 21
18 local server; 22 local server;
19 23 local set_config;
20 if use_luaevent then 24 if server_type == "event" then
21 server = require "net.server_event"; 25 server = require "net.server_event";
22 26
23 -- Overwrite signal.signal() because we need to ask libevent to 27 -- Overwrite signal.signal() because we need to ask libevent to
24 -- handle them instead 28 -- handle them instead
25 local ok, signal = pcall(require, "util.signal"); 29 local ok, signal = pcall(require, "util.signal");
33 return false, "invalid-signal"; 37 return false, "invalid-signal";
34 end 38 end
35 return server.hook_signal(signal_id, handler); 39 return server.hook_signal(signal_id, handler);
36 end 40 end
37 end 41 end
42
43 local defaults = {};
44 for k,v in pairs(server.cfg) do
45 defaults[k] = v;
46 end
47 function set_config(settings)
48 local event_settings = {
49 ACCEPT_DELAY = settings.event_accept_retry_interval;
50 ACCEPT_QUEUE = settings.tcp_backlog;
51 CLEAR_DELAY = settings.event_clear_interval;
52 CONNECT_TIMEOUT = settings.connect_timeout;
53 DEBUG = settings.debug;
54 HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
55 MAX_CONNECTIONS = settings.max_connections;
56 MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
57 MAX_READ_LENGTH = settings.max_receive_buffer_size;
58 MAX_SEND_LENGTH = settings.max_send_buffer_size;
59 READ_TIMEOUT = settings.read_timeout;
60 WRITE_TIMEOUT = settings.send_timeout;
61 };
62
63 for k,default in pairs(defaults) do
64 server.cfg[k] = event_settings[k] or default;
65 end
66 end
67 elseif server_type == "select" then
68 server = require "net.server_select";
69
70 local defaults = {};
71 for k,v in pairs(server.getsettings()) do
72 defaults[k] = v;
73 end
74 function set_config(settings)
75 local select_settings = {};
76 for k,default in pairs(defaults) do
77 select_settings[k] = settings[k] or default;
78 end
79 server.changesettings(select_settings);
80 end
38 else 81 else
39 use_luaevent = false; 82 error("Unsupported server type")
40 server = require "net.server_select";
41 end 83 end
42 84
43 if prosody then 85 if prosody then
44 local config_get = require "core.configmanager".get; 86 local config_get = require "core.configmanager".get;
45 local defaults = {};
46 for k,v in pairs(server.cfg or server.getsettings()) do
47 defaults[k] = v;
48 end
49 local function load_config() 87 local function load_config()
50 local settings = config_get("*", "network_settings") or {}; 88 local settings = config_get("*", "network_settings") or {};
51 if use_luaevent then 89 return set_config(settings);
52 local event_settings = {
53 ACCEPT_DELAY = settings.event_accept_retry_interval;
54 ACCEPT_QUEUE = settings.tcp_backlog;
55 CLEAR_DELAY = settings.event_clear_interval;
56 CONNECT_TIMEOUT = settings.connect_timeout;
57 DEBUG = settings.debug;
58 HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
59 MAX_CONNECTIONS = settings.max_connections;
60 MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
61 MAX_READ_LENGTH = settings.max_receive_buffer_size;
62 MAX_SEND_LENGTH = settings.max_send_buffer_size;
63 READ_TIMEOUT = settings.read_timeout;
64 WRITE_TIMEOUT = settings.send_timeout;
65 };
66
67 for k,default in pairs(defaults) do
68 server.cfg[k] = event_settings[k] or default;
69 end
70 else
71 local select_settings = {};
72 for k,default in pairs(defaults) do
73 select_settings[k] = settings[k] or default;
74 end
75 server.changesettings(select_settings);
76 end
77 end 90 end
78 load_config(); 91 load_config();
79 prosody.events.add_handler("config-reloaded", load_config); 92 prosody.events.add_handler("config-reloaded", load_config);
80 end 93 end
81 94