Skip to content

Commit

Permalink
added reload support
Browse files Browse the repository at this point in the history
  • Loading branch information
dnet committed Dec 27, 2010
1 parent 31833e5 commit 65eff1a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
27 changes: 26 additions & 1 deletion admin.erl
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
-module(admin).
-export([ircmain/1, ircproc/1, lsmod/2, rmmod/3, insmod/3]).
-export([ircmain/1, ircproc/1, lsmod/2, rmmod/3, insmod/3, reload/3, reload/2]).
-include("nick.hrl").

ircmain(Contact) ->
Pid = spawn(?MODULE, ircproc, [Contact]),
Contact ! {subscribe, Pid},
Pid.

reload(Contact, Pid) ->
Pid ! reloaded,
ircproc(Contact).

ircproc(Contact) ->
receive
quit -> quit;
Expand All @@ -23,6 +27,8 @@ ircproc(Contact) ->
{ident, Pid} ->
Pid ! {ident, "admin"},
ircproc(Contact);
{reload, Pid} ->
?MODULE:reload(Contact, Pid);
_ -> ircproc(Contact)
end.

Expand Down Expand Up @@ -100,6 +106,13 @@ process_privmsg("-insmod", Remainder, ReplyTo, Prefix, Contact) ->
false ->
noreply
end;
process_privmsg("-reload", Remainder, ReplyTo, Prefix, Contact) ->
case admin(Prefix) of
true ->
spawn(?MODULE, reload, [ReplyTo, Remainder, Contact]), noreply;
false ->
noreply
end;
process_privmsg("-load", [Module | _], ReplyTo, Prefix, _Contact) ->
case admin(Prefix) of
true ->
Expand Down Expand Up @@ -166,6 +179,18 @@ insmod(To, [ModName | Params], Contact) ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :(timeout)"}
end.

reload(To, [FirstParam | _], Contact) ->
N = list_to_integer(FirstParam),
Contact ! {getmods, self()},
receive
{mods, L} ->
lists:nth(N, L) ! {reload, self()},
Resp = receive reloaded -> "module reloaded successfully" after 2500 -> "(timeout)" end,
Contact ! {raw, "PRIVMSG " ++ To ++ " :" ++ Resp}
after 1500 ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :(timeout)"}
end.

strip_crlf(Str) ->
string:strip(string:strip(Str, right, $\n), right, $\r).

Expand Down
14 changes: 13 additions & 1 deletion ping.erl
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
-module(ping).
-export([ircmain/1, ircproc/1]).
-export([ircmain/1, ircproc/1, reload/2, reload_inner/4]).

ircmain(Contact) ->
Pid = spawn(?MODULE, ircproc, [Contact]),
Contact ! {subscribe, Pid},
Pid.

reload_inner(Contact, ServerName, Reconnect, Pid) ->
Pid ! reloaded,
inner_ircproc(Contact, ServerName, Reconnect).

reload(Contact, Pid) ->
Pid ! reloaded,
ircproc(Contact).

ircproc(Contact) ->
receive
quit -> quit;
Expand All @@ -16,6 +24,8 @@ ircproc(Contact) ->
{ident, Pid} ->
Pid ! {ident, "ping"},
ircproc(Contact);
{reload, Pid} ->
?MODULE:reload(Contact, Pid);
_ -> ircproc(Contact)
end.

Expand All @@ -28,6 +38,8 @@ inner_ircproc(Contact, ServerName, Reconnect) ->
{ident, Pid} ->
Pid ! {ident, "ping"},
inner_ircproc(Contact, ServerName, Reconnect);
{reload, Pid} ->
?MODULE:reload_inner(Contact, ServerName, Reconnect, Pid);
_ -> inner_ircproc(Contact, ServerName)
after 120000 ->
case Reconnect of
Expand Down
8 changes: 7 additions & 1 deletion pong.erl
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
-module(pong).
-export([ircmain/1, ircproc/1]).
-export([ircmain/1, ircproc/1, reload/2]).

ircmain(Contact) ->
Pid = spawn(?MODULE, ircproc, [Contact]),
Contact ! {subscribe, Pid},
Pid.

reload(Contact, Pid) ->
Pid ! reloaded,
ircproc(Contact).

ircproc(Contact) ->
receive
quit -> quit;
Expand All @@ -15,5 +19,7 @@ ircproc(Contact) ->
{ident, Pid} ->
Pid ! {ident, "pong"},
ircproc(Contact);
{reload, Pid} ->
?MODULE:reload(Contact, Pid);
_ -> ircproc(Contact)
end.

0 comments on commit 65eff1a

Please sign in to comment.