1-module(portc). 2-vsn(1). 3-behaviour(gen_server). 4 5-export([get_data/0]). 6-export([init/1, handle_call/3, handle_info/2, code_change/3]). 7 8-record(state, {port, data}). 9 10get_data() -> gen_server:call(portc, get_data). 11 12init([]) -> 13 PortProg = code:priv_dir(foo) ++ "/bin/portc", 14 Port = open_port({spawn, PortProg}, [binary, {packet, 2}]), 15 {ok, #state{port = Port}}. 16 17handle_call(get_data, _From, State) -> 18 {reply, {ok, State#state.data}, State}. 19 20handle_info({Port, Cmd}, State) -> 21 NewState = do_cmd(Cmd, State), 22 {noreply, NewState}. 23 24code_change(_, State, change_port_only) -> 25 State#state.port ! close, 26 receive 27 {Port, closed} -> true 28 end, 29 NPortProg = code:priv_dir(foo) ++ "/bin/portc", % get new version 30 NPort = open_port({spawn, NPortProg}, [binary, {packet, 2}]), 31 {ok, State#state{port = NPort}}. 32