% otherplaces.erl -module(otherplaces). -author('Alan G. Labouseur'). -define(else, true). % -- This is to make the if statements (somewhat) readable. %-------- % Public %-------- -export([startP1/0, startP2/0, p1/0, p2/0, test/2]). startP1() -> io:fwrite("~nstartP1() [pid ~w] has started on node ~w.~n~n", [self(), node()]), P1pid = spawn(otherplaces, p1, []), % We want to publish this process in Erlang's Process Dictionary. % Before we do that, we need to un-register it if it's already been registered. SomePlace = whereis(place1), if (SomePlace /= undefined) -> % "not undefined" = "is defined" (This is horrible, I know.) unregister(place1); ?else -> % The proccess was NOT already published/registered, so we don't really want to do anything here. true % This is dumb, but I couldn't think of a better "no-op". end, % Publish this process in Erlang's Process Dictionary. register(place1, P1pid). startP2() -> io:fwrite("~nstartP2() [pid ~w] has started on node ~w.~n~n", [self(), node()]), P2pid = spawn(otherplaces, p2, []), % We want to publish this process in Erlang's Process Dictionary. % Before we do that, we need to un-register it if it's already been registered. SomePlace = whereis(place2), if (SomePlace /= undefined) -> % "not undefined" = "is defined" (This is horrible, I know.) unregister(place2); ?else -> % The proccess was NOT already published/registered, so we don't really want to do anything here. true % This is dumb, but I couldn't think of a better "no-op". end, % Publish this process in Erlang's Process Dictionary. register(place2, P2pid). % This is spawned from startP1(). p1() -> receive {From, Request} -> io:fwrite("p1() [pid ~w] received request [~w] from pid ~w.~n", [self(), Request, From]), Response = io_lib:format("This text is p1() [pid ~w's] on node [~w]'s response to pid ~w.~n", [self(), node(), From]), From ! Response, p1() end. % This is spawned from startP2(). p2() -> receive {From, Request} -> io:fwrite("p2() [pid ~w] received request [~w] from pid ~w.~n", [self(), Request, From]), Response = io_lib:format("This text is p2() [pid ~w's] on node [~w]'s response to pid ~w.~n", [self(), node(), From]), From ! Response, p2() end. test(Place, Node) -> io:fwrite("Testing ~w on node ~w.~n", [Place, Node]), {Place, Node} ! {self(), remoteRequest}, receive Response -> io:fwrite("~s~n", [Response]) % Wait for ANY response. end.