Quantcast

Help! with gen_server and a chat_server

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help! with gen_server and a chat_server

megalomania
This post has NOT been accepted by the mailing list yet.
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@ubuntuPC)1> c(servidor).
{ok,servidor}
(server@ubuntuPC)2> servidor:start_link().
{ok,<0.45.0>}
(server@ubuntuPC)3>      


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


almost or the most of instructions makes server_node crash :(

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