id_generator.erl 1019 B

1234567891011121314151617181920
  1. -module(id_generator).
  2. -behaviour(gen_server).
  3. -export([start_link/0]).
  4. -export([get_id/0,get_id2/0]).
  5. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  6. -define(SERVER, ?MODULE).
  7. -record(state, {lastid = 1000000, robotid = 1500000}).
  8. get_id() -> gen_server:call(?MODULE, get_id).
  9. get_id2() -> gen_server:call(?MODULE, get_id2).
  10. start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  11. init([]) -> {ok, #state{lastid=wf:config(nsx_idgen,game_pool,1000000)}}.
  12. handle_call(get_id, _From, #state{lastid = LID} = State) -> Reply = LID + 1, {reply, Reply, State#state{lastid = Reply}};
  13. handle_call(get_id2, _From, #state{robotid = RID} = State) -> Reply = RID + 1, {reply, Reply, State#state{robotid = Reply}};
  14. handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}.
  15. handle_cast(_Msg, State) -> {noreply, State}.
  16. handle_info(_Info, State) -> {noreply, State}.
  17. terminate(_Reason, _State) -> ok.
  18. code_change(_OldVsn, State, _Extra) -> {ok, State}.