0

Im working on erlang for the first time. everytime i try to run the erlang process it gets stuck and does not take input. Im using erlide plugin in eclipse to test the erlang code.

CODE IS::

-module(message_router).

%% ====================================================================
%% API functions
%% ====================================================================
%%-compile(export_all).
-export([start/0]).
-export([stop/1]).
-export([send_chat_message/3]).
-export([route_messages/0]).



%% ====================================================================
%% Internal functions
%% ====================================================================
start() ->
    spawn(message_router, route_messages, []).

stop(RouterPid) ->
    RouterPid ! shutdown.

send_chat_message(RouterPid, Addressee, MessageBody) ->
    io:format("send_chat_msg FROM:: ~p TO:: ~p ~n", [RouterPid, Addressee]),
    RouterPid ! {send_chat_msg, Addressee, MessageBody}.

route_messages() ->

receive
    {send_chat_msg, Addressee, MessageBody} ->
        io:format("recv_chat_msg PID:: ~p ~n", [Addressee]),
        Addressee ! {recv_chat_msg, MessageBody},
        route_messages();

    {recv_chat_msg, MessageBody} ->
        io:format("Received: ~p~n", [MessageBody]);

    shutdown ->
        io:format("Shutting down ~n");

    Oops ->
        io:format("Warning! Received: ~p~n", [Oops]),
        route_messages()
end.

When i hit try to run the code like in the shell

Eshell V5.10.4
(nodename@pa)1> P1 = message_router:start().
<0.1308.0>
(nodename@pa)2> P2 = message_router:start().
<0.1373.0>
(nodename@pa)3> chat_client:send_message(P1, P2, "FIRST Msg").
Sending chat message from chat_client
send_chat_msg FROM:: <0.1308.0> TO:: <0.1373.0> 

Every thing i enter in the shell after this has on effect. Also could anyone explain how loops are handled in erlang and best practices.

[edit]

chat client code:

-module(chat_client).
-export([send_message/3]).
send_message(RouterPid, Addressee, MessageBody) ->
    io:format("Sending chat message from chat_client~n"),
    message_router:send_chat_message(RouterPid, Addressee, MessageBody).
Pascal
  • 13,977
  • 2
  • 24
  • 32
Prajun Adhikary
  • 637
  • 1
  • 6
  • 8
  • Try calling `message_router:send_message(P1, P2, "FIRST Msg").` – aggelgian Jan 12 '14 at 12:00
  • 1
    Have you looked into gen_servers? It's good practise for examples like this one, and will make your job a little easier http://www.erlang.org/doc/man/gen_server.html | http://learnyousomeerlang.com/clients-and-servers – danihodovic Jan 12 '14 at 13:38
  • Could you please to show chat_client:send_message code? – Odobenus Rosmarus Jan 13 '14 at 07:51
  • If you have no prior Erlang experience, or even functional programming, consider some reading to get up to speed. If books are a problem, there are great online guides like this one: http://learnyousomeerlang.com/content – Berzemus Jan 13 '14 at 08:39
  • Odobenus Rosmarus Here the code from chat_client -module(chat_client). -export([send_message/3]). send_message(RouterPid, Addressee, MessageBody) -> io:format("Sending chat message from chat_client~n"), message_router:send_chat_message(RouterPid, Addressee, MessageBody). – Prajun Adhikary Jan 13 '14 at 09:32
  • Berzemus i have been following the link you suggested. Its really thankful of you. – Prajun Adhikary Jan 13 '14 at 09:33
  • Seems that `route_messages()` call is missing in `{recv_chat_msg,...}` clause. Intentional? – shino Jan 17 '14 at 09:18

1 Answers1

0

Problem in module. Call this message_router:send_chat_message(P1, P2, "FIRST Msg")
Look at this example and compare with his:

Have module call observer it registers two process loop/0 and trans/0

-module(observer).
-export([observer/0, loop/0, trans/0]).

observer() ->
  register(loop, spawn(fun observer:loop/0)),
  register(trans, spawn(fun observer:trans/0)).

loop() ->
  receive
    {'PRINT', Msg} -> 
      io:format("print from ~p~n~p~n", [self(), Msg]), loop();
    {'EXIT', _FromPid} -> 
      io:format("Exit ~nFrom ~p", [_FromPid]), exit(self(), kill)
  end.

trans() ->
  receive
    {Command, Msg} -> io:format("transports from: ~p~n~p~n", [self(), Msg]),
                      loop ! {Command, Msg}
  end.

and have module send_messenger one function send_message that sends messages through trans:

-module(send_messenger).
-export([send_message/1]).

send_message({Command, Msg}) ->
  trans ! {Command, Msg},
  ok.

I hope this has helped you!

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103