summaryrefslogtreecommitdiff
path: root/components/dlink
diff options
context:
space:
mode:
authorUlf Wiger <ulf@feuerlabs.com>2015-12-06 13:54:17 -0800
committerUlf Wiger <ulf@feuerlabs.com>2015-12-06 13:54:17 -0800
commit6cfeffca9f8e93e45dd885702a77896e2a1d0951 (patch)
tree620e2dd9006b52df7129d135fa7256d793571df1 /components/dlink
parent7d098a34b25704dbaa8bea0217ca6b7be37a0e48 (diff)
downloadrvi_core-6cfeffca9f8e93e45dd885702a77896e2a1d0951.tar.gz
new protocol & setup scripts
Diffstat (limited to 'components/dlink')
-rw-r--r--components/dlink/src/dlink.app.src6
-rw-r--r--components/dlink/src/dlink_data_json.erl60
-rw-r--r--components/dlink/src/dlink_data_msgpack.erl15
-rw-r--r--components/dlink/src/dlink_data_rvi.erl123
4 files changed, 39 insertions, 165 deletions
diff --git a/components/dlink/src/dlink.app.src b/components/dlink/src/dlink.app.src
index 6666333..7dca4d5 100644
--- a/components/dlink/src/dlink.app.src
+++ b/components/dlink/src/dlink.app.src
@@ -14,7 +14,11 @@
{applications, [
kernel,
stdlib,
- rvi_common
+ rvi_common,
+ authorize,
+ service_edge,
+ service_discovery,
+ schedule
]},
{mod, {dlink_app, []}},
{start_phases, [{announce, []}]},
diff --git a/components/dlink/src/dlink_data_json.erl b/components/dlink/src/dlink_data_json.erl
index 1ad82b2..6a68e48 100644
--- a/components/dlink/src/dlink_data_json.erl
+++ b/components/dlink/src/dlink_data_json.erl
@@ -1,6 +1,10 @@
-module(dlink_data_json).
--compile(export_all).
+-export([encode/2,
+ decode/3]).
+-export([init/1,
+ port_options/0]).
+
init(_Opts) ->
[].
@@ -8,41 +12,25 @@ init(_Opts) ->
port_options() ->
[list, {packet, 0}].
-decode(Msg, St) ->
- {Msg1, St1} = append(St, Msg),
- try exo_json:decode(St1, Msg1) of
- {done, {ok, {struct, Elems}}, Rest} ->
- {ok, [Elems], Rest};
- {done, {ok, {array, Structs}}, Rest} ->
- {ok, [Str || {struct, Str} <- Structs], Rest};
- {done, {error, Reason}, Rest} ->
- {error, Reason, Rest};
- {more, Cont} ->
- {more, Cont}
- catch
- error:Error ->
- {error, Error, St1};
- exit:Exit ->
- {error, Exit, St1}
- end.
+decode(Msg, F, St) when is_function(F, 1) ->
+ jsx_decode_stream(Msg, F, St).
+
+encode(Msg, St) ->
+ {ok, rvi_common:term_to_json(Msg), St}.
-encode({struct, _} = JSON, St) ->
- try {ok, exo_json:encode(JSON), St}
- catch exit:Error -> erlang:error(Error)
- end;
-encode({array, Structs} = JSON, St) ->
- case lists:all(fun({struct,_}) -> true;
- (_) -> false
- end, Structs) of
- true ->
- {ok, exo_json:encode(JSON), St};
- false ->
- erlang:error(invalid_json_structure)
+jsx_decode_stream(Data, F, St) ->
+ case jsx_decode(Data, St) of
+ {incomplete, Cont} ->
+ {ok, Cont};
+ {with_tail, Elems, <<>>} ->
+ F(Elems),
+ {ok, []};
+ {with_tail, Elems, Rest} ->
+ F(Elems),
+ jsx_decode_stream(Rest, F, [])
end.
-append([], Msg) ->
- {Msg, []};
-append([_|_] = St, Msg) ->
- {St ++ Msg, []};
-append(Cont, Msg) when is_tuple(Cont) ->
- {Msg, Cont}.
+jsx_decode(Data, []) ->
+ jsx:decode(Data, [stream, return_tail]);
+jsx_decode(Data, Cont) when is_function(Cont, 1) ->
+ Cont(Data).
diff --git a/components/dlink/src/dlink_data_msgpack.erl b/components/dlink/src/dlink_data_msgpack.erl
index 9510a53..253da55 100644
--- a/components/dlink/src/dlink_data_msgpack.erl
+++ b/components/dlink/src/dlink_data_msgpack.erl
@@ -1,6 +1,10 @@
-module(dlink_data_msgpack).
--compile(export_all).
+-export([init/1,
+ decode/3,
+ encode/2]).
+
+-export([port_options/0]).
-record(st, {opts = [{allow_atom, pack},
{enable_str, true},
@@ -13,15 +17,16 @@ port_options() ->
init(_CS) ->
#st{}.
-decode(Msg0, #st{buf = Prev, opts = Opts} = St) ->
+decode(Msg0, F, #st{buf = Prev, opts = Opts} = St) when is_function(F, 1) ->
Msg = append(Prev, Msg0),
case msgpack:unpack_stream(Msg, Opts) of
{error, incomplete} ->
- {more, St#st{buf = Msg}};
+ {ok, St#st{buf = Msg}};
{error, E} ->
- {error, E, St};
+ {error, E};
{Decoded, Rest} when is_binary(Rest) ->
- {ok, Decoded, St#st{buf = Rest}}
+ F(Decoded),
+ decode(Rest, F, St#st{buf = <<>>})
end.
encode({struct, Elems}, #st{opts = Opts} = St) ->
diff --git a/components/dlink/src/dlink_data_rvi.erl b/components/dlink/src/dlink_data_rvi.erl
deleted file mode 100644
index 01131be..0000000
--- a/components/dlink/src/dlink_data_rvi.erl
+++ /dev/null
@@ -1,123 +0,0 @@
--module(dlink_data_rvi).
-
--compile(export_all).
-
--record(dlink_data_rvi, {need, buf}).
-
--define(MAX_LINE, 79).
-
-init(_Opts) ->
- undefined.
-
-port_options() ->
- [].
-
-encode(Elems, St) ->
- Bin = encode_(Elems, <<>>),
- Sz = byte_size(Bin),
- {ok, <<"&RVI|",
- (integer_to_binary(Sz, 16))/binary, "\n",
- Bin/binary>>, St}.
-
-encode_([{Key, Val}|T], Acc) ->
- {Type, ValBin} = encode_val(Val),
- Bin = encode_elem(to_bin(Key), Type, ValBin),
- encode_(T, <<Acc/binary, Bin/binary>>);
-encode_([], Acc) ->
- Acc.
-
-encode_val(V) when is_binary(V) -> {$B, V};
-encode_val(V) when is_integer(V) -> {$i, integer_to_binary(V,16)};
-encode_val(V) when is_atom(V) -> {$a, atom_to_binary(V, latin1)};
-encode_val(V) when is_float(V) ->
- Bin = <<V/float>>,
- {$f, Bin};
-encode_val({T,_} = J) when T==array; T==struct ->
- JSON = exo_json:encode(J),
- {$J, iolist_to_binary(JSON)};
-encode_val([T|_] = L) when is_tuple(T) ->
- {$L, encode_(L, <<>>)}.
-
-decode_value($B, Bin) -> Bin;
-decode_value($i, Bin) -> binary_to_integer(Bin, 16);
-decode_value($f, <<F/float>>) -> F;
-decode_value($a, Bin) -> binary_to_existing_atom(Bin, latin1);
-decode_value($J, Bin) ->
- {ok, Obj} = exo_json:decode_string(binary_to_list(Bin)),
- Obj;
-decode_value($L, Bin) ->
- decode_packet(Bin).
-
-encode_elem(Key, Type, Bin) ->
- BSz = byte_size(Bin),
- case byte_size(Key) + BSz of
- Sz when Sz =< 78, Type >= $a, Type =< $z ->
- <<Key/binary, "|", Type, ":", Bin/binary, "\n">>;
- _ ->
- <<Key/binary, "|", Type, "|",
- (integer_to_binary(BSz+1,16))/binary, "\n",
- Bin/binary, "\n">>
- end.
-
-decode(<<"&RVI|", Rest/binary>>, undefined) ->
- case erlang:decode_packet(line, Rest, [{line_length, 79}]) of
- {more, _} ->
- {more, Rest};
- {ok, Ln, Rest1} ->
- LSz = byte_size(Ln),
- LSz1 = LSz-1,
- <<Size:LSz1/binary, "\n">> = Ln,
- Bytes = binary_to_integer(Size, 16),
- case Rest1 of
- <<Pkt:Bytes/binary, Tail/binary>> ->
- {ok, decode_packet(Pkt), Tail};
- _ ->
- {more, #dlink_data_rvi{need = Bytes, buf = Rest1}}
- end
- end;
-decode(Data, #dlink_data_rvi{need = Bytes, buf = Buf} = St) ->
- case <<Buf/binary, Data/binary>> of
- <<Pkt:Bytes/binary, Tail/binary>> ->
- {ok, decode_packet(Pkt), Tail};
- Buf1 ->
- {more, St#dlink_data_rvi{buf = Buf1}}
- end;
-decode(_, _St) ->
- {error, unknown, undefined}.
-
-decode_packet(<<>>) ->
- [];
-decode_packet(P) ->
- {ok, L, Rest} = erlang:decode_packet(line, P, [{line_length, ?MAX_LINE}]),
- case split_line(L) of
- {Key, Type, simple, Data} ->
- [{Key, decode_value(Type, Data)}|decode_packet(Rest)];
- {Key, Type, Size} ->
- Size1 = Size-1,
- <<VBin:Size1/binary, "\n", Rest1/binary>> = Rest,
- [{Key, decode_value(Type, VBin)}|decode_packet(Rest1)]
- end.
-
-to_bin(V) when is_atom(V) -> atom_to_binary(V, latin1);
-to_bin(V) when is_binary(V) -> V;
-to_bin(V) when is_list(V) -> iolist_to_binary(V).
-
-split_line(L) ->
- split_line(L, <<>>).
-
-split_line(<<"\\", $|, Rest/binary>>, Acc) ->
- split_line(Rest, <<Acc/binary, $|>>);
-split_line(<<"|", T, ":", Rest/binary>>, Acc) ->
- {Acc, T, simple, remove_nl(Rest)};
-split_line(<<"|", T, "|", Rest/binary>>, Acc) ->
- SzBin = remove_nl(Rest),
- {Acc, T, binary_to_integer(SzBin, 16)};
-split_line(<<H, T/binary>>, Acc) ->
- split_line(T, <<Acc/binary, H>>).
-
-
-remove_nl(B) ->
- Sz = byte_size(B),
- Sz1 = Sz-1,
- <<V:Sz1/binary, "\n">> = B,
- V.