Created
November 1, 2011 12:13
-
-
Save ScrapCodes/1330399 to your computer and use it in GitHub Desktop.
p2p erlang messanger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(erlMessage). | |
-export([start/1,sendMessage/2,msgReceive/0]). | |
% msgReceive blocks on the message queue and waits for a message! | |
% and prints the message as soon as it arrives and again tail recurses! | |
msgReceive() -> | |
receive | |
{Message,Message_from} -> io:format("~p said:~p~n",[Message_from,Message]); | |
Message ->io:format("~p ~n",[Message]) | |
end, | |
msgReceive(). %tail recursion | |
%sendMessage simply passes your message to the node specified and prints an acknowledgement. | |
sendMessage(Message,{Name,Node}) -> | |
{Name,Node}!{Message,node()}, | |
io:format("Message: ~p sent to ~p ~n",[Message,{Name,Node}]). | |
%start the seperate process for waiting for incoming messages! | |
start(User_name) -> | |
register(User_name,spawn(erlMessage,msgReceive,[])). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment