Skip to content

Instantly share code, notes, and snippets.

@pmurias
Created December 5, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmurias/b80128944e9a49fb97161c170ed93961 to your computer and use it in GitHub Desktop.
Save pmurias/b80128944e9a49fb97161c170ed93961 to your computer and use it in GitHub Desktop.
random erlang code from the internet
loop(Users, N) ->
receive
{connect, Pid, User, Password} ->
io:format("connection request from:~p ~p ~p~n",
[Pid, User, Password]),
case member({User, Password}, Users) of
true ->
Max = max_connections(),
if
N > Max ->
Pid ! {ftp_server,
{error, too_many_connections}},
loop(Users, N);
true ->
New = spawn_link(?MODULE, handler, [Pid]),
Pid ! {ftp_server, {ok, New}},
loop(Users, N + 1)
end;
false ->
Pid ! {ftp_server, {error, rejected}},
loop(Users, N)
end;
{'EXIT', Pid} ->
io:format("Handler ~p died~n", [Pid]),
loop(Users, lists:max(N-1, 0));
Any ->
io:format("received:~p~n",[Any]),
loop(Users, N)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment