summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Feuer <mfeuer@jaguarlandrover.com>2015-06-11 20:34:24 +0200
committerGerrit Code Review <support@gerrithub.io>2015-06-11 20:34:25 +0200
commit87a10222cc8321e93dda041013a90d53943e833e (patch)
treea8492549c36a03f6409d2d5abacb88f08ec298d0
parent60c513fd4c2c43f8953d244c6ad8423505b95cfe (diff)
parente2413ec232b775770ed2cec91e73f1cf166c5c2e (diff)
downloadrvi_core-87a10222cc8321e93dda041013a90d53943e833e.tar.gz
Merge changes from topic 'GitHub #27' into release-next
* changes: Merge branch 'testing' into release-next Now processes multiple JSON elements in one chunk
-rw-r--r--components/dlink_tcp/src/connection.erl44
1 files changed, 37 insertions, 7 deletions
diff --git a/components/dlink_tcp/src/connection.erl b/components/dlink_tcp/src/connection.erl
index 3917bee..06a648d 100644
--- a/components/dlink_tcp/src/connection.erl
+++ b/components/dlink_tcp/src/connection.erl
@@ -236,17 +236,18 @@ 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 count_brackets(Data, PST) of
- { incomplete, NPST } ->
+ case extract_json(Data, PST) of
+ { [], NPST } ->
?debug("~p:handle_info(data incomplete)", [ ?MODULE]),
inet:setopts(Sock, [{active, once}]),
{noreply, State#st { pst = NPST} };
- {complete, Processed, NPST } ->
- ?debug("~p:handle_info(data complete): Processed: ~p", [ ?MODULE, Processed]),
+ { JSONElements, NPST } ->
+ ?debug("~p:handle_info(data complete): Processed: ~p", [ ?MODULE, JSONElements]),
FromPid = self(),
- spawn(fun() -> Mod:Fun(FromPid, IP, Port,
- data, Processed, Arg) end),
+ spawn(fun() -> [ Mod:Fun(FromPid, IP, Port,
+ data, SingleElem, Arg) || SingleElem <- JSONElements ]
+ end),
inet:setopts(Sock, [ { active, once } ]),
{noreply, State#st { pst = NPST} }
end;
@@ -314,6 +315,20 @@ code_change(_OldVsn, State, _Extra) ->
%%%===================================================================
+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,
@@ -337,7 +352,7 @@ count_brackets(Rem,
{ complete, lists:reverse(Buffer),
PSt#pst {
- buffer = lists:reverse(Rem),
+ buffer = Rem,
balance = start
}
};
@@ -405,3 +420,18 @@ count_brackets([C | Rem],
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,[]).