Commits

arkdro committed 15707dd

add multiply api

Comments (0)

Files changed (1)

erlang/src/c_server.erl

 -behaviour(gen_server).
 
 -export([
+         multiply/1,
          ping/0
         ]).
 
     gen_server:call(?SERVER, ping).
 
 %-------------------------------------------------------------------
+multiply(List) ->
+    gen_server:call(?SERVER, {mult, List}).
+
+%-------------------------------------------------------------------
 start_link() ->
     gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
 
 handle_call(ping, From, #state{status=running, port=Port} = State) ->
     port_command(Port, term_to_binary(ping)),
     {noreply, State#state{from=From}};
+handle_call({mult, Term}, From, #state{status=running, port=Port} = State) ->
+    Cmd = term_to_binary({ms, Term}),
+    port_command(Port, Cmd),
+    {noreply, State#state{from=From}};
 handle_call(stop, _From, St) ->
     {stop, normal, ok, St};
 handle_call(status, _From, St) ->
             #state{from=From, port = Port} = State) ->
     gen_server:reply(From, pong),
     {noreply, State#state{from=undefined}};
-handle_info({Port, {data, Data}}, #state{port = Port} = State) ->
+handle_info({Port, {data, Data}}, #state{from=From, port=Port} = State) ->
     Term = binary_to_term(Data),
     error_logger:info_report({handle_info, data, Term}),
+    gen_server:reply(From, {ok, Term}),
     {noreply, State};
 handle_info(_I, State) ->
     error_logger:info_report({handle_info, unknown, _I}),