Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(mappingserver).
- -behaviour(gen_server).
- -export([start/0]).
- %% gen_server callbacks
- -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3]).
- -compile(export_all).
- start() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
- stop() -> gen_server:call(?MODULE, stop).
- lookup_port(Host) -> gen_server:call(?MODULE, {port, Host}).
- lookup_command(Host) -> gen_server:call(?MODULE, {command, Host}).
- refresh() -> gen_server:call(?MODULE, {refresh}).
- refresh_file() ->
- {ok,List} = file:consult("mapping.dat"),
- dict:from_list([{Host,{Port,Command}} || {Host,Port,Command} <- List]).
- init([]) ->
- State = refresh_file(),
- timer:apply_interval(60*1000,?MODULE,refresh,[]),
- {ok, State}.
- handle_call({port,Host}, _From, State) ->
- {Reply,_} = dict:fetch(Host,State),
- {reply, Reply, State};
- handle_call({command,Host}, _From, State) ->
- {_,Reply} = dict:fetch(Host,State),
- {reply, Reply, State};
- handle_call({refresh}, _From, State) ->
- NewState = refresh_file(),
- {reply, ok, NewState};
- handle_call(stop, _From, State) ->
- {stop, normal, stopped, State}.
- handle_cast(_Msg, State) -> {noreply, State}.
- handle_info(_Info, State) -> {noreply, State}.
- terminate(_Reason, _State) -> ok.
- code_change(_OldVsn, State, Extra) -> {ok, State}.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement