Permalink
Browse files

transports: move common functions into verx_client

  • Loading branch information...
1 parent 6666d6d commit 5b9221d84c7d60cff3da5ccedf14d1d04b8ab9e2 @msantos committed Sep 15, 2012
Showing with 29 additions and 64 deletions.
  1. +24 −1 src/verx_client.erl
  2. +2 −21 src/verx_client_tcp.erl
  3. +2 −35 src/verx_client_tls.erl
  4. +1 −7 src/verx_client_unix.erl
View
@@ -29,6 +29,8 @@
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
-module(verx_client).
+
+-include_lib("kernel/include/inet.hrl").
-include("verx.hrl").
-include("verx_client.hrl").
@@ -47,7 +49,11 @@
getserial/1
]).
--export([stream/2]).
+-export([
+ stream/2,
+ resolv/1, resolv/2,
+ reply_to_caller/2
+ ]).
%%-------------------------------------------------------------------------
@@ -226,3 +232,20 @@ stream(Data, #verx_buf{
buflen = BufLen + iolist_size(Data),
buf = [Data|Buf]
}}.
+
+resolv(Host) ->
+ resolv(Host, inet6).
+resolv(Host, Family) ->
+ case inet:gethostbyname(Host, Family) of
+ {error, nxdomain} ->
+ resolv(Host, inet);
+ {ok, #hostent{h_addr_list = [IPaddr|_IPaddrs]}} ->
+ {IPaddr, Family};
+ Error ->
+ Error
+ end.
+
+reply_to_caller(Pid, Data) ->
+ Reply = verx_rpc:decode(Data),
+ Pid ! {verx, self(), Reply},
+ ok.
@@ -31,7 +31,6 @@
-module(verx_client_tcp).
-behaviour(gen_server).
--include_lib("kernel/include/inet.hrl").
-include("verx.hrl").
-include("verx_client.hrl").
@@ -55,7 +54,7 @@ init([Pid, Opt]) ->
Host = proplists:get_value(host, Opt, "127.0.0.1"),
Port = proplists:get_value(port, Opt, ?LIBVIRT_TCP_PORT),
- {IP, Family} = resolv(Host),
+ {IP, Family} = verx_client:resolv(Host),
% Connect to the libvirt socket
{ok, Socket} = gen_tcp:connect(IP, Port, [
@@ -136,7 +135,7 @@ handle_info({tcp, Socket, Data},
buf = Buf} = State) ->
inet:setopts(Socket, [{active, once}]),
{Msgs, Rest} = verx_client:stream(Data, Buf),
- [ reply_to_caller(Pid, Msg) || Msg <- Msgs ],
+ [ verx_client:reply_to_caller(Pid, Msg) || Msg <- Msgs ],
{noreply, State#state{buf = Rest}};
handle_info({tcp_closed, Socket}, #state{s = Socket} = State) ->
@@ -152,27 +151,9 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-
%%-------------------------------------------------------------------------
%%% Internal functions
%%-------------------------------------------------------------------------
-resolv(Host) ->
- resolv(Host, inet6).
-resolv(Host, Family) ->
- case inet:gethostbyname(Host, Family) of
- {error, nxdomain} ->
- resolv(Host, inet);
- {ok, #hostent{h_addr_list = [IPaddr|_IPaddrs]}} ->
- {IPaddr, Family};
- Error ->
- Error
- end.
-
send_rpc(Socket, Buf) ->
Len = ?REMOTE_MESSAGE_HEADER_XDR_LEN + byte_size(Buf),
gen_tcp:send(Socket, <<?UINT32(Len), Buf/binary>>).
-
-reply_to_caller(Pid, Data) ->
- Reply = verx_rpc:decode(Data),
- Pid ! {verx, self(), Reply},
- ok.
@@ -31,7 +31,6 @@
-module(verx_client_tls).
-behaviour(gen_server).
--include_lib("kernel/include/inet.hrl").
-include("verx.hrl").
-include("verx_client.hrl").
@@ -63,7 +62,7 @@ init([Pid, Opt]) ->
Password = proplists:get_value(password, Opt, ""),
Ciphers = proplists:get_value(ciphers, Opt, ssl:cipher_suites()),
- {IP, Family} = resolv(Host),
+ {IP, Family} = verx_client:resolv(Host),
% Connect to the libvirt TLS port
{ok, Socket} = ssl:connect(IP, Port, [
@@ -141,20 +140,6 @@ handle_call(stop, _From, State) ->
handle_cast(_Msg, State) ->
{noreply, State}.
-handle_info({ssl, Socket, <<?UINT32(Len), Data/binary>>},
- #state{s = Socket,
- pid = Pid,
- buf = {0, []}} = State)
- when Len =:= byte_size(Data) + ?REMOTE_MESSAGE_HEADER_XDR_LEN ->
- ssl:setopts(Socket, [{active, once}]),
- reply_to_caller(Pid, Data),
- {noreply, State};
-
-handle_info({ssl, Socket, <<?UINT32(Len), Data/binary>>},
- #state{s = Socket, buf = {0, []}} = State) ->
- ssl:setopts(Socket, [{active, once}]),
- {noreply, State#state{buf = {Len, [Data]}}};
-
% XXX FIXME 1 byte (<<1>>) is received at the beginning of the packet
% XXX FIXME
% XXX FIXME Thought it was because of this:
@@ -179,7 +164,7 @@ handle_info({ssl, Socket, Data},
buf = Buf} = State) ->
ssl:setopts(Socket, [{active, once}]),
{Msgs, Rest} = verx_client:stream(Data, Buf),
- [ reply_to_caller(Pid, Msg) || Msg <- Msgs ],
+ [ verx_client:reply_to_caller(Pid, Msg) || Msg <- Msgs ],
{noreply, State#state{buf = Rest}};
handle_info({ssl_closed, Socket}, #state{s = Socket} = State) ->
@@ -195,27 +180,9 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-
%%-------------------------------------------------------------------------
%%% Internal functions
%%-------------------------------------------------------------------------
-resolv(Host) ->
- resolv(Host, inet6).
-resolv(Host, Family) ->
- case inet:gethostbyname(Host, Family) of
- {error, nxdomain} ->
- resolv(Host, inet);
- {ok, #hostent{h_addr_list = [IPaddr|_IPaddrs]}} ->
- {IPaddr, Family};
- Error ->
- Error
- end.
-
send_rpc(Socket, Buf) ->
Len = ?REMOTE_MESSAGE_HEADER_XDR_LEN + byte_size(Buf),
ssl:send(Socket, <<?UINT32(Len), Buf/binary>>).
-
-reply_to_caller(Pid, Data) ->
- Reply = verx_rpc:decode(Data),
- Pid ! {verx, self(), Reply},
- ok.
@@ -140,7 +140,7 @@ handle_info({Port, {data, Data}},
pid = Pid,
buf = Buf} = State) ->
{Msgs, Rest} = verx_client:stream(Data, Buf),
- [ reply_to_caller(Pid, Msg) || Msg <- Msgs ],
+ [ verx_client:reply_to_caller(Pid, Msg) || Msg <- Msgs ],
{noreply, State#state{buf = Rest}};
handle_info({'EXIT', Port, Reason}, #state{port = Port} = State) ->
@@ -158,7 +158,6 @@ terminate(_Reason, #state{s = Socket, port = Port}) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-
%%-------------------------------------------------------------------------
%%% Internal functions
%%-------------------------------------------------------------------------
@@ -176,8 +175,3 @@ send_rpc(Port, Buf) ->
error:Error ->
{error, Error}
end.
-
-reply_to_caller(Pid, Data) ->
- Reply = verx_rpc:decode(Data),
- Pid ! {verx, self(), Reply},
- ok.

0 comments on commit 5b9221d

Please sign in to comment.