Skip to content

Instantly share code, notes, and snippets.

@featheredtoast
Created October 30, 2012 21:10
Show Gist options
  • Save featheredtoast/3983087 to your computer and use it in GitHub Desktop.
Save featheredtoast/3983087 to your computer and use it in GitHub Desktop.
Hackish tcp server from erlang OTP in action
-module(tr_server).
-behavior(gen_server).
%%API
-export([start_link/1, start_link/0, get_count/0, stop/0]).
%% Gen Server
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-define(DEFAULT_PORT, 1055).
-record(state,{port, lsock, request_count=0}).
%%API
%%
start_link(Port) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
start_link() ->
start_link(?DEFAULT_PORT).
get_count() ->
gen_server:call(?SERVER, get_count).
stop() ->
gen_server:cast(?SERVER, stop).
%% Gen Server
%%
init([Port]) ->
{ok, LSock} = gen_tcp:listen(Port, [{active, true}]),
{ok, #state{port = Port, lsock = LSock}, 0}.
handle_call(get_count, _From, State) ->
{reply, {ok, State#state.request_count}, State}.
handle_cast(stop, State) ->
{stop, ok, State}.
handle_info({tcp, Socket, RawData}, State) ->
RequestCount = State#state.request_count,
try
MFA = re:replace(RawData, "\r\n$","",[{return, list}]),
{match, [M, F, A]} = re:run(MFA, "(.*):(.*)\s*\\((.*)\s*\\)\s*.\s*$",
[{capture, [1,2,3], list}, ungreedy]),
Result = apply(list_to_atom(M), list_to_atom(F), args_to_terms(A)),
gen_tcp:send(Socket, io_lib:fwrite("~p~n",[Result]))
catch
_C:E ->
gen_tcp:send(Socket, io_lib:fwrite("~p~n", [E]))
end,
{noreply, State#state{request_count = RequestCount + 1}};
handle_info(timeout, #state{lsock = LSock} = State) ->
{ok, _Sock} = gen_tcp:accept(LSock),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%internal
args_to_terms([])->
[];
args_to_terms(RawArgs) ->
{ok, Toks, _Line} = erl_scan:string("[" ++ RawArgs ++ "]. ", 1),
{ok, Args} = erl_parse:parse_term(Toks),
Args.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment