Permalink
Browse files

Move the common functions out of the transports

Begin merging the transports by moving the API calls into verx_client.
The return type of start/start_link is once again a reference to the
gen_server.
  • Loading branch information...
1 parent 735c6bf commit 6666d6d83ca79d7d00f1fcac35f095c6e608bc1c @msantos committed Sep 15, 2012
Showing with 116 additions and 462 deletions.
  1. +14 −20 bin/gen_verx.escript
  2. +3 −3 bin/verx
  3. +93 −31 src/verx_client.erl
  4. +2 −137 src/verx_client_tcp.erl
  5. +2 −134 src/verx_client_tls.erl
  6. +2 −137 src/verx_client_unix.erl
View
@@ -77,28 +77,22 @@ main([File]) ->
Functions = [ begin
{Pattern, Body} = case Arity of
0 ->
- % name({Module, Ref}) -> Module:call(Ref, 'PROC_NAME').
- {[erl_syntax:tuple([
- erl_syntax:variable("Module"),
- erl_syntax:variable("Ref")
- ])],
- erl_syntax:application(
- erl_syntax:variable("Module"),
- erl_syntax:atom(call),
- [erl_syntax:variable("Ref"), erl_syntax:atom(Proc)]
- )};
+ % name(Ref) -> verx_client:call(Ref, 'PROC_NAME').
+ {[erl_syntax:variable("Ref")],
+ erl_syntax:application(
+ erl_syntax:atom(verx_client),
+ erl_syntax:atom(call),
+ [erl_syntax:variable("Ref"), erl_syntax:atom(Proc)]
+ )};
_ ->
% name(Ref, Arg) -> verx_client:call(Ref, 'PROC_NAME', Arg).
- {[erl_syntax:tuple([
- erl_syntax:variable("Module"),
- erl_syntax:variable("Ref")
- ]), erl_syntax:variable("Arg")],
- erl_syntax:application(
- erl_syntax:variable("Module"),
- erl_syntax:atom(call),
- [erl_syntax:variable("Ref"), erl_syntax:atom(Proc),
- erl_syntax:variable("Arg")]
- )}
+ {[erl_syntax:variable("Ref"), erl_syntax:variable("Arg")],
+ erl_syntax:application(
+ erl_syntax:atom(verx_client),
+ erl_syntax:atom(call),
+ [erl_syntax:variable("Ref"), erl_syntax:atom(Proc),
+ erl_syntax:variable("Arg")]
+ )}
end,
Clause = erl_syntax:clause(Pattern, [], [Body]),
View
@@ -378,13 +378,13 @@ console_recv(Name, Opt) ->
ok = verx:domain_open_console(Ref, [D, Devname, Flags]),
console_read(Ref).
-console_read({_,Pid} = Ref) ->
+console_read(Ref) ->
receive
- {verx, Pid, {#remote_message_header{
+ {verx, Ref, {#remote_message_header{
type = <<?REMOTE_STREAM:32>>,
status = <<?REMOTE_OK:32>>}, []}} ->
ok;
- {verx, Pid, {#remote_message_header{
+ {verx, Ref, {#remote_message_header{
type = <<?REMOTE_STREAM:32>>,
status = <<?REMOTE_CONTINUE:32>>}, Buf}} ->
io:format("~s", [Buf]),
View
@@ -37,6 +37,9 @@
-export([
call/2, call/3,
+ cast/2, cast/3, cast/4,
+ reply/2, reply/3,
+
send/2,
recv/1, recv/2,
recvall/1, recvall/2,
@@ -50,54 +53,113 @@
%%-------------------------------------------------------------------------
%%% API
%%-------------------------------------------------------------------------
-call({Module, Ref}, Proc) ->
- Module:call(Ref, Proc).
-call({Module, Ref}, Proc, Arg) ->
- Module:call(Ref, Proc, Arg).
-
start() ->
start([]).
-
start(Arg) ->
Transport = proplists:get_value(transport, Arg, verx_client_unix),
- case Transport:start(Arg) of
- {ok, Ref} -> {ok, {Transport, Ref}};
- Error -> Error
- end.
+ Self = self(),
+ gen_server:start(Transport, [Self, Arg], []).
start_link() ->
start_link([]).
-
start_link(Arg) ->
Transport = proplists:get_value(transport, Arg, verx_client_unix),
- case Transport:start_link(Arg) of
- {ok, Ref} -> {ok, {Transport, Ref}};
- Error -> Error
- end.
+ Self = self(),
+ gen_server:start_link(Transport, [Self, Arg], []).
-stop({Module, Ref}) ->
- Module:stop(Ref).
+stop(Ref) when is_pid(Ref) ->
+ gen_server:call(Ref, stop).
-send({Module, Ref}, Buf) ->
- Module:send(Ref, Buf).
-recv({Module, Ref}) ->
- Module:recv(Ref).
+call(Ref, Proc) ->
+ call(Ref, Proc, []).
+call(Ref, Proc, Arg) ->
+ case cast(Ref, Proc, Arg, infinity) of
+ {ok, Serial} -> reply(Ref, Serial);
+ Error -> Error
+ end.
-recv({Module, Ref}, Timeout) ->
- Module:recv(Ref, Timeout).
+cast(Ref, Proc) ->
+ cast(Ref, Proc, [], infinity).
+cast(Ref, Proc, Arg) ->
+ cast(Ref, Proc, Arg, infinity).
+cast(Ref, Proc, Arg, Timeout)
+ when is_pid(Ref), is_atom(Proc), is_list(Arg) ->
+ gen_server:call(Ref, {call, Proc, Arg}, Timeout).
+
+send(_Ref, []) ->
+ ok;
+send(Ref, [Buf|Rest]) when is_binary(Buf) ->
+ ok = gen_server:call(Ref, {send, Buf}, infinity),
+ send(Ref, Rest).
+
+reply(Ref, Serial) ->
+ reply(Ref, Serial, infinity).
+reply(Ref, Serial, Timeout) when is_pid(Ref), is_integer(Serial) ->
+ receive
+ {verx, Ref, {#remote_message_header{
+ serial = <<Serial:32>>,
+ type = <<?REMOTE_REPLY:32>>}, _} = Reply} ->
+ verx_rpc:status(Reply)
+ after
+ Timeout ->
+ {error, eagain}
+ end.
-recvall({Module, Ref}) ->
- Module:recvall(Ref).
+recv(Ref) ->
+ recv(Ref, 5000).
+recv(Ref, Timeout) ->
+ Serial = getserial(Ref),
+ recv(Ref, Serial, Timeout).
+recv(Ref, Serial, Timeout) when is_pid(Ref), is_integer(Serial) ->
+ receive
+ {verx, Ref, {#remote_message_header{
+ serial = <<Serial:32>>,
+ type = <<?REMOTE_STREAM:32>>,
+ status = <<?REMOTE_OK:32>>}, []}} ->
+ ok;
+ {verx, Ref, {#remote_message_header{
+ serial = <<Serial:32>>,
+ type = <<?REMOTE_STREAM:32>>,
+ status = <<?REMOTE_CONTINUE:32>>}, Payload}} ->
+ {ok, Payload}
+ after
+ Timeout ->
+ {error, eagain}
+ end.
+
+recvall(Ref) ->
+ recvall(Ref, 2000).
+recvall(Ref, Timeout) ->
+ recvall(Ref, Timeout, []).
+recvall(Ref, Timeout, Acc) when is_pid(Ref) ->
+ receive
+ {verx, Ref, {#remote_message_header{
+ type = <<?REMOTE_STREAM:32>>,
+ status = <<?REMOTE_OK:32>>}, []}} ->
+ {ok, lists:reverse(Acc)};
+ % XXX A stream indicates finish by setting the status to
+ % XXX REMOTE_OK. For screenshots, an empty body is returned with the
+ % XXX status set to 'continue'.
+ {verx, Ref, {#remote_message_header{
+ type = <<?REMOTE_STREAM:32>>,
+ status = <<?REMOTE_CONTINUE:32>>}, <<>>}} ->
+ {ok, lists:reverse(Acc)};
+ {verx, Ref, {#remote_message_header{
+ type = <<?REMOTE_STREAM:32>>,
+ status = <<?REMOTE_CONTINUE:32>>}, Payload}} ->
+ recvall(Ref, Timeout, [Payload|Acc])
+ after
+ Timeout ->
+ {ok, lists:reverse(Acc)}
+ end.
-recvall({Module, Ref}, Timeout) ->
- Module:recvall(Ref, Timeout).
+finish(Ref) when is_pid(Ref) ->
+ gen_server:call(Ref, finish, infinity).
-finish({Module, Ref}) ->
- Module:finish(Ref).
+getserial(Ref) when is_pid(Ref) ->
+ gen_server:call(Ref, getserial).
-getserial({Module, Ref}) ->
- Module:getserial(Ref).
%%-------------------------------------------------------------------------
%%% Utility functions
View
@@ -35,22 +35,6 @@
-include("verx.hrl").
-include("verx_client.hrl").
--export([
- call/2, call/3,
-
- cast/2, cast/3, cast/4,
- reply/2, reply/3,
-
- recv/1, recv/2, recv/3,
- recvall/1, recvall/2,
-
- send/2,
- finish/1,
-
- getserial/1
- ]).
--export([start_link/0, start_link/1]).
--export([start/0, start/1, stop/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
@@ -63,117 +47,6 @@
}).
-%%-------------------------------------------------------------------------
-%%% API
-%%-------------------------------------------------------------------------
-call(Ref, Proc) ->
- call(Ref, Proc, []).
-call(Ref, Proc, Arg) when is_pid(Ref), is_atom(Proc), is_list(Arg) ->
- case cast(Ref, Proc, Arg, infinity) of
- {ok, Serial} -> reply(Ref, Serial);
- Error -> Error
- end.
-
-cast(Ref, Proc) ->
- cast(Ref, Proc, [], infinity).
-cast(Ref, Proc, Arg) ->
- cast(Ref, Proc, Arg, infinity).
-cast(Ref, Proc, Arg, Timeout) ->
- gen_server:call(Ref, {call, Proc, Arg}, Timeout).
-
-reply(Ref, Serial) ->
- reply(Ref, Serial, infinity).
-reply(Ref, Serial, Timeout) ->
- receive
- {verx, Ref, {#remote_message_header{
- serial = <<Serial:32>>,
- type = <<?REMOTE_REPLY:32>>}, _} = Reply} ->
- verx_rpc:status(Reply)
- after
- Timeout ->
- {error, eagain}
- end.
-
-recv(Ref) ->
- recv(Ref, 5000).
-recv(Ref, Timeout) ->
- #state{serial = Serial} = getstate(Ref),
- recv(Ref, Serial, Timeout).
-recv(Ref, Serial, Timeout) ->
- receive
- {verx, Ref, {#remote_message_header{
- serial = <<Serial:32>>,
- type = <<?REMOTE_STREAM:32>>,
- status = <<?REMOTE_OK:32>>}, []}} ->
- ok;
- {verx, Ref, {#remote_message_header{
- serial = <<Serial:32>>,
- type = <<?REMOTE_STREAM:32>>,
- status = <<?REMOTE_CONTINUE:32>>}, Payload}} ->
- {ok, Payload}
- after
- Timeout ->
- {error, eagain}
- end.
-
-recvall(Ref) ->
- recvall(Ref, 2000).
-recvall(Ref, Timeout) ->
- recvall(Ref, Timeout, []).
-recvall(Ref, Timeout, Acc) ->
- receive
- {verx, Ref, {#remote_message_header{
- type = <<?REMOTE_STREAM:32>>,
- status = <<?REMOTE_OK:32>>}, []}} ->
- {ok, lists:reverse(Acc)};
- % XXX A stream indicates finish by setting the status to
- % XXX REMOTE_OK. For screenshots, an empty body is returned with the
- % XXX status set to 'continue'.
- {verx, Ref, {#remote_message_header{
- type = <<?REMOTE_STREAM:32>>,
- status = <<?REMOTE_CONTINUE:32>>}, <<>>}} ->
- {ok, lists:reverse(Acc)};
- {verx, Ref, {#remote_message_header{
- type = <<?REMOTE_STREAM:32>>,
- status = <<?REMOTE_CONTINUE:32>>}, Payload}} ->
- recvall(Ref, Timeout, [Payload|Acc])
- after
- Timeout ->
- case length(Acc) of
- 0 -> {error, eagain};
- _ -> {ok, lists:reverse(Acc)}
- end
- end.
-
-send(_Ref, []) ->
- ok;
-send(Ref, [Buf|Rest]) when is_binary(Buf) ->
- ok = gen_server:call(Ref, {send, Buf}, infinity),
- send(Ref, Rest).
-
-finish(Ref) when is_pid(Ref) ->
- gen_server:call(Ref, finish, infinity).
-
-getserial(Ref) when is_pid(Ref) ->
- #state{serial = Serial} = gen_server:call(Ref, getstate),
- Serial.
-
-start() ->
- start([]).
-start(Opt) when is_list(Opt) ->
- Self = self(),
- gen_server:start(?MODULE, [Self, Opt], []).
-
-start_link() ->
- start_link([]).
-start_link(Opt) when is_list(Opt) ->
- Self = self(),
- gen_server:start_link(?MODULE, [Self, Opt], []).
-
-stop(Ref) when is_pid(Ref) ->
- gen_server:call(Ref, stop).
-
-
%%-------------------------------------------------------------------------
%%% Callbacks
%%-------------------------------------------------------------------------
@@ -244,8 +117,8 @@ handle_call(finish, _From, #state{
inet:setopts(Socket, [{active, once}]),
{reply, Reply, State};
-handle_call(getstate, _From, State) ->
- {reply, State, State};
+handle_call(getserial, _From, #state{serial = Serial} = State) ->
+ {reply, Serial, State};
handle_call(stop, _From, State) ->
{stop, shutdown, ok, State}.
@@ -280,11 +153,6 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-%%-------------------------------------------------------------------------
-%%% Utility functions
-%%-------------------------------------------------------------------------
-
-
%%-------------------------------------------------------------------------
%%% Internal functions
%%-------------------------------------------------------------------------
@@ -308,6 +176,3 @@ reply_to_caller(Pid, Data) ->
Reply = verx_rpc:decode(Data),
Pid ! {verx, self(), Reply},
ok.
-
-getstate(Ref) when is_pid(Ref) ->
- gen_server:call(Ref, getstate).
Oops, something went wrong.

0 comments on commit 6666d6d

Please sign in to comment.