%%%------------------------------------------------------------------- %%% @author Heinz Nikolaus Gies %%% @copyright (C) 2012, Heinz Nikolaus Gies %%% @doc %%% %%% @end %%% Created : 9 Oct 2012 by Heinz Nikolaus Gies %%%------------------------------------------------------------------- -module(libchunter_server). -behaviour(gen_server). %% API -export([start_link/0, call/3, cast/3, call/4]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, console/4]). -define(SERVER, ?MODULE). -record(state, {}). %%%=================================================================== %%% API %%%=================================================================== %%-------------------------------------------------------------------- %% @doc %% Starts the server %% %% @spec start_link() -> {ok, Pid} | ignore | {error, Error} %% @end %%-------------------------------------------------------------------- start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). %%%=================================================================== %%% gen_server callbacks %%%=================================================================== %%-------------------------------------------------------------------- %% @private %% @doc %% Initializes the server %% %% @spec init(Args) -> {ok, State} | %% {ok, State, Timeout} | %% ignore | %% {stop, Reason} %% @end %%-------------------------------------------------------------------- init([]) -> {ok, #state{}}. -spec call(Server::inet:ip_address() | inet:hostname(), Port::inet:port_number(), Command::fifo:chunter_message()) -> term(). call(Server, Port, Command) -> gen_server:call(?SERVER, {call, Server, Port, Command}). call(Server, Port, Command, Timeout) -> gen_server:call(?SERVER, {call, Server, Port, Command, Timeout}, %% We add 1000 to make sure the call returns w/o %% a gen_server timeout Timeout + 1000). -spec cast(Server::inet:ip_address() | inet:hostname(), Port::inet:port_number(), Command::fifo:chunter_message()) -> ok. cast(Server, Port, Command) -> gen_server:cast(?SERVER, {cast, Server, Port, Command}). console(Server, Port, VM, Proc) -> gen_server:call(?SERVER, {console, Server, Port, VM, Proc}). %%-------------------------------------------------------------------- %% @private %% @doc %% Handling call messages %% %% @spec handle_call(Request, From, State) -> %% {reply, Reply, State} | %% {reply, Reply, State, Timeout} | %% {noreply, State} | %% {noreply, State, Timeout} | %% {stop, Reason, Reply, State} | %% {stop, Reason, State} %% @end %%-------------------------------------------------------------------- handle_call({call, Server, Port, Command, Timeout}, Form, State) -> libchunter_fsm:call(Server, Port, Command, Form, Timeout), {noreply, State}; handle_call({call, Server, Port, Command}, Form, State) -> libchunter_fsm:call(Server, Port, Command, Form), {noreply, State}; handle_call({console, Server, Port, VM, Proc}, _From, State) -> Cons = libchunter_console_server:console(Server, Port, VM, Proc), {reply, Cons, State}; handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}. %%-------------------------------------------------------------------- %% @private %% @doc %% Handling cast messages %% %% @spec handle_cast(Msg, State) -> {noreply, State} | %% {noreply, State, Timeout} | %% {stop, Reason, State} %% @end %%-------------------------------------------------------------------- handle_cast({cast, Server, Port, Command}, State) -> libchunter_fsm:cast(Server, Port, Command), {noreply, State}; handle_cast(_Msg, State) -> {noreply, State}. %%-------------------------------------------------------------------- %% @private %% @doc %% Handling all non call/cast messages %% %% @spec handle_info(Info, State) -> {noreply, State} | %% {noreply, State, Timeout} | %% {stop, Reason, State} %% @end %%-------------------------------------------------------------------- handle_info(_Info, State) -> {noreply, State}. %%-------------------------------------------------------------------- %% @private %% @doc %% This function is called by a gen_server when it is about to %% terminate. It should be the opposite of Module:init/1 and do any %% necessary cleaning up. When it returns, the gen_server terminates %% with Reason. The return value is ignored. %% %% @spec terminate(Reason, State) -> void() %% @end %%-------------------------------------------------------------------- terminate(_Reason, _State) -> ok. %%-------------------------------------------------------------------- %% @private %% @doc %% Convert process state when code is changed %% %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState} %% @end %%-------------------------------------------------------------------- code_change(_OldVsn, State, _Extra) -> {ok, State}. %%%=================================================================== %%% Internal functions %%%===================================================================