|
- -module(stock_server).
-
- -behaviour(gen_server).
-
- -export([list/1, sell/2, buy/2, start/0,stop/0,start_trade/1,stop_trade/1]).
-
- -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-
- -record(stock,{id,pid}).
-
- -import(lists, [foreach/2]).
- -import(stock,[start/1,stop/1]).
-
- -include_lib("stdlib/include/qlc.hrl").
-
- start()->gen_server:start_link({local,stock_server},stock_server,[],[]).
- stop()->gen_server:cast(?MODULE,stop).
- sell(Id,Trade)->gen_server:cast(?MODULE,{sell,Id,Trade}).
- buy(Id,Trade)->gen_server:cast(?MODULE,{buy,Id,Trade}).
- list(Id)->gen_server:cast(?MODULE,{list,Id}).
- start_trade(Id)->gen_server:cast(?MODULE,{start_trade,Id}).
- stop_trade(Id)->gen_server:cast(?MODULE,{stop_trade,Id}).
-
- init([]) ->
- mnesia:start(),
- mnesia:wait_for_tables([stock], 20000),
- foreach(fun start_stock/1,getStock()),
- {ok, []}.
-
- handle_call(_Request, _From, State) ->
- Reply = ok,
- {reply, Reply, State}.
-
-
- handle_cast(stop,State) ->
- {stop,normal,State};
-
- handle_cast({start_trade,Id},State) ->
- start_stock(Id),
- {noreply, State};
- handle_cast({stop_trade,Id},State) ->
- case getPid(Id) of
- []->
- io:format("stock Id=~w no such stock~n",[Id]);
- [stop]->
- io:format("stock Id=~w have no start trade~n",[Id]);
-
- [Pid]->
- gen_server:cast(Pid,stop),
- removeId(Id)
- end,
- {noreply, State};
-
- handle_cast({list,Id}, State) ->
- case getPid(Id) of
- []->
- io:format("stock Id=~w no such stock~n",[Id]);
- [stop]->
- io:format("stock Id=~w have no start trade~n",[Id]);
- [Pid]->
- gen_server:cast(Pid,{list,5})
- end,
- {noreply, State};
-
- handle_cast({buy,Id,Trade}, State) ->
- case getPid(Id) of
- []->
- io:format("stock Id=~w no such stock~n",[Id]);
- [stop]->
- io:format("stock Id=~w have no start trade~n",[Id]);
- [Pid]->
-
- gen_server:cast(Pid,{buy,Trade})
- end,
- {noreply, State};
-
- handle_cast({sell,Id,Trade}, State) ->
- case getPid(Id) of
- []->
- io:format("stock Id=~w no such stock~n",[Id]);
- [stop]->
- io:format("stock Id=~w have no start trade~n",[Id]);
- [Pid]->
-
- gen_server:cast(Pid,{sell,Trade})
- end,
- {noreply, State};
-
- handle_cast({ok,Msg}, State) ->
- io:format("ok: ~w~n",[Msg]),
- {noreply, State};
-
- handle_cast(_Msg, State) ->
- {noreply, State}.
-
- handle_info(_Info, State) ->
- {noreply, State}.
-
- terminate(_Reason, _State) ->
- foreach(fun stop_stock/1,getStock()),
- mnesia:stop(),
- ok.
-
- code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
-
- %% --------------------------------------------------------------------
- %%% Internal functions
- %% --------------------------------------------------------------------
- removeId(Item) ->
- Oid = {stock, Item},
- F = fun() ->
- mnesia:delete(Oid)
- end,
- mnesia:transaction(F).
-
- saveId(Id,Pid)->
- Row = #stock{id=Id,pid=Pid},
- F = fun() ->
- mnesia:write(Row)
- end,
- mnesia:transaction(F).
-
- getStock()->
- do(qlc:q([{X#stock.id,X#stock.pid} || X <- mnesia:table(stock)])).
-
- getPid(Id)->
- do(qlc:q([X#stock.pid || X <- mnesia:table(stock),
- X#stock.id =:=Id])).
-
- do(Q) ->
- F = fun() -> qlc:e(Q) end,
- {atomic, Val} = mnesia:transaction(F),
- Val.
-
- start_stock({Id,_})->
- case gen_server:start(stock,[],[]) of
- {ok,Pid}->saveId(Id,Pid);
- Any->
- io:format("start_trade error:~w~n",[Any])
- end.
-
- stop_stock({Id,Pid})->
- case Pid of
- stop->
- ok;
- Pid->
- gen_server:cast(Pid, stop)
- end,
- saveId(Id,stop).
|