Skip to content

Commit

Permalink
add execute
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Jan 29, 2016
1 parent bb69289 commit 9e4e736
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libchunter.app.src
@@ -1,7 +1,7 @@
{application, libchunter,
[
{description, "Chunter interface library."},
{vsn, "0.1.43"},
{vsn, "0.1.44"},
{registered, []},
{applications, [
kernel,
Expand Down
26 changes: 26 additions & 0 deletions src/libchunter.erl
Expand Up @@ -24,6 +24,7 @@
console_open/3,
console_open/4,
console_send/2,
execute/6,
snapshot/4,
delete_snapshot/4,
rollback_snapshot/4,
Expand Down Expand Up @@ -81,6 +82,31 @@ update(Server, Port) ->
%%% Console commands
%%%===================================================================


execute(Server, Port, VM, Cmd, Acc0, FoldFn) ->
{ok, Socket} = gen_tcp:connect(Server, Port, [binary, {active, false},
{packet, 4}], 500),
ok = gen_tcp:send(Socket, term_to_binary({execute, VM, Cmd})),
wait_for_data(Socket, Acc0, FoldFn).

wait_for_data(Socket, Acc, Callback) ->
case gen_tcp:recv(Socket, 0) of
{ok, Bin} ->
case binary_to_term(Bin) of
done ->
R = Callback(Acc, done),
gen_tcp:close(Socket),
R;
Other ->
Acc1 = Callback(Acc, Other),
wait_for_data(Socket, Acc1, Callback)
end;
E ->
R = Callback(Acc, E),
gen_tcp:close(Socket),
R
end.

%%--------------------------------------------------------------------
%% @doc Opens a new console connection. A process is spawned all
%% output from the repote console is send to the current process in
Expand Down

0 comments on commit 9e4e736

Please sign in to comment.