diff options
author | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-06-11 10:56:48 -0700 |
---|---|---|
committer | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-06-11 10:56:48 -0700 |
commit | 6e797e454cf174ee5336fd43a4a39b6c19a59828 (patch) | |
tree | 5413985c4d50a4cbc8d7f7913ad7aaea171cceba /components/dlink_tcp | |
parent | bb6e4fa991dd5240864122f8343adf66e9e08e0c (diff) | |
download | rvi_core-6e797e454cf174ee5336fd43a4a39b6c19a59828.tar.gz |
Moved json extractor to rvi_common
Diffstat (limited to 'components/dlink_tcp')
-rw-r--r-- | components/dlink_tcp/src/connection.erl | 134 |
1 files changed, 3 insertions, 131 deletions
diff --git a/components/dlink_tcp/src/connection.erl b/components/dlink_tcp/src/connection.erl index 06a648d..daa5835 100644 --- a/components/dlink_tcp/src/connection.erl +++ b/components/dlink_tcp/src/connection.erl @@ -19,6 +19,7 @@ -behaviour(gen_server). -include_lib("lager/include/log.hrl"). + %% API %% gen_server callbacks @@ -36,13 +37,6 @@ -define(SERVER, ?MODULE). --record(pst, { - buffer = [], - balance = start, - in_string = false, - escaped = false - }). - -record(st, { ip = {0,0,0,0}, port = 0, @@ -146,7 +140,7 @@ init({IP, Port, Sock, Mod, Fun, Arg}) -> mod = Mod, func = Fun, args = Arg, - pst = #pst{} + pst = undefined }}. @@ -236,7 +230,7 @@ handle_info({tcp, Sock, Data}, ?debug("~p:handle_info(data): Data: ~p", [ ?MODULE, Data]), ?debug("~p:handle_info(data): From: ~p:~p ", [ ?MODULE, IP, Port]), - case extract_json(Data, PST) of + case rvi_common:extract_json(Data, PST) of { [], NPST } -> ?debug("~p:handle_info(data incomplete)", [ ?MODULE]), inet:setopts(Sock, [{active, once}]), @@ -313,125 +307,3 @@ code_change(_OldVsn, State, _Extra) -> %%%=================================================================== %%% Internal functions %%%=================================================================== - - -count_brackets([], - #pst { - buffer = [], - balance = start } = PSt) -> - { incomplete, PSt#pst {}}; - -count_brackets([], - #pst { - buffer = Buffer, - balance = start } = PSt) -> - count_brackets(Buffer, - PSt#pst { - buffer = [], - balance = start } ); -count_brackets([${ | Rem], - #pst { - buffer = Buffer, - balance = start } = PSt) -> - count_brackets(Rem, - PSt#pst{ - buffer = [ ${ | Buffer ], - balance = 1}); - -%% Drop any initial characters prior to opening bracket -count_brackets([_ | Rem], - #pst { balance = start } = PSt) -> - count_brackets(Rem, PSt ); - -%% If balance is back to zero, we have completed a JSON -%% element. -count_brackets(Rem, - #pst { - buffer = Buffer, - balance = 0 } = PSt) -> - - { complete, lists:reverse(Buffer), - PSt#pst { - buffer = Rem, - balance = start - } - }; - -%% If we still have balance, but no more input -%% we have an incomplete element.x -count_brackets([], PSt) -> - { incomplete, PSt }; - - -%% We have a string start or end, and we are not esacped -%% Flip our in-string state -count_brackets([$" | Rem], - #pst { - buffer = Buffer, - in_string = InString, - escaped = false} = PSt) -> - - count_brackets(Rem, PSt#pst { - buffer = [ $" | Buffer ], - in_string = not InString }); - - -%% We have an escape character, and we are in a string. Turn on our escape state -count_brackets([$\\ | Rem], - #pst { - buffer = Buffer, - in_string = true, - escaped = false } = PSt) -> - - count_brackets(Rem, PSt#pst { - buffer = [ $\\ | Buffer ], - escaped = true}); - -%% We have an opening bracket and we are not in a string -count_brackets([${ | Rem], - #pst { - buffer = Buffer, - balance = Balance, - in_string = false } = PSt) -> - - count_brackets(Rem, - PSt#pst { - buffer = [ ${ | Buffer ], - balance = Balance + 1}); - -%% We have an closing bracket and we are not in a string -count_brackets([$} | Rem], - #pst { - buffer = Buffer, - balance = Balance, - in_string = false } = PSt) -> - - count_brackets(Rem, - PSt#pst { - buffer = [ $} | Buffer ], - balance = Balance - 1}); - -%% We have just regular data to feed over. -%% Make sure to clear the escape state. -count_brackets([C | Rem], - #pst { buffer = Buffer } = PSt) -> - - count_brackets(Rem, PSt#pst { - buffer = [ C | Buffer ], - escaped = false - } ). - -extract_json(Buf, PST, Acc) -> - case count_brackets(Buf, PST) of - { complete, Processed, NPST} -> - io:format("Trying again~n"), - extract_json([], NPST, [ Processed | Acc]); - - - { incomplete, NPST} -> - io:format("Incomplete~n"), - { Acc, NPST } - end. - -extract_json(Buf, PST) -> - extract_json(Buf, PST,[]). |