Annotate

net/server.lua @ 6581:f2a7ad099e01

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Tue, 24 Feb 2015 15:22:34 +0000
parent 6485:4224abbf0fdd
child 7099:8e64e7b82928
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2925
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
1 -- Prosody IM
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5531
diff changeset
4 --
2925
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
6 -- COPYING file in the source package for more information.
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
7 --
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
8
6483
63539dfb96e7 net.server: Rename 'server' config option to 'network_backend' (to select which net.server implementation to use)
Matthew Wild <mwild1@gmail.com>
parents: 6482
diff changeset
9 local server_type = prosody and require "core.configmanager".get("*", "network_backend") or "select";
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
10 if prosody and require "core.configmanager".get("*", "use_libevent") then
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
11 server_type = "event";
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
12 end
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
14 if server_type == "event" then
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
15 if not pcall(require, "luaevent.core") then
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
16 log("error", "libevent not found, falling back to select()");
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
17 server_type = "select"
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
18 end
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
19 end
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
20
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local server;
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
22 local set_config;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
23 if server_type == "event" then
4811
1d1fdfa29f06 net.server: server_select != server_event (thanks Nulani, Florob)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
24 server = require "net.server_event";
4808
07d0a3a75c8a net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents: 3461
diff changeset
25
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
26 local defaults = {};
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
27 for k,v in pairs(server.cfg) do
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
28 defaults[k] = v;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
29 end
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
30 function set_config(settings)
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
31 local event_settings = {
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
32 ACCEPT_DELAY = settings.event_accept_retry_interval;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
33 ACCEPT_QUEUE = settings.tcp_backlog;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
34 CLEAR_DELAY = settings.event_clear_interval;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
35 CONNECT_TIMEOUT = settings.connect_timeout;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
36 DEBUG = settings.debug;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
37 HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
38 MAX_CONNECTIONS = settings.max_connections;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
39 MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
40 MAX_READ_LENGTH = settings.max_receive_buffer_size;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
41 MAX_SEND_LENGTH = settings.max_send_buffer_size;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
42 READ_TIMEOUT = settings.read_timeout;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
43 WRITE_TIMEOUT = settings.send_timeout;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
44 };
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
45
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
46 for k,default in pairs(defaults) do
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
47 server.cfg[k] = event_settings[k] or default;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
48 end
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
49 end
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
50 elseif server_type == "select" then
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
51 server = require "net.server_select";
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
52
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
53 local defaults = {};
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
54 for k,v in pairs(server.getsettings()) do
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
55 defaults[k] = v;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
56 end
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
57 function set_config(settings)
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
58 local select_settings = {};
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
59 for k,default in pairs(defaults) do
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
60 select_settings[k] = settings[k] or default;
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
61 end
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
62 server.changesettings(select_settings);
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
63 end
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 else
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
65 error("Unsupported server type")
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
6482
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
68 -- If server.hook_signal exists, replace signal.signal()
6485
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
69 local has_signal, signal = pcall(require, "util.signal");
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
70 if has_signal then
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
71 if server.hook_signal then
6482
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
72 function signal.signal(signal_id, handler)
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
73 if type(signal_id) == "string" then
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
74 signal_id = signal[signal_id:upper()];
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
75 end
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
76 if type(signal_id) ~= "number" then
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
77 return false, "invalid-signal";
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
78 end
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
79 return server.hook_signal(signal_id, handler);
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
80 end
6485
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
81 else
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
82 server.hook_signal = signal.signal;
6482
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
83 end
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
84 else
6485
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
85 if not server.hook_signal then
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
86 server.hook_signal = function()
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
87 return false, "signal hooking not supported"
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
88 end
4224abbf0fdd net/server: Handle lack of util.signal correctly
daurnimator <quae@daurnimator.com>
parents: 6484
diff changeset
89 end
6482
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
90 end
410067cdeb2f net/server: If server.hook_signal exists, overwrite signal.signal; else make server.hook_signal == signal.signal
daurnimator <quae@daurnimator.com>
parents: 6480
diff changeset
91
5198
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
92 if prosody then
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
93 local config_get = require "core.configmanager".get;
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
94 local function load_config()
5383
143670deee3e net.server: No sections in config anymore
Kim Alvefur <zash@zash.se>
parents: 5284
diff changeset
95 local settings = config_get("*", "network_settings") or {};
6480
37b12475f648 net/server: Split up different backends in a nicer way. Add global config option 'server'
daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
96 return set_config(settings);
5198
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
97 end
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
98 load_config();
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
99 prosody.events.add_handler("config-reloaded", load_config);
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
100 end
430797a8fc81 net.server: Make server_* configurable in the config file.
Kim Alvefur <zash@zash.se>
parents: 4811
diff changeset
101
2136
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
102 -- require "net.server" shall now forever return this,
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
103 -- ie. server_select or server_event as chosen above.
2645
625d02b2a1a0 net.server: Remove redundant commented code and some trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2435
diff changeset
104 return server;