/source/otp_src_R14B02/lib/dialyzer/test/small_tests_SUITE_data/src/comm_layer/comm_acceptor.erl

https://github.com/akiernan/omnibus · Erlang · 120 lines · 80 code · 9 blank · 31 comment · 1 complexity · 1b8a09498a73368ed7ac5805183fb71b MD5 · raw file

  1. % Copyright 2008 Konrad-Zuse-Zentrum für Informationstechnik Berlin
  2. %
  3. % Licensed under the Apache License, Version 2.0 (the "License");
  4. % you may not use this file except in compliance with the License.
  5. % You may obtain a copy of the License at
  6. %
  7. % http://www.apache.org/licenses/LICENSE-2.0
  8. %
  9. % Unless required by applicable law or agreed to in writing, software
  10. % distributed under the License is distributed on an "AS IS" BASIS,
  11. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. % See the License for the specific language governing permissions and
  13. % limitations under the License.
  14. %%%-------------------------------------------------------------------
  15. %%% File : comm_acceptor.erl
  16. %%% Author : Thorsten Schuett <schuett@zib.de>
  17. %%% Description : Acceptor
  18. %%% This module accepts new connections and starts corresponding
  19. %%% comm_connection processes.
  20. %%%
  21. %%% Created : 18 Apr 2008 by Thorsten Schuett <schuett@zib.de>
  22. %%%-------------------------------------------------------------------
  23. %% @author Thorsten Schuett <schuett@zib.de>
  24. %% @copyright 2008 Konrad-Zuse-Zentrum für Informationstechnik Berlin
  25. %% @version $Id $
  26. -module(comm_layer_dir.comm_acceptor).
  27. -export([start_link/1, init/2]).
  28. -import(config).
  29. -import(gen_tcp).
  30. -import(inet).
  31. -import(log).
  32. -import(lists).
  33. -import(process_dictionary).
  34. start_link(InstanceId) ->
  35. Pid = spawn_link(comm_layer_dir.comm_acceptor, init, [InstanceId, self()]),
  36. receive
  37. {started} ->
  38. {ok, Pid}
  39. end.
  40. init(InstanceId, Supervisor) ->
  41. process_dictionary:register_process(InstanceId, acceptor, self()),
  42. erlang:register(comm_layer_acceptor, self()),
  43. log:log(info,"[ CC ] listening on ~p:~p", [config:listenIP(), config:listenPort()]),
  44. LS = case config:listenIP() of
  45. undefined ->
  46. open_listen_port(config:listenPort(), first_ip());
  47. _ ->
  48. open_listen_port(config:listenPort(), config:listenIP())
  49. end,
  50. {ok, {_LocalAddress, LocalPort}} = inet:sockname(LS),
  51. comm_port:set_local_address(undefined, LocalPort),
  52. %io:format("this() == ~w~n", [{LocalAddress, LocalPort}]),
  53. Supervisor ! {started},
  54. server(LS).
  55. server(LS) ->
  56. case gen_tcp:accept(LS) of
  57. {ok, S} ->
  58. case comm_port:get_local_address_port() of
  59. {undefined, LocalPort} ->
  60. {ok, {MyIP, _LocalPort}} = inet:sockname(S),
  61. comm_port:set_local_address(MyIP, LocalPort);
  62. _ ->
  63. ok
  64. end,
  65. receive
  66. {tcp, S, Msg} ->
  67. {endpoint, Address, Port} = binary_to_term(Msg),
  68. % auto determine remote address, when not sent correctly
  69. NewAddress = if Address =:= {0,0,0,0} orelse Address =:= {127,0,0,1} ->
  70. case inet:peername(S) of
  71. {ok, {PeerAddress, _Port}} ->
  72. % io:format("Sent Address ~p\n",[Address]),
  73. % io:format("Peername is ~p\n",[PeerAddress]),
  74. PeerAddress;
  75. {error, _Why} ->
  76. % io:format("Peername error ~p\n",[Why]).
  77. Address
  78. end;
  79. true ->
  80. % io:format("Address is ~p\n",[Address]),
  81. Address
  82. end,
  83. NewPid = comm_connection:new(NewAddress, Port, S),
  84. gen_tcp:controlling_process(S, NewPid),
  85. inet:setopts(S, [{active, once}, {send_timeout, config:read(tcp_send_timeout)}]),
  86. comm_port:register_connection(NewAddress, Port, NewPid, S)
  87. end,
  88. server(LS);
  89. Other ->
  90. log:log(warn,"[ CC ] unknown message ~p", [Other])
  91. end.
  92. open_listen_port({From, To}, IP) ->
  93. open_listen_port(lists:seq(From, To), IP);
  94. open_listen_port([Port | Rest], IP) ->
  95. case gen_tcp:listen(Port, [binary, {packet, 4}, {reuseaddr, true},
  96. {active, once}, {ip, IP}]) of
  97. {ok, Socket} ->
  98. Socket;
  99. {error, Reason} ->
  100. log:log(error,"[ CC ] can't listen on ~p: ~p~n", [Port, Reason]),
  101. open_listen_port(Rest, IP)
  102. end;
  103. open_listen_port([], _) ->
  104. abort;
  105. open_listen_port(Port, IP) ->
  106. open_listen_port([Port], IP).
  107. -include_lib("kernel/include/inet.hrl").
  108. first_ip() ->
  109. {ok, Hostname} = inet:gethostname(),
  110. {ok, HostEntry} = inet:gethostbyname(Hostname),
  111. erlang:hd(HostEntry#hostent.h_addr_list).