Skip to content

Instantly share code, notes, and snippets.

@dc0d
Created September 27, 2015 21:39
Show Gist options
  • Save dc0d/2f204983b2933dace8b4 to your computer and use it in GitHub Desktop.
Save dc0d/2f204983b2933dace8b4 to your computer and use it in GitHub Desktop.
-module(server3).
-export([start/2, rpc/2, swap_code/2]).
start(Name, Mod) ->
register(Name,
spawn(fun() -> loop(Name,Mod,Mod:init()) end)).
swap_code(Name, Mod) -> rpc(Name, {swap_code, Mod}).
rpc(Name, Request) ->
Name ! {self(), Request},
receive
{Name, Response} -> Response
end.
loop(Name, Mod, OldState) ->
receive
{From, {swap_code, NewCallBackMod}} ->
From ! {Name, ack},
loop(Name, NewCallBackMod, OldState);
{From, Request} ->
{Response, NewState} = Mod:handle(Request, OldState),
From ! {Name, Response},
loop(Name, Mod, NewState)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment