Skip to content

Instantly share code, notes, and snippets.

@shey
Created July 2, 2009 06:39
Show Gist options
  • Save shey/139295 to your computer and use it in GitHub Desktop.
Save shey/139295 to your computer and use it in GitHub Desktop.
-module(talk).
-compile(export_all).
%% call talk:start() to start the program
start() ->
Node = chomp(io:get_line('Which node do you want to talk to?')),
%%Registered process where all the magic happens:
register(me, spawn(?MODULE, receiving, [])),
register(them, spawn(?MODULE, sending, [list_to_atom(Node)])),
loop().
chomp(Data) ->
string:strip(Data, both, $\n).
loop() ->
Message = chomp(io:get_line('>>_')),
them ! {send, Message},
loop().
sending(ReceivingNode) ->
receive
{send, Message} ->
rpc:call(ReceivingNode, talk, talk, [Message]),
sending(ReceivingNode)
end.
talk(Message) ->
me ! {message, Message}.
receiving() ->
receive
{message, Message} ->
io:format("<<~s~n", [Message]),
receiving()
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment