[erlang-questions] Help! with gen_server and a chat_server

megalomania <>
Thu Apr 12 01:03:55 CEST 2012

*Hi all,
Im newbie on Erlang and trying lear to use gen_server but i begin to
understan that maybe im wrong.

Im just trying to making a chat_server with gen_serve, so the idea is a
client on an another node could try to login in the chat_server who will be
in another node.

here is my code:

-module(servidor) .
-behavior(gen_server) . 

% API functions
-export([start_link/0, hi/0, login/2, logout/1, times/2, stop/0]).

% Callback functions
-export([init/1, handle_call/3, handle_info/2, terminate/2, handle_cast/2,

-define(LOGUEADOS, logeados).

start_link() ->
	gen_server:start_link({local, ?NAME_SERVER}, ?MODULE, [], []).%Se registra
el proceso con el nombre del modulo(servidor)

hi() ->
	gen_server:cast(?MODULE, hi) .

stop() -> 
	gen_server:cast(?MODULE, stop).

login(Nick,Pid) ->
	gen_server:call(?MODULE, {login, Nick, Pid}).

logout(Nick) ->
	gen_server:call(?MODULE, {logout, Nick}).

times(N, M) ->
	?MODULE ! {times_request, self(), {N, M}}.

%% Callback functions implementation %%

init([]) ->
	{ok, ets:new(?LOGUEADOS, [set])}.

terminate(_Reason, _State) ->
	{_, Tab} = _State,

handle_call({login, Nick, Pid}, _From, _State) ->
	%% En el login habra que crear una nodo del que haga el login // Para
probarlo, miro simplemente si existe en la BD.
	io:format("Loggin.. y state=~w: ~n", [_State]),
	BOOL = ets:member(_State, Nick),
	io:format("The user is online? = ~w ~n", [BOOL]),
		BOOL == true -> io:format("~w its already login", [Nick]);
		true -> ets:insert(_State, {Nick,Pid}), io:format("Login succesful ~n")
	{reply, {login, Nick}, _State};

handle_call({logout, Nick}, _From, _State) ->
	io:format("Logout...~n", []),
	ets:delete(_State, Nick).

handle_cast(hi, State) -> 
	{noreply, State} ;

handle_cast(stop, _State) ->
	io:format("Stopped~n", []),
	{_, Tab} = _State,
	{stop, normal, _State} .

handle_info({times_request, Pid, {N, M}}, State) ->
	io:fwrite("\t~w server handling info, request number ~w~n", [?MODULE,
	Pid ! {times_response, N*M},
	{noreply, State} .

code_change(_OldVsn, _State, _Extra) ->
	{ok, _State}.

*and then i typed this in one console:*

>erl -sname server
()1> c(servidor).
()2> servidor:start_link().

*and then i typed this in another console:*
)25> {} ! {servidor,login,irene,777}.         
()26> {} ! {servidor,login,irene,777}.
()27> {} !
** exception error: undefined function servidor:call/3
()28> {} ! servidor:login(irene,6666).        
** exception exit: {noproc,{gen_server,call,[servidor,{login,irene,6666}]}}
     in function  gen_server:call/2
()29> {} !
** exception error: undefined shell command process_request/2
()30> {} ! {hi}.                               
()31> {} ! {async,hi}.

*almost or the most of instructions makes server_node crash :(*

My question is, what im doing wrong? It is the correct way?

View this message in context: http://erlang.2086793.n4.nabble.com/Help-with-gen-server-and-a-chat-server-tp4550462p4550462.html
Sent from the Erlang Questions mailing list archive at Nabble.com.

More information about the erlang-questions mailing list