From 359513452f4078cf6348602044567d7fcdeaa832 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 24 Oct 2015 02:37:58 +0200 Subject: Reply status --- tda.erl | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/tda.erl b/tda.erl index b88dd32..f4e4f8b 100644 --- a/tda.erl +++ b/tda.erl @@ -9,6 +9,8 @@ -include("acse.hrl"). -include("rose.hrl"). +-define(TIMEOUT, 300000). + % A-ASSOCIATE Request % 60 23 AARQ-apdu % 80 02 07 80 protocol-version { version1 } @@ -89,7 +91,6 @@ start_client() -> client() -> Host = "192.168.240.20", - %Host = "localhost", case gen_tcp:connect(Host, 33333, [binary, {active, true}, {packet, 2}]) of {ok, Sock} -> {ok, Hello} = associate_request(), @@ -106,15 +107,40 @@ loop(Sock) -> io:format("loop ~p~n", [Sock]), receive {tcp, Sock, Msg} -> - decode(Msg), + decode(Sock, Msg), loop(Sock); - {tcp_closed, Sock} -> - io:format(">>> closed", []), - Sock - after 5000 -> + {tcp_closed, _} -> + io:format(">>> closed", []) + after ?TIMEOUT -> + io:format("timeout~n"), stop(Sock) - end, - io:format("loop end~n"). + end. + +decode(Sock, Msg) -> + io:format(">>> ~p~n", [Msg]), + case rose:decode('ROS', Msg) of + {ok, Rose} -> + rose_handler(Sock, Rose), + io:format("ROSE> ~p~n", [Rose]); + {error, _} -> + case acse:decode('ACSE-apdu', Msg) of + {ok, Acse} -> + io:format("ACSE> ~p~n", [Acse]) + end + end. + +rose_handler(Sock, {invoke, Rose}) -> + gen_tcp:send(Sock, invoke_handler(Rose)). + +invoke_handler(#'Invoke'{invokeId = {present, Id}, opcode = {local, Op}}) -> + case Op of + 211 -> + RR = #'ReturnResult_result'{opcode = {local, Op}, + result = <<5,0>>}, + R = #'ReturnResult'{invokeId = {present, Id}, result = RR}, + case rose:encode('ROS', {returnResult, R}) of + {ok, Invoke} -> Invoke; + {error, Reason} -> Reason + end + end. -decode(Msg) -> - io:format(">>> ~p~n", [Msg]). -- cgit v1.2.3