summaryrefslogtreecommitdiff
path: root/components/dlink_tcp
diff options
context:
space:
mode:
authorMagnus Feuer <mfeuer@jaguarlandrover.com>2015-06-10 13:59:31 -0700
committerMagnus Feuer <mfeuer@jaguarlandrover.com>2015-06-10 15:52:44 -0700
commit84c55f5e3e186f8f4e1f958e5265f1dbe7b132c5 (patch)
treea8492549c36a03f6409d2d5abacb88f08ec298d0 /components/dlink_tcp
parent13cebd20cb7be14d0bfb2ffd01e84043f6692832 (diff)
downloadrvi_core-84c55f5e3e186f8f4e1f958e5265f1dbe7b132c5.tar.gz
Now processes multiple JSON elements in one chunk
Diffstat (limited to 'components/dlink_tcp')
-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,[]).