Skip to content

Instantly share code, notes, and snippets.

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