Created
October 29, 2008 19:45
-
-
Save jasonroelofs/20798 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(echo_server). | |
-export([handle_event/3, handle_sync_event/4, init/1, | |
code_change/4, handle_info/3, | |
terminate/3]). | |
-export([start/0, start_fsm/1, loop/2]). | |
-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]). | |
% echo_server specific code | |
start() -> | |
{ok, Socket} = gen_tcp:listen(7000, ?TCP_OPTIONS), | |
accept(Socket). | |
accept(LSocket) -> | |
{ok, Socket} = gen_tcp:accept(LSocket), | |
io:format("New connection incoming, start new FSM ~p~n", [Socket]), | |
proc_lib:spawn(?MODULE, start_fsm, [Socket]), | |
accept(LSocket). | |
start_fsm(Socket) -> | |
io:format("We gots connection: ~p~n", [Socket]), | |
gen_fsm:enter_loop(?MODULE, [], loop, [Socket]). | |
loop(Event, Socket) -> | |
io:format("In loop event ~p ~p~n", [Event, Socket]), | |
case gen_tcp:recv(Socket, 0) of | |
{ok, Data} -> | |
gen_tcp:send(Socket, Data), | |
{next_state, loop, Socket}; | |
{error, closed} -> | |
{stop, connection_closed, Socket} | |
end. | |
%% | |
%% Gen FSM Callbacks | |
%% | |
init(Socket) -> | |
io:format("Init! Socket is ~p~n", [Socket]), | |
{ok, loop, Socket}. | |
handle_event(_Event, StateName, StateData) -> | |
io:format("handle_event with ~p, ~p, ~p~n", [_Event, StateName, StateData]), | |
{next_state, StateName, StateData}. | |
handle_sync_event(_Event, _From, StateName, StateData) -> | |
io:format("handle_sync_event with ~p, ~p, ~p~n", [_Event, StateName, StateData]), | |
{next_state, StateName, StateData}. | |
handle_info(_Info, StateName, StateData) -> | |
io:format("handle_info with ~p, ~p, ~p~n", [_Info, StateName, StateData]), | |
{next_state, StateName, StateData}. | |
terminate(Reason, StateName, VideoServer) -> | |
io:format("terminate with ~p, ~p, ~p~n", [Reason, StateName, VideoServer]), | |
{terminated, Reason}. | |
code_change(_OldVsn, StateName, StateData, _Extra) -> | |
{ok, StateName, StateData}. | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
On run I get: | |
1> c(echo_server). | |
{ok,echo_server} | |
2> echo_server:start(). | |
New connection incoming, start new FSM #Port<0.135> | |
We gots connection: #Port<0.135> | |
And that's it. Nothing about the loop 'state' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment