|
|
@ -0,0 +1,189 @@ |
|
|
|
-module(ePortSrv). |
|
|
|
|
|
|
|
-behavior(gen_server). |
|
|
|
|
|
|
|
-define(PORT_NAME, "./priv/ePort"). |
|
|
|
-define(ERR(Args), io:format("IMY***************** ~p~n", [Args])). |
|
|
|
-define(CATCH(Expression), ( |
|
|
|
try Expression |
|
|
|
catch |
|
|
|
Error:Reason -> |
|
|
|
?ERR([?MODULE, ?LINE, {"Error", Error}, {"Reason", Reason}, {"Stack", erlang:get_stacktrace()}, {"Expression", ??Expression}]), |
|
|
|
{'EXIT',{Error, Reason}} |
|
|
|
end |
|
|
|
)). |
|
|
|
|
|
|
|
-record(state, {port = undefined}). |
|
|
|
|
|
|
|
-compile(export_all). |
|
|
|
|
|
|
|
%%%================================EXPORT================================ |
|
|
|
-export([ |
|
|
|
start_link/0 |
|
|
|
]). |
|
|
|
|
|
|
|
-export([ |
|
|
|
init/1 |
|
|
|
, handle_call/3 |
|
|
|
, handle_cast/2 |
|
|
|
, handle_info/2 |
|
|
|
, terminate/2 |
|
|
|
, code_change/3 |
|
|
|
]). |
|
|
|
|
|
|
|
open() -> |
|
|
|
Opts = [{packet, 2}, binary, exit_status, use_stdio], |
|
|
|
erlang:open_port({spawn_executable, ?PORT_NAME}, Opts). |
|
|
|
|
|
|
|
|
|
|
|
tt() -> |
|
|
|
case get(ppp) of |
|
|
|
undefined -> |
|
|
|
P = open(), |
|
|
|
put(ppp, P); |
|
|
|
_ -> |
|
|
|
ignore |
|
|
|
end, |
|
|
|
|
|
|
|
ePortMsg:port_call(get(ppp), term_to_binary({$g, 12432, 4324})). |
|
|
|
|
|
|
|
tt(CmdId) -> |
|
|
|
case get(ppp) of |
|
|
|
undefined -> |
|
|
|
P = open(), |
|
|
|
put(ppp, P); |
|
|
|
_ -> |
|
|
|
ignore |
|
|
|
end, |
|
|
|
case CmdId of |
|
|
|
$g -> |
|
|
|
ePortMsg:port_call(get(ppp), term_to_binary({CmdId, 12432, 4324})); |
|
|
|
$u -> |
|
|
|
ePortMsg:port_call(get(ppp), term_to_binary({CmdId, 12432, 4324})); |
|
|
|
$t -> |
|
|
|
ignore; |
|
|
|
_ -> |
|
|
|
ePortMsg:port_call(get(ppp), term_to_binary({CmdId, 12432, 4324})) |
|
|
|
end. |
|
|
|
|
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% start_link/0 |
|
|
|
%% @doc 启动gen_server进程 |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%% ----------------------------------------------------------------- |
|
|
|
-spec start_link() ->gen:start_ret(). |
|
|
|
%% ----------------------------------------------------------------- |
|
|
|
start_link() -> |
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). |
|
|
|
|
|
|
|
%%%================================behaviour functions================================ |
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% init/0 |
|
|
|
%% @doc 初始化 |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
-spec init(Args :: term()) -> |
|
|
|
{ok, State :: term()} | {ok, State :: term(), timeout() | hibernate} | |
|
|
|
{stop, Reason :: term()} | ignore. |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
init([]) -> |
|
|
|
process_flag(trap_exit,true), |
|
|
|
Port = open(), |
|
|
|
{ok, #state{port = Port}}. |
|
|
|
|
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% handle_call/3 |
|
|
|
%% @doc |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
-spec handle_call(Request :: term(), From :: {pid(), Tag :: term()}, |
|
|
|
State :: term()) -> |
|
|
|
{reply, Reply :: term(), NewState :: term()} | |
|
|
|
{reply, Reply :: term(), NewState :: term(), timeout() | hibernate} | |
|
|
|
{noreply, NewState :: term()} | |
|
|
|
{noreply, NewState :: term(), timeout() | hibernate} | |
|
|
|
{stop, Reason :: term(), Reply :: term(), NewState :: term()} | |
|
|
|
{stop, Reason :: term(), NewState :: term()}. |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
handle_call({func, F, Args}, _From, State) -> |
|
|
|
Result = ?CATCH(util_kit:catch_apply(F,Args)), |
|
|
|
{reply, Result, State}; |
|
|
|
handle_call(Request, _From, State) -> |
|
|
|
?ERR(["handle_call function clause", {"request", Request}]), |
|
|
|
Reply = ok, |
|
|
|
{reply, Reply, State}. |
|
|
|
|
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% handle_cast/2 |
|
|
|
%% @doc |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
-spec handle_cast(Request :: term(), State :: term()) -> |
|
|
|
{noreply, NewState :: term()} | |
|
|
|
{noreply, NewState :: term(), timeout() | hibernate} | |
|
|
|
{stop, Reason :: term(), NewState :: term()}. |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
handle_cast(Msg, State) -> |
|
|
|
?ERR(["handle_cast function clause", {"request", Msg}]), |
|
|
|
{noreply, State}. |
|
|
|
|
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% handle_info/2 |
|
|
|
%% @doc |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
-spec handle_info(Info :: timeout | term(), State :: term()) -> |
|
|
|
{noreply, NewState :: term()} | |
|
|
|
{noreply, NewState :: term(), timeout() | hibernate} | |
|
|
|
{stop, Reason :: term(), NewState :: term()}. |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
handle_info(Info, State) -> |
|
|
|
?ERR(["handle_info function clause", {"request", Info}]), |
|
|
|
{noreply, State}. |
|
|
|
|
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% terminate/2 |
|
|
|
%% @doc |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
-spec terminate(Reason :: (normal | shutdown | {shutdown, term()} |term()), State :: term()) -> |
|
|
|
term(). |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
terminate(Reason, #state{port = Port} = State) -> |
|
|
|
?ERR(["server_terminate_for", {"Reason", Reason}, {"State", State}, {"Dictionary", element(2,process_info(self(),dictionary))}]), |
|
|
|
port_close(Port), |
|
|
|
ok. |
|
|
|
|
|
|
|
%%----------------------------------------------------------------- |
|
|
|
%% code_change/3 |
|
|
|
%% @doc |
|
|
|
%% ``` |
|
|
|
%% |
|
|
|
%% ''' |
|
|
|
%% @end |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
-spec code_change(OldVsn :: (term() | {down, term()}), State :: term(), Extra :: term()) -> |
|
|
|
{ok, NewState :: term()} | {error, Reason :: term()}. |
|
|
|
%%-------------------------------------------------------------------- |
|
|
|
code_change(_OldVsn, State, _Extra) -> |
|
|
|
{ok, State}. |
|
|
|
|
|
|
|
|