From d837c94dcd08cacee427e59ff11a7e07d6d9e530 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 30 Oct 2015 16:09:55 +0100 Subject: Go parallel --- src/tda.erl | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/tda.erl') diff --git a/src/tda.erl b/src/tda.erl index 3305f0c..7f3eba7 100644 --- a/src/tda.erl +++ b/src/tda.erl @@ -1,7 +1,7 @@ -module(tda). --export([start/0, client/1, stop/0]). --export([ext/0, co/0, snapshot/1]). +-export([start/0, client/1, stop/0, decode/1]). +-export([send/1, ext/0, co/0, snapshot/1]). -include("config.hrl"). -include("opcodes.hrl"). @@ -19,14 +19,15 @@ ext() -> co() -> tdaPid ! rose:invoke(?ESCAPE, escape:lines(co)). +send(Reply) -> + tdaPid ! Reply. + snapshot(Device) -> tdaPid ! rose:invoke(?SNAPSHOT, snapshot:encode(Device)). client({dial, Host, Port}) -> io:format("Dial ~p:~p~n", [Host, Port]), - Conn = gen_tcp:connect(Host, Port, - [binary, {active, once}, {packet, 2}], - ?CONNTIMEOUT), + Conn = gen_tcp:connect(Host, Port, [binary, {packet, 2}], ?TIMEOUT), counter:start(), client(Conn); @@ -40,18 +41,14 @@ client({error, Reason}) -> {error, Reason}. loop(Sock) -> - inet:setopts(Sock, [{active, once}]), + %inet:setopts(Sock, [{active, once}]), receive {ok, Reply} -> - %io:format("Send ~p~n", [Reply]), - gen_tcp:send(Sock, Reply); + io:format("Send ~p~n", [Reply]), + spawn(gen_tcp, send, [Sock, Reply]); {tcp, Sock, Data} -> - %io:format("Got ~p~n", [Data]), - case decode(Data) of - {ok, Reply} -> tdaPid ! {ok, Reply}; - ok -> ok; - error -> exit(error) - end; + io:format("Recv ~p~n", [Data]), + spawn(?MODULE, decode, [Data]); {tcp_closed, _} -> io:format("Connection closed~n", []), exit(closed) -- cgit v1.2.3