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

megalomania ricardocb48@REDACTED
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,
code_change/3]).

-define(NAME_SERVER, ?MODULE).
-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,
	ets:delete(Tab),
	ok.


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]),
	if
		BOOL == true -> io:format("~w its already login", [Nick]);
		true -> ets:insert(_State, {Nick,Pid}), io:format("Login succesful ~n")
	end,
	{reply, {login, Nick}, _State};


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


handle_cast(hi, State) -> 
	io:fwrite("\t\tHi!\~n"),
	{noreply, State} ;

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

	
handle_info({times_request, Pid, {N, M}}, State) ->
	io:fwrite("\t~w server handling info, request number ~w~n", [?MODULE,
State]),
	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
(server@REDACTED)1> c(servidor).
{ok,servidor}
(server@REDACTED)2> servidor:start_link().
{ok,<0.45.0>}
(server@REDACTED)3>       


*and then i typed this in another console:*
user@REDACTED)25> {servidor,server@REDACTED} ! {servidor,login,irene,777}.         
{servidor,login,irene,777}
(user@REDACTED)26> {servidor,server@REDACTED} ! {servidor,login,irene,777}.
{servidor,login,irene,777}
(user@REDACTED)27> {servidor,server@REDACTED} !
servidor:call(login,irene,6666).
** exception error: undefined function servidor:call/3
(user@REDACTED)28> {servidor,server@REDACTED} ! servidor:login(irene,6666).        
** exception exit: {noproc,{gen_server,call,[servidor,{login,irene,6666}]}}
     in function  gen_server:call/2
(user@REDACTED)29> {servidor,server@REDACTED} !
process_request(login,{irene,2321}).
** exception error: undefined shell command process_request/2
(user@REDACTED)30> {servidor,server@REDACTED} ! {hi}.                               
{hi}
(user@REDACTED)31> {servidor,server@REDACTED} ! {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