Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 004ae965d6
Fetching contributors…

Cannot retrieve contributors at this time

file 47 lines (40 sloc) 1.187 kb
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
-module(egitd_server).
-export([start_link/0, init/1]).

start_link() ->
  proc_lib:start_link(?MODULE, init, [self()]).

init(Parent) ->
  ets:new(db, [set, named_table]),
  read_conf(),
  init_log(),
  log:write("start", ["ok"]),
  LSock = try_listen(10),
  proc_lib:init_ack(Parent, {ok, self()}),
  loop(LSock).
  
read_conf() ->
  {ok, Conf} = application:get_env(conf),
  error_logger:info_msg("Using conf file ~p~n", [Conf]),
  conf:read_conf(Conf).
  
init_log() ->
  init_log(application:get_env(log)).
init_log({ok, Log}) ->
  log:init_log(Log);
init_log(undefined) ->
  ok.
  
try_listen(0) ->
  error_logger:info_msg("Could not listen on port 9418~n");
try_listen(Times) ->
  Res = gen_tcp:listen(9418, [binary, {packet, 0}, {active, false}]),
  case Res of
    {ok, LSock} ->
      error_logger:info_msg("Listening on port 9418~n"),
      LSock;
    {error, Reason} ->
      error_logger:info_msg("Could not listen on port 9418: ~p~n", [Reason]),
      timer:sleep(5000),
      try_listen(Times - 1)
  end.
    
loop(LSock) ->
  {ok, Sock} = gen_tcp:accept(LSock),
  {ok, Pid} = egitd_connection:start_link(Sock),
  gen_tcp:controlling_process(Sock, Pid),
  loop(LSock).
  
Something went wrong with that request. Please try again.