Permalink
Browse files

updated resource discovery code to match the book

  • Loading branch information...
richcarl committed Oct 6, 2010
1 parent f13b07f commit 6bf066bcdc9f7dc5921e113e628c5ec3d295a714
@@ -16,8 +16,8 @@
-define(SERVER, ?MODULE).
-record(state, {target_resource_types,
- local_typed_resources,
- typed_resources}).
+ local_resource_tuples,
+ found_resource_tuples}).
%% API
@@ -40,44 +40,44 @@ trade_resources() ->
init([]) ->
{ok, #state{target_resource_types = [],
- local_typed_resources = dict:new(),
- typed_resources = dict:new()}}.
+ local_resource_tuples = dict:new(),
+ found_resource_tuples = dict:new()}}.
handle_call({fetch_resources, Type}, _From, State) ->
- {reply, dict:find(Type, State#state.typed_resources), State}.
+ {reply, dict:find(Type, State#state.found_resource_tuples), State}.
handle_cast({add_target_resource_type, Type}, State) ->
TargetTypes = State#state.target_resource_types,
NewTargetTypes = [Type | lists:delete(Type, TargetTypes)],
{noreply, State#state{target_resource_types = NewTargetTypes}};
handle_cast({add_local_resource, {Type, Resource}}, State) ->
- LocalTypedResources = State#state.local_typed_resources,
- NewLocalTypedResources = add_resource(Type, Resource, LocalTypedResources),
- {noreply, State#state{local_typed_resources = NewLocalTypedResources}};
+ ResourceTuples = State#state.local_resource_tuples,
+ NewResourceTuples = add_resource(Type, Resource, ResourceTuples),
+ {noreply, State#state{local_resource_tuples = NewResourceTuples}};
handle_cast(trade_resources, State) ->
- LocalTypedResources = State#state.local_typed_resources,
+ ResourceTuples = State#state.local_resource_tuples,
AllNodes = [node() | nodes()],
lists:foreach(
fun(Node) ->
gen_server:cast({?SERVER, Node},
- {trade_resources, {node(), LocalTypedResources}})
+ {trade_resources, {node(), ResourceTuples}})
end,
AllNodes),
{noreply, State};
-handle_cast({trade_resources, {ReplyTo, RemoteTypedResources}},
- #state{local_typed_resources = LocalTypedResources,
+handle_cast({trade_resources, {ReplyTo, Remotes}},
+ #state{local_resource_tuples = Locals,
target_resource_types = TargetTypes,
- typed_resources = TypedResources} = State) ->
- FilteredRemoteTypedResources = filter_remote_resources_by_target_types(TargetTypes, RemoteTypedResources),
- NewTypedResources = add_typed_resources(FilteredRemoteTypedResources, TypedResources),
+ found_resource_tuples = OldFound} = State) ->
+ FilteredRemotes = resources_for_types(TargetTypes, Remotes),
+ NewFound = add_resources(FilteredRemotes, OldFound),
case ReplyTo of
noreply ->
ok;
_ ->
gen_server:cast({?SERVER, ReplyTo},
- {trade_resources, {noreply, LocalTypedResources}})
+ {trade_resources, {noreply, Locals}})
end,
- {noreply, State#state{typed_resources = NewTypedResources}}.
+ {noreply, State#state{found_resource_tuples = NewFound}}.
handle_info(ok = _Info, State) ->
{noreply, State}.
@@ -91,9 +91,9 @@ code_change(_OldVsn, State, _Extra) ->
%% Utilities
-add_typed_resources([{Type, Identifier}|T], Dict) ->
- add_typed_resources(T, add_resource(Type, Identifier, Dict));
-add_typed_resources([], Dict) ->
+add_resources([{Type, Identifier}|T], Dict) ->
+ add_resources(T, add_resource(Type, Identifier, Dict));
+add_resources([], Dict) ->
Dict.
add_resource(Type, Resource, Dict) ->
@@ -105,10 +105,10 @@ add_resource(Type, Resource, Dict) ->
dict:store(Type, [Resource], Dict)
end.
-filter_remote_resources_by_target_types(Types, TypedResources) ->
+resources_for_types(Types, ResourceTuples) ->
Fun =
fun(Type, Acc) ->
- case dict:find(Type, TypedResources) of
+ case dict:find(Type, ResourceTuples) of
{ok, List} ->
[{Type, Resource} || Resource <- List] ++ Acc;
error ->
@@ -16,8 +16,8 @@
-define(SERVER, ?MODULE).
-record(state, {target_resource_types,
- local_typed_resources,
- typed_resources}).
+ local_resource_tuples,
+ found_resource_tuples}).
%% API
@@ -40,44 +40,44 @@ trade_resources() ->
init([]) ->
{ok, #state{target_resource_types = [],
- local_typed_resources = dict:new(),
- typed_resources = dict:new()}}.
+ local_resource_tuples = dict:new(),
+ found_resource_tuples = dict:new()}}.
handle_call({fetch_resources, Type}, _From, State) ->
- {reply, dict:find(Type, State#state.typed_resources), State}.
+ {reply, dict:find(Type, State#state.found_resource_tuples), State}.
handle_cast({add_target_resource_type, Type}, State) ->
TargetTypes = State#state.target_resource_types,
NewTargetTypes = [Type | lists:delete(Type, TargetTypes)],
{noreply, State#state{target_resource_types = NewTargetTypes}};
handle_cast({add_local_resource, {Type, Resource}}, State) ->
- LocalTypedResources = State#state.local_typed_resources,
- NewLocalTypedResources = add_resource(Type, Resource, LocalTypedResources),
- {noreply, State#state{local_typed_resources = NewLocalTypedResources}};
+ ResourceTuples = State#state.local_resource_tuples,
+ NewResourceTuples = add_resource(Type, Resource, ResourceTuples),
+ {noreply, State#state{local_resource_tuples = NewResourceTuples}};
handle_cast(trade_resources, State) ->
- LocalTypedResources = State#state.local_typed_resources,
+ ResourceTuples = State#state.local_resource_tuples,
AllNodes = [node() | nodes()],
lists:foreach(
fun(Node) ->
gen_server:cast({?SERVER, Node},
- {trade_resources, {node(), LocalTypedResources}})
+ {trade_resources, {node(), ResourceTuples}})
end,
AllNodes),
{noreply, State};
-handle_cast({trade_resources, {ReplyTo, RemoteTypedResources}},
- #state{local_typed_resources = LocalTypedResources,
+handle_cast({trade_resources, {ReplyTo, Remotes}},
+ #state{local_resource_tuples = Locals,
target_resource_types = TargetTypes,
- typed_resources = TypedResources} = State) ->
- FilteredRemoteTypedResources = filter_remote_resources_by_target_types(TargetTypes, RemoteTypedResources),
- NewTypedResources = add_typed_resources(FilteredRemoteTypedResources, TypedResources),
+ found_resource_tuples = OldFound} = State) ->
+ FilteredRemotes = resources_for_types(TargetTypes, Remotes),
+ NewFound = add_resources(FilteredRemotes, OldFound),
case ReplyTo of
noreply ->
ok;
_ ->
gen_server:cast({?SERVER, ReplyTo},
- {trade_resources, {noreply, LocalTypedResources}})
+ {trade_resources, {noreply, Locals}})
end,
- {noreply, State#state{typed_resources = NewTypedResources}}.
+ {noreply, State#state{found_resource_tuples = NewFound}}.
handle_info(ok = _Info, State) ->
{noreply, State}.
@@ -91,9 +91,9 @@ code_change(_OldVsn, State, _Extra) ->
%% Utilities
-add_typed_resources([{Type, Identifier}|T], Dict) ->
- add_typed_resources(T, add_resource(Type, Identifier, Dict));
-add_typed_resources([], Dict) ->
+add_resources([{Type, Identifier}|T], Dict) ->
+ add_resources(T, add_resource(Type, Identifier, Dict));
+add_resources([], Dict) ->
Dict.
add_resource(Type, Resource, Dict) ->
@@ -105,10 +105,10 @@ add_resource(Type, Resource, Dict) ->
dict:store(Type, [Resource], Dict)
end.
-filter_remote_resources_by_target_types(Types, TypedResources) ->
+resources_for_types(Types, ResourceTuples) ->
Fun =
fun(Type, Acc) ->
- case dict:find(Type, TypedResources) of
+ case dict:find(Type, ResourceTuples) of
{ok, List} ->
[{Type, Resource} || Resource <- List] ++ Acc;
error ->
@@ -8,16 +8,16 @@
add_local_resource/2,
fetch_resources/1,
trade_resources/0
- ]).
+ ]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {target_resource_types,
- local_resources,
- resources}).
+ local_resource_tuples,
+ found_resource_tuples}).
%% API
@@ -27,8 +27,8 @@ start_link() ->
add_target_resource_type(Type) ->
gen_server:cast(?SERVER, {add_target_resource_type, Type}).
-add_local_resource(Type, Instance) ->
- gen_server:cast(?SERVER, {add_local_resource, {Type, Instance}}).
+add_local_resource(Type, Resource) ->
+ gen_server:cast(?SERVER, {add_local_resource, {Type, Resource}}).
fetch_resources(Type) ->
gen_server:call(?SERVER, {fetch_resources, Type}).
@@ -40,51 +40,46 @@ trade_resources() ->
init([]) ->
{ok, #state{target_resource_types = [],
- local_resources = dict:new(),
- resources = dict:new()}}.
+ local_resource_tuples = dict:new(),
+ found_resource_tuples = dict:new()}}.
handle_call({fetch_resources, Type}, _From, State) ->
- {reply, dict:find(Type, State#state.resources), State};
-handle_call(_Request, _From, State) ->
- Reply = ok,
- {reply, Reply, State}.
+ {reply, dict:find(Type, State#state.found_resource_tuples), State}.
handle_cast({add_target_resource_type, Type}, State) ->
TargetTypes = State#state.target_resource_types,
NewTargetTypes = [Type | lists:delete(Type, TargetTypes)],
{noreply, State#state{target_resource_types = NewTargetTypes}};
-handle_cast({add_local_resource, {Type, Instance}}, State) ->
- LocalResources = State#state.local_resources,
- NewLocalResources = add_resource(Type, Instance, LocalResources),
- {noreply, State#state{local_resources = NewLocalResources}};
+handle_cast({add_local_resource, {Type, Resource}}, State) ->
+ ResourceTuples = State#state.local_resource_tuples,
+ NewResourceTuples = add_resource(Type, Resource, ResourceTuples),
+ {noreply, State#state{local_resource_tuples = NewResourceTuples}};
handle_cast(trade_resources, State) ->
- LocalResources = State#state.local_resources,
+ ResourceTuples = State#state.local_resource_tuples,
AllNodes = [node() | nodes()],
lists:foreach(
fun(Node) ->
gen_server:cast({?SERVER, Node},
- {trade_resources, {node(), LocalResources}})
+ {trade_resources, {node(), ResourceTuples}})
end,
AllNodes),
{noreply, State};
-handle_cast({trade_resources, {ReplyTo, RemoteResources}},
- #state{local_resources = LocalResources,
- target_resource_types = TargetTypes,
- resources = Resources} = State) ->
- ResourceList = resources_for_types(TargetTypes, RemoteResources),
- NewResources = add_resources(ResourceList, Resources),
+handle_cast({trade_resources, {ReplyTo, Remotes}},
+ #state{local_resource_tuples = Locals,
+ target_resource_types = TargetTypes,
+ found_resource_tuples = OldFound} = State) ->
+ FilteredRemotes = resources_for_types(TargetTypes, Remotes),
+ NewFound = add_resources(FilteredRemotes, OldFound),
case ReplyTo of
noreply ->
ok;
_ ->
gen_server:cast({?SERVER, ReplyTo},
- {trade_resources, {noreply, LocalResources}})
+ {trade_resources, {noreply, Locals}})
end,
- {noreply, State#state{resources = NewResources}};
-handle_cast(_Msg, State) ->
- {noreply, State}.
+ {noreply, State#state{found_resource_tuples = NewFound}}.
-handle_info(_Info, State) ->
+handle_info(ok = _Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
@@ -110,12 +105,12 @@ add_resource(Type, Resource, Dict) ->
dict:store(Type, [Resource], Dict)
end.
-resources_for_types(Types, Resources) ->
+resources_for_types(Types, ResourceTuples) ->
Fun =
fun(Type, Acc) ->
- case dict:find(Type, Resources) of
+ case dict:find(Type, ResourceTuples) of
{ok, List} ->
- [{Type, Instance} || Instance <- List] ++ Acc;
+ [{Type, Resource} || Resource <- List] ++ Acc;
error ->
Acc
end
Oops, something went wrong.

0 comments on commit 6bf066b

Please sign in to comment.