Added some basic echo functionality

This commit is contained in:
Jonatan Pålsson 2011-02-16 19:49:43 +01:00
parent 63170fd0de
commit ec78283a46
17 changed files with 75 additions and 448 deletions

View file

@ -1,5 +1,7 @@
-module(ggs_dispatcher).
-behaviour(gen_server).
%% API Exports
-export([start_link/1, stop/1]).
@ -7,6 +9,8 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
-define(SERVER, ?MODULE).
%% @doc This module is the entry-point for clients connecting to GGS. This is
%% the module responsible for:
@ -20,19 +24,21 @@
%% Port = Integer
%% Pid = #<Pid>
start_link(Port) ->
not_implemented.
gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
%% @doc Stops the dispatcher with the specified reason.
%% @spec stop(Reason) -> ok.
%% Reason = String
stop(Reason) -> not_implemented.
stop(_Reason) -> not_implemented.
%% gen_server callbacks
%% @doc Initiate the dispatcher. This is called from gen_server
init([Port]) ->
{ok, ok}.
{ok, LSock} = gen_tcp:listen(Port, [{active, true},
{reuseaddr, true}]),
{ok, LSock, 0}.
handle_call(_Message, _From, State) ->
{noreply, State}.
@ -40,8 +46,24 @@ handle_call(_Message, _From, State) ->
handle_cast(_Message, State) ->
{noreply, State}.
handle_info({tcp, _Socket, RawData}, State) ->
io:format("Got connect request!~n"),
{noreply, State};
handle_info({tcp_closed, Socket}, State) ->
gen_tcp:close(Socket),
{stop, "Client closed socket", State};
%% @doc This is our function for accepting connections. When a client connects,
%% it will immediately time out due to timing settings set in init and here,
%% and when it does, we accept the connection.
handle_info(timeout, LSock) ->
{ok, Sock} = gen_tcp:accept(LSock),
spawn(ggs_player, start_link, [Sock]),
{noreply, LSock, 0}.
terminate(normal, _State) ->
ok.
code_change(_OldVsn, State, Extra) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.