From 4ee8fd87719428159c1a059f8aa13ecb2d18b2b8 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 2 Nov 2015 23:55:00 +0100 Subject: Add gen_server stub --- src/tda.erl | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/tda.erl') diff --git a/src/tda.erl b/src/tda.erl index 847c421..7a7b04c 100644 --- a/src/tda.erl +++ b/src/tda.erl @@ -1,4 +1,10 @@ -module(tda). +-behaviour(gen_server). +-define (SERVER, ?MODULE). + +-export([start_link/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). -export([start/0, client/1, stop/0, decode/1]). -export([send/1, ext/0, co/0, snapshot/1, monitor/1, button/1, dial/2]). @@ -6,6 +12,33 @@ -include("config.hrl"). -include("opcodes.hrl"). +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [?HOST, ?PORT], []). + +init(Args) -> + counter:start(), + [Host, Port] = Args, + gen_tcp:connect(Host, Port, [binary, {packet, 2}], ?TIMEOUT). + +handle_call(_Request, _From, State) -> + {reply, ok, State}. + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(_Info, State) -> + {noreply, State}. + +terminate(_Reason, State) -> + counter:stop(), + gen_tcp:close(State), + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + start() -> register(?MODULE, spawn(?MODULE, client, [{dial, ?HOST, ?PORT}])). -- cgit v1.2.3