aboutsummaryrefslogtreecommitdiffstats
path: root/lib/inets/examples/httpd_load_test/hdlt_server.erl
blob: 3e5a849d5b7f1543642abc6a40d98478f3601b01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%%----------------------------------------------------------------------
%% Purpose: The HDLT server module.
%%          This is just a stub, making future expansion easy.
%%          All code in this module is executed in the local node!
%%----------------------------------------------------------------------

-module(hdlt_server).

-export([start/1, stop/0, start_inets/0, start_service/1]).

-export([proxy/1]).

-include_lib("kernel/include/file.hrl").
-include("hdlt_logger.hrl").


-define(PROXY, hdlt_proxy).


%% This function is used to start the proxy process
%% This function is called *after* the nodes has been 
%% "connected" with the controller/collector node.

start(Debug) ->
    proc_lib:start(?MODULE, proxy, [Debug]).

stop() ->
    ?PROXY ! stop.

start_inets() ->
    ?PROXY ! start_inets.

start_service(Config) ->
    ?PROXY ! {server_start, Config, self()},
    receive
	{server_start_result, Result} ->
	    Result
    after 15000 ->
	    {error, timeout}
    end.


proxy(Debug) ->
    process_flag(trap_exit, true),
    erlang:register(?PROXY, self()),
    ?SET_NAME("HDLT PROXY"),
    ?SET_LEVEL(Debug), 
    ?LOG("starting", []),
    Ref = await_for_controller(10), 
    CtrlNode = node(Ref), 
    erlang:monitor_node(CtrlNode, true),
    proc_lib:init_ack({ok, self()}),
    ?DEBUG("started", []),
    proxy_loop(Ref, CtrlNode).

await_for_controller(N) when N > 0 ->
    case global:whereis_name(hdlt_ctrl) of
	Pid when is_pid(Pid) ->
	    erlang:monitor(process, Pid);
	_ ->
	    timer:sleep(1000),
	    await_for_controller(N-1)
    end;
await_for_controller(_) ->
    proc_lib:init_ack({error, controller_not_found, nodes()}),
    timer:sleep(500),
    halt().

    
proxy_loop(Ref, CtrlNode) ->
    ?DEBUG("await command", []),
    receive
	stop ->
	    ?LOG("received stop", []),
	    halt();

	start_inets ->
	    ?LOG("start the inets service framework", []),
	    case (catch inets:start()) of
		ok ->
		    ?LOG("framework started", []),
		    proxy_loop(Ref, CtrlNode);
		Error ->
		    ?LOG("failed starting inets service framework: "
			 "~n   Error: ~p", [Error]),
		    halt()
	    end;

	{server_start, Config, From} ->
	    ?LOG("start-server", []),
	    maybe_start_crypto_and_ssl(Config),
	    %% inets:enable_trace(max, "/tmp/inets-httpd-trace.log", httpd),
	    %% inets:enable_trace(max, "/tmp/inets-httpd-trace.log", all),
	    case (catch inets:start(httpd, Config)) of
		{ok, _} ->
		    ?LOG("server started when"
			 "~n   which(inets): ~p"
			 "~n   RootDir:      ~p"
			 "~n   System info:  ~p", [code:which(inets), 
						   code:root_dir(), 
						   get_node_info()]),
		    From ! {server_start_result, ok},
		    proxy_loop(Ref, CtrlNode);
		Error ->
		    ?INFO("server start failed"
			  "~n   Error: ~p", [Error]),
		    From ! {server_start_result, Error},
		    halt()
	    end;

	{nodedown, CtrlNode} ->
	    ?LOG("received nodedown for controller node - terminate", []), 
	    halt();

	{'DOWN', Ref, process, _, _} ->
	    ?LOG("received DOWN message for controller - terminate", []),
	    %% The controller has terminated, time to die
	    halt()

    end.


maybe_start_crypto_and_ssl(Config) ->
    case lists:keysearch(socket_type, 1, Config) of
	{value, {socket_type, SocketType}} when ((SocketType =:= ssl) orelse
						 (SocketType =:= ossl) orelse
						 (SocketType =:= essl)) ->
	    ?LOG("maybe start crypto and ssl", []),
	    (catch crypto:start()), 
	    ssl:start();
	_ ->
	    ok
    end.


get_node_info() ->
    [{cpu_topology,        erlang:system_info(cpu_topology)},
     {heap_type,           erlang:system_info(heap_type)},
     {nof_schedulers,      erlang:system_info(schedulers)},
     {otp_release,         erlang:system_info(otp_release)}, 
     {version,             erlang:system_info(version)}, 
     {system_version,      erlang:system_info(system_version)},
     {system_architecture, erlang:system_info(system_architecture)}].