diff options
author | Brian Cully <bjc@kublai.com> | 2012-02-09 15:33:02 -0500 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2012-02-09 15:45:29 -0500 |
commit | b9892e56efff45f76d505337fc03f3e04f7c6073 (patch) | |
tree | a8491258f6ecc26ce445d87ee63958196ed4498d /src | |
parent | 44d5b82c5d48a3b8eb58642ccc882357392c7274 (diff) | |
download | mysqlerl-b9892e56efff45f76d505337fc03f3e04f7c6073.tar.gz mysqlerl-b9892e56efff45f76d505337fc03f3e04f7c6073.zip |
Remove port module and port supervisor
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 5 | ||||
-rw-r--r-- | src/mysqlerl.app | 3 | ||||
-rw-r--r-- | src/mysqlerl_connection.erl | 60 | ||||
-rw-r--r-- | src/mysqlerl_port.erl | 83 | ||||
-rw-r--r-- | src/mysqlerl_port.hrl | 1 | ||||
-rw-r--r-- | src/mysqlerl_port_sup.erl | 13 |
6 files changed, 49 insertions, 116 deletions
diff --git a/src/Makefile b/src/Makefile index 7fb96de..40062eb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,13 +3,14 @@ EFLAGS = -W +debug_info BEAMDIR = ../ebin BINS = $(BEAMDIR)/mysqlerl.app -BEAMS = mysqlerl.beam mysqlerl_app.beam mysqlerl_connection.beam \ - mysqlerl_port.beam mysqlerl_port_sup.beam mysqlerl_sup.beam +BEAMS = mysqlerl.beam mysqlerl_app.beam mysqlerl_sup.beam \ + mysqlerl_connection.beam all: $(BEAMDIR) $(BINS) clean: rm -rf *.beam + rm -rf $(BEAMDIR)/*.beam rm -rf $(BINS) $(BEAMS) $(BEAMDIR)/mysqlerl.app %.beam: %.erl diff --git a/src/mysqlerl.app b/src/mysqlerl.app index 1fa56b4..cbade16 100644 --- a/src/mysqlerl.app +++ b/src/mysqlerl.app @@ -4,8 +4,7 @@ {application, mysqlerl, [{description, "ODBC-compatible MYSQL driver"}, {vsn, "0"}, - {modules, [mysqlerl, mysqlerl_app, mysqlerl_sup, mysqlerl_connection, - mysql_port_sup, mysql_port]}, + {modules, [mysqlerl, mysqlerl_app, mysqlerl_sup, mysqlerl_connection]}, {registered, [mysqlerl_sup]}, {applications, [kernel, stdlib]}, {env, []}, diff --git a/src/mysqlerl_connection.erl b/src/mysqlerl_connection.erl index e009428..46d9fd3 100644 --- a/src/mysqlerl_connection.erl +++ b/src/mysqlerl_connection.erl @@ -2,7 +2,6 @@ -author('bjc@kublai.com'). -include("mysqlerl.hrl"). --include("mysqlerl_port.hrl"). -behavior(gen_server). @@ -11,7 +10,9 @@ -export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]). --record(state, {sup, owner}). +-record(state, {port, owner}). + +-define(CONNECT_TIMEOUT, 30000). start_link(Owner, Host, Port, Database, User, Password, Options) -> gen_server:start_link(?MODULE, [Owner, Host, Port, Database, @@ -25,11 +26,20 @@ stop(Pid) -> init([Owner, Host, Port, Database, User, Password, Options]) -> process_flag(trap_exit, true), - link(Owner), - {ok, Sup} = supervisor:start_link(mysqlerl_port_sup, - [helper(), Host, Port, Database, - User, Password, Options]), - {ok, #state{sup = Sup, owner = Owner}}. + erlang:monitor(process, Owner), + Ref = open_port({spawn, helper()}, [{packet, 4}, binary]), + ConnectArgs = #sql_connect{host = Host, + port = Port, + database = Database, + user = User, + password = Password, + options = Options}, + case send_port_cmd(Ref, ConnectArgs, ?CONNECT_TIMEOUT) of + {data, ok} -> + {ok, #state{port = Ref, owner = Owner}}; + {'EXIT', Ref, Reason} -> + {stop, {port_closed, Reason}} + end. terminate(Reason, _State) -> io:format("DEBUG: connection got terminate: ~p~n", [Reason]), @@ -45,15 +55,28 @@ handle_call(Request, From, #state{owner = Owner} = State) {reply, {error, process_not_owner_of_odbc_connection}, State}; handle_call(stop, _From, State) -> {stop, normal, State}; -handle_call(Request, _From, State) -> - {reply, gen_server:call(port_ref(State#state.sup), - #req{request = Request}, infinity), State}. +handle_call({Req, Timeout}, From, State) -> + case send_port_cmd(State#state.port, Req, Timeout) of + {data, Res} -> + {reply, Res, State}; + {'EXIT', _Ref, Reason} -> + {stop, {port_closed, Reason}, State}; + timeout -> + gen_server:reply(From, timeout), + {stop, timeout, State}; + Other -> + error_logger:warning_msg("Got unknown query response: ~p~n", + [Other]), + gen_server:reply(From, {error, connection_closed}), + {stop, {unknownreply, Other}, State} + end. handle_cast(_Request, State) -> {noreply, State}. -handle_info({'EXIT', Pid, _Reason}, #state{owner = Pid} = State) -> - io:format("DEBUG: owner ~p shut down.~n", [Pid]), +handle_info({'DOWN', _Monitor, process, _Reason, _PID, Reason}, + #state{owner = PID} = State) -> + io:format("DEBUG: owner ~p shut down: ~p.~n", [PID, Reason]), {stop, normal, State}. helper() -> @@ -63,6 +86,13 @@ helper() -> end, filename:nativename(filename:join([PrivDir, "bin", "mysqlerl"])). -port_ref(Sup) -> - [{mysqlerl_port, Ref, worker, _}] = supervisor:which_children(Sup), - Ref. +send_port_cmd(Ref, Request, Timeout) -> + io:format("DEBUG: Sending request: ~p~n", [Request]), + port_command(Ref, term_to_binary(Request)), + receive + {Ref, {data, Res}} -> + {data, binary_to_term(Res)}; + Other -> Other + after Timeout -> + timeout + end. diff --git a/src/mysqlerl_port.erl b/src/mysqlerl_port.erl deleted file mode 100644 index 74fe177..0000000 --- a/src/mysqlerl_port.erl +++ /dev/null @@ -1,83 +0,0 @@ --module(mysqlerl_port). --author('bjc@kublai.com'). - --include("mysqlerl.hrl"). --include("mysqlerl_port.hrl"). - --behavior(gen_server). - --export([start_link/7]). --export([init/1, terminate/2, code_change/3, - handle_call/3, handle_cast/2, handle_info/2]). - --define(CONNECT_TIMEOUT, 30000). - --record(state, {ref}). --record(port_closed, {reason}). - -start_link(Cmd, Host, Port, Database, User, Password, Options) -> - gen_server:start_link(?MODULE, - [Cmd, Host, Port, Database, User, Password, Options], - []). - -init([Cmd, Host, Port, Database, User, Password, Options]) -> - process_flag(trap_exit, true), - Ref = open_port({spawn, Cmd}, [{packet, 4}, binary]), - {data, ok} = send_port_cmd(Ref, #sql_connect{host = Host, - port = Port, - database = Database, - user = User, - password = Password, - options = Options}, - ?CONNECT_TIMEOUT), - {ok, #state{ref = Ref}}. - -terminate(#port_closed{reason = Reason}, #state{ref = Ref}) -> - io:format("DEBUG: mysqlerl connection ~p shutting down (~p).~n", - [Ref, Reason]), - ok; -terminate(Reason, State) -> - catch port_close(State#state.ref), - io:format("DEBUG: mysqlerl_port got terminate: ~p~n", [Reason]), - ok. - -code_change(_OldVsn, State, _Extra) -> - {ok, State}. - -handle_call(#req{request = {Request, Timeout}}, From, - #state{ref = Ref} = State) -> - case send_port_cmd(Ref, Request, Timeout) of - {data, Res} -> - {reply, Res, State}; - {'EXIT', Ref, Reason} -> - gen_server:reply(From, {error, connection_closed}), - {stop, #port_closed{reason = Reason}, State}; - timeout -> - gen_server:reply(From, timeout), - {stop, timeout, State}; - Other -> - error_logger:warning_msg("Got unknown query response: ~p~n", - [Other]), - gen_server:reply(From, {error, connection_closed}), - {stop, {unknownreply, Other}, State} - end. - - -handle_cast(_Request, State) -> - {noreply, State}. - -handle_info({'EXIT', Ref, Reason}, #state{ref = Ref} = State) -> - io:format("DEBUG: Port ~p closed on ~p.~n", [Ref, State]), - {stop, #port_closed{reason = Reason}, State}. - - -send_port_cmd(Ref, Request, Timeout) -> - io:format("DEBUG: Sending request: ~p~n", [Request]), - port_command(Ref, term_to_binary(Request)), - receive - {Ref, {data, Res}} -> - {data, binary_to_term(Res)}; - Other -> Other - after Timeout -> - timeout - end. diff --git a/src/mysqlerl_port.hrl b/src/mysqlerl_port.hrl deleted file mode 100644 index a7a3300..0000000 --- a/src/mysqlerl_port.hrl +++ /dev/null @@ -1 +0,0 @@ --record(req, {request}). diff --git a/src/mysqlerl_port_sup.erl b/src/mysqlerl_port_sup.erl deleted file mode 100644 index f215d48..0000000 --- a/src/mysqlerl_port_sup.erl +++ /dev/null @@ -1,13 +0,0 @@ --module(mysqlerl_port_sup). --author('bjc@kublai.com'). - --behavior(supervisor). - --export([init/1]). - -init([Cmd, Host, Port, Database, User, Password, Options]) -> - Ref = {mysqlerl_port, {mysqlerl_port, start_link, - [Cmd, Host, Port, Database, - User, Password, Options]}, - transient, 5, worker, [mysqlerl_port]}, - {ok, {{one_for_one, 10, 5}, [Ref]}}. |