Permalink
Browse files

modified: include/bael.hrl

	modified:   src/bael.erl
	modified:   src/bael_fsm.erl
	modified:   src/bael_fsm_sup.erl
	modified:   src/bael_msg_manager.erl
	modified:   src/bael_server_sup.erl
	modified:   src/bael_struct.erl
	modified:   src/bael_views.erl
	modified:   src/bael_xml.erl
  • Loading branch information...
1 parent 016588f commit bdc596d215778f8888c1b8b58328d9dd05a334e3 @solomonwzs committed Apr 21, 2012
Showing with 97 additions and 48 deletions.
  1. +3 −1 include/bael.hrl
  2. +1 −1 src/bael.erl
  3. +1 −1 src/bael_fsm.erl
  4. +5 −3 src/bael_fsm_sup.erl
  5. +47 −19 src/bael_msg_manager.erl
  6. +5 −3 src/bael_server_sup.erl
  7. +21 −13 src/bael_struct.erl
  8. +5 −3 src/bael_views.erl
  9. +9 −4 src/bael_xml.erl
View
@@ -3,6 +3,8 @@
-define(MAX_FSMS_POOL_SIZE, 10).
-define(MAX_MSG_POOL_SIZE, 100).
-define(FSM_BUSY_TIMEOUT, 1000).
+-define(MSG_MANAGER_RETRY_TIME, 1000).
+-define(MSG_START_WORK_TIME, 100).
-define(URLS, [
{"^hello/?$", hello},
{"^hello/(.+?)/?$", hello},
@@ -31,7 +33,7 @@
fsm_sup_ref,
msg_num=0,
get_msgs,%{M, F}
- get_msgs_args,
+ get_msgs_args=null,
msg_list=[]
}).
View
@@ -44,4 +44,4 @@ stop() ->
init_db_conn()->
emysql:add_pool(db_test, ?DB_CONN_NUM, ?DB_USER_NAME, ?DB_PASSWORD,
- ?DB_HOSTNAME, ?DB_PORT, ?DB_DEFAULT_DB, utf8).
+ ?DB_HOSTNAME, ?DB_PORT, ?DB_DEFAULT_DB, utf8).
View
@@ -25,7 +25,7 @@ handle_event(_Event, _StateName, StateData)->
handle_sync_event(just_test, From, StateName, StateData)->
io:format("handle sync_send_all_state_event(from: ~p): ~p~n",
- [From, just_test]),
+ [From, just_test]),
io:format("fsm(~p) state: ~p~n", [self(), StateName]),
{reply, {self(), reply}, StateName, StateData, hibernate};
handle_sync_event(test_timeout, _From, StateName, StateData)->
View
@@ -13,7 +13,9 @@ upgrade()->
init([])->
Strategy={one_for_one, 10, 10},
- SpecsList=[{lists:concat(["fsm_", ID]), {bael_fsm, start_link, []},
- permanent, 5000, worker, dynamic}||
- ID<-lists:seq(0, ?MAX_FSMS_POOL_SIZE-1)],
+ SpecsList=[{
+ lists:concat(["fsm_", ID]),
+ {bael_fsm, start_link, []},
+ permanent, 5000, worker, dynamic
+ }||ID<-lists:seq(0, ?MAX_FSMS_POOL_SIZE-1)],
{ok, {Strategy, lists:flatten(SpecsList)}}.
View
@@ -1,49 +1,77 @@
-module(bael_msg_manager).
-behaviour(gen_fsm).
-include("bael.hrl").
--export([start_link/3]).
+-export([start_link/2]).
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3,
terminate/3, code_change/4]).
--export([idle/2]).
+-export([idle/2, work/2]).
-start_link(FsmSupRef, GetMsgs, InitArgs)->
+start_link(FsmSupRef, GetMsgs)->
gen_server:start_link({local, ?MODULE}, ?MODULE,
- [FsmSupRef, GetMsgs, InitArgs], []).
+ [FsmSupRef, GetMsgs], []).
-init([FsmSupRef, {M, F}, InitArgs])->
- {Msgs, NextArgs}=M:F(InitArgs),
+init([FsmSupRef, {M, F}])->
{ok, idle, #msg_manager_state{
fsm_sup_ref=FsmSupRef,
- get_msgs={M, F},
- get_msgs_args=NextArgs,
- msg_list=Msgs
+ get_msgs={M, F}
}}.
-handle_event(_Event, _StateName, StateData)->
- {next_state, idle, StateData}.
+handle_event(_Event, StateName, StateData)->
+ {next_state, StateName, StateData}.
-handle_sync_event(_Event, _From, _StateName, StateData)->
+handle_sync_event(_Event, _From, StateName, StateData)->
%{next_state, idle, StateData}.
- {reply, null, idle, StateData}.
+ {reply, null, StateName, StateData}.
-handle_info(_Info, _StateName, StateData)->
- {next_state, idle, StateData}.
+handle_info(_Info, StateName, StateData)->
+ {next_state, StateName, StateData}.
terminate(_Reason, _StateName, _StateData)->
ok.
code_change(_OldVsn, _StateName, StateData, _Extra)->
{ok, idle, StateData}.
-idle(start, StateData)->
- {next_state, idle, StateData}.
+idle({start, Args}, StateData)->
+ {next_state, work, StateData#msg_manager_state{
+ get_msgs_args=Args
+ }, 1}.
+
+work(timeout, StateData)->
+ {MsgList, NextArgs}=check_msg_list(StateData),
+ case MsgList of
+ []->
+ {next_state, idle, StateData#msg_manager_state{
+ msg_list=[],
+ get_msgs_args=null
+ }};
+ [Msg|Tail]->
+ Pid=find_idle_fsm(StateData#msg_manager_state.fsm_sup_ref),
+ gen_fsm:send_all_state_event(Pid, Msg),
+ {next_state, work, StateData#msg_manager_state{
+ msg_list=Tail,
+ get_msgs_args=NextArgs
+ }, 1}
+ end.
+
+check_msg_list(#msg_manager_state{
+ msg_list=List,
+ get_msgs={M, F},
+ get_msgs_args=Args
+})->
+ case List of
+ []->
+ M:F(Args);
+ _->
+ {List, Args}
+ end.
find_idle_fsm(FsmSupRef)->
Func=fun(X)->
{_, Pid, _, _}=X,
try
State=gen_fsm:sync_send_all_event(Pid, get_state,
- ?FSM_BUSY_TIMEOUT),
+ ?FSM_BUSY_TIMEOUT),
{State, Pid}
catch
_:_->{busy, Pid}
@@ -52,7 +80,7 @@ find_idle_fsm(FsmSupRef)->
List=lists:foreach(Func, supervisor:which_children(FsmSupRef)),
case proplists:get_all_values(idle, List) of
[]->
- timer:sleep(1000),
+ timer:sleep(?MSG_MANAGER_RETRY_TIME),
find_idle_fsm(FsmSupRef);
Res->
hd(Res)
View
@@ -13,7 +13,9 @@ upgrade()->
init([])->
Strategy={one_for_one, 10, 10},
- SpecsList=[{lists:concat(["server_", ID]), {bael_server, start_link, []},
- permanent, 5000, worker, dynamic}||
- ID<-lists:seq(0, ?MAX_SERVERS_POOL_SIZE-1)],
+ SpecsList=[{
+ lists:concat(["server_", ID]),
+ {bael_server, start_link, []},
+ permanent, 5000, worker, dynamic
+ }||ID<-lists:seq(0, ?MAX_SERVERS_POOL_SIZE-1)],
{ok, {Strategy, lists:flatten(SpecsList)}}.
View
@@ -29,22 +29,30 @@ set_value([Head|Tail], NewValue, Struct)->
Value=proplists:get_value(Key, ValueList),
if
Value=:=undefined->throw("invalid_key");
- Tail=:=[]->
- {struct, lists:append(proplists:delete(Key, ValueList),
- [{Key, bael_common_func:thing_to_binary(NewValue)}])};
- length(Tail)>0->
- {struct, lists:append(proplists:delete(Key, ValueList),
- [{Key, set_value(Tail,
- bael_common_func:thing_to_binary(NewValue), Value)}])}
+ Tail=:=[]->{
+ struct,
+ lists:append(proplists:delete(Key, ValueList),
+ [{Key, bael_common_func:thing_to_binary(NewValue)}]
+ )};
+ length(Tail)>0->{
+ struct,
+ lists:append(proplists:delete(Key, ValueList), [{
+ Key,
+ set_value(
+ Tail,
+ bael_common_func:thing_to_binary(NewValue),
+ Value
+ )}])}
end
catch
Type:What->
- Report=["set struct value failed",
- {key, Head},
- {value, NewValue},
- {type, Type},
- {what, What},
- {trace, erlang:get_stacktrace()}],
+ Report=[
+ "set struct value failed",
+ {key, Head},
+ {value, NewValue},
+ {type, Type},
+ {what, What},
+ {trace, erlang:get_stacktrace()}],
error_logger:error_report(Report),
Type(set_struct_value_failed)
end.
View
@@ -33,9 +33,11 @@ hello('POST', Req)->
handle_data_table(Req, DataTable)->
{_ResultPacket, _, FieldList, TableData, _}=DataTable,
FieldNames=get_fields(FieldList),
- bael_shortcuts:render_ok(Req, bael_table_dtl, [
- {field_names, FieldNames},
- {table_data, TableData}]).
+ bael_shortcuts:render_ok(
+ Req,
+ bael_table_dtl,
+ [{field_names, FieldNames}, {table_data, TableData}]
+ ).
get_fields([])->
[];
get_fields([Head|Tail])->
View
@@ -54,8 +54,11 @@ save_elements_to_dict({Tags, Depth, XmlElement})->
Length=length(ParentsList)+1,
if
Depth=:=null orelse Depth>=Length->
- {TextList, ElementList}=
- lists:foldl(fun filter_xml_content/2, {[], []}, Content),
+ {TextList, ElementList}=lists:foldl(
+ fun filter_xml_content/2,
+ {[], []},
+ Content
+ ),
Element=#xml_element{
tag_name=Tag,
parents=ParentsList,
@@ -67,8 +70,10 @@ save_elements_to_dict({Tags, Depth, XmlElement})->
MathElementsList=erlang:get(?MATH_XML_ELEMENTS_LIST),
if
Head=:=Tag andalso Math=:=true->
- erlang:put(?MATH_XML_ELEMENTS_LIST,
- lists:append(MathElementsList, [Element]));
+ erlang:put(
+ ?MATH_XML_ELEMENTS_LIST,
+ lists:append(MathElementsList, [Element])
+ );
true->not_math
end,
lists:foreach(fun save_elements_to_dict/1,

0 comments on commit bdc596d

Please sign in to comment.