summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-11-29 03:50:59 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-11-29 03:50:59 +0100
commit759c05470e33cc25ced5b47aa181f1ca79181ac8 (patch)
treedc3da695bad67ccf025f0350216f6358f08b6a22 /src
parent02da4adf9ae6b477376bb27a092feec06a3f2b91 (diff)
Adds owners to caches items, allowing for pings.
Diffstat (limited to 'src')
-rw-r--r--src/handler.erl10
-rw-r--r--src/io/database_shim.erl6
-rw-r--r--src/io/timed_cache.erl39
-rw-r--r--src/io/timed_caches_manager.erl71
-rw-r--r--src/query/character_turn.erl26
-rw-r--r--src/query/load_state.erl12
6 files changed, 103 insertions, 61 deletions
diff --git a/src/handler.erl b/src/handler.erl
index 15f9e5c..ed14801 100644
--- a/src/handler.erl
+++ b/src/handler.erl
@@ -17,11 +17,9 @@
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start (_YawsParams) ->
- {ok, Pid} = gen_server:start(timed_caches_manager, [], []),
+ {ok, Pid} = timed_caches_manager:start(),
database_shim:generate_db(Pid),
- gen_server:cast(Pid, {add, battlemap_db, none}),
- gen_server:cast(Pid, {add, battlemap_instance_db, none}),
- gen_server:cast(Pid, {add, character_db, none}),
-% gen_server:cast(Pid, {add, character_turn_db, none, character_turn:keypos()}),
-% gen_server:cast(Pid, {add, player_data_db, none, player_data:keypos()}),
+ timed_caches_manager:new_cache(Pid, battlemap_db, none),
+ timed_caches_manager:new_cache(Pid, battlemap_instance_db, none),
+ timed_caches_manager:new_cache(Pid, character_db, none),
ok.
diff --git a/src/io/database_shim.erl b/src/io/database_shim.erl
index 0b9ea1c..d55d848 100644
--- a/src/io/database_shim.erl
+++ b/src/io/database_shim.erl
@@ -12,7 +12,7 @@
[
generate_db/1,
fetch/2,
- commit/3,
+ commit/4,
assert_session_is_valid/2
]
).
@@ -98,9 +98,9 @@ fetch (DB, ObjectID) ->
[] -> nothing
end.
-commit (DB, ObjectID, Value) ->
+commit (DB, Owner, ObjectID, Value) ->
add_to_db({DB, ObjectID}, Value),
- timed_cache:invalidate(DB, ObjectID).
+ timed_cache:invalidate(DB, Owner, ObjectID).
assert_session_is_valid (_PlayerID, _SessionToken) ->
% Ask PlayerID's login server if SessionToken is correct.
diff --git a/src/io/timed_cache.erl b/src/io/timed_cache.erl
index 1b750b0..ec58caa 100644
--- a/src/io/timed_cache.erl
+++ b/src/io/timed_cache.erl
@@ -26,18 +26,18 @@
-export
(
[
- fetch/2,
- invalidate/2
+ fetch/3,
+ invalidate/3
]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-add_to_cache (DB, ObjectID) ->
- {ok, TimerPID} = gen_server:start(?MODULE, {DB, ObjectID}, []),
+add_to_cache (DB, Owner, ObjectID) ->
+ {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []),
{ok, Data} = database_shim:fetch(DB, ObjectID),
- ets:insert(DB, {ObjectID, TimerPID, Data}),
+ ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}),
Data.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -48,13 +48,21 @@ init ({DB, ObjectID}) ->
{ok, {DB, ObjectID}}.
handle_call (invalidate, _, State) ->
- {stop, normal, State}.
+ {stop, normal, State};
+handle_call (ping, _, State) ->
+ {noreply, State, timed_caches_manager:get_timeout()}.
handle_cast (invalidate, State) ->
- {stop, normal, State}.
+ {stop, normal, State};
+handle_cast (ping, State) ->
+ {noreply, State, timed_caches_manager:get_timeout()}.
terminate (_, {DB, ObjectID}) ->
- io:format("~nCache entry timed out: ~p.~n", [{DB, ObjectID}]),
+ io:format
+ (
+ "~nCache entry timed out or was invalidated: ~p.~n",
+ [{DB, ObjectID}]
+ ),
ets:delete(DB, ObjectID).
code_change (_, State, _) ->
@@ -66,20 +74,21 @@ format_status (_, [_, State]) ->
handle_info(timeout, State) ->
{stop, normal, State};
handle_info(_, {DB, ObjectID}) ->
- {noreply, {DB, ObjectID}, timed_caches_manager:get_timeout(DB)}.
+ {noreply, {DB, ObjectID}, timed_caches_manager:get_timeout()}.
%%%% Interface Functions
-fetch (DB, ObjectID) ->
- io:format("~nfetch from cache: ~p.~n", [{DB, ObjectID}]),
- case ets:lookup(DB, ObjectID) of
- [] -> add_to_cache(DB, ObjectID);
+fetch (DB, Owner, ObjectID) ->
+ io:format("~nfetch from cache: ~p.~n", [{DB, {Owner, ObjectID}}]),
+ case ets:lookup(DB, {Owner, ObjectID}) of
+ [] -> add_to_cache(DB, Owner, ObjectID);
[{_, TimerPID, Data}] ->
+ gen_server:cast(TimerPID, ping),
Data
end.
-invalidate (DB, ObjectID) ->
- case ets:lookup(DB, ObjectID) of
+invalidate (DB, Owner, ObjectID) ->
+ case ets:lookup(DB, {Owner, ObjectID}) of
[] -> ok;
[{_, TimerPID, _}] ->
diff --git a/src/io/timed_caches_manager.erl b/src/io/timed_caches_manager.erl
index 21f2b6b..5901964 100644
--- a/src/io/timed_caches_manager.erl
+++ b/src/io/timed_caches_manager.erl
@@ -24,17 +24,17 @@
%%%% Actual Interface
-export(
[
- add_cache/3,
- inherit_cache/3,
+ start/0,
+ new_cache/3,
delete_cache/2,
- get_timeout/1
+ get_timeout/0
]
)
.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-delete_cache (DB) ->
+remove_cache (DB) ->
ets:delete(DB).
add_cache (DB, none) ->
@@ -74,6 +74,29 @@ inherit_cache (CacheList, DB, Heir) ->
[DB|CacheList]
end.
+remove_cache (CacheList, DB) ->
+ case lists:member(DB, CacheList) of
+ true ->
+ remove_cache(DB),
+ lists:delete(DB, CacheList);
+ false ->
+ CacheList
+ end.
+
+add_cache (CacheList, DB, Heir) ->
+ case lists:member(DB, CacheList) of
+ true when (Heir =:= none) ->
+ CacheList;
+
+ true ->
+ ets:setopts(DB, {heir, Heir, DB}),
+ CacheList;
+
+ false ->
+ add_cache(DB, Heir),
+ [DB|CacheList]
+ end.
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -82,8 +105,8 @@ init (CacheList) ->
io:format("~nStarting Timed Caches Manager..."),
{ok, CacheList}.
-handle_call ({delete, CacheName}, _Caller, State) ->
- {noreply, delete_cache(State, CacheName)};
+handle_call ({remove, CacheName}, _Caller, State) ->
+ {noreply, remove_cache(State, CacheName)};
handle_call ({add, CacheName, Heir}, _Caller, State)->
{noreply, add_cache(State, CacheName, Heir)};
handle_call ({inherit, CacheName, Heir}, _Caller, State)->
@@ -91,8 +114,8 @@ handle_call ({inherit, CacheName, Heir}, _Caller, State)->
handle_call (terminate, _, State) ->
{stop, normal, State}.
-handle_cast ({delete, CacheName}, State) ->
- {noreply, delete_cache(State, CacheName)};
+handle_cast ({remove, CacheName}, State) ->
+ {noreply, remove_cache(State, CacheName)};
handle_cast ({add, CacheName, Heir}, State)->
{noreply, add_cache(State, CacheName, Heir)};
handle_cast ({inherit, CacheName, Heir}, State)->
@@ -103,7 +126,7 @@ handle_cast (terminate, State) ->
terminate (_Reason, []) ->
ok;
terminate (Reason, [CacheName|OtherCaches]) ->
- delete_cache(CacheName),
+ remove_cache(CacheName),
terminate(Reason, OtherCaches).
code_change (_, State, _) ->
@@ -116,28 +139,14 @@ handle_info(_, State) ->
{noreply, State}.
%%%% Interface Functions
-delete_cache (CacheList, DB) ->
- case lists:member(DB, CacheList) of
- true ->
- delete_cache(DB),
- lists:delete(DB, CacheList);
- false ->
- CacheList
- end.
+start () ->
+ gen_server:start(timed_caches_manager, [], []).
-add_cache (CacheList, DB, Heir) ->
- case lists:member(DB, CacheList) of
- true when (Heir =:= none) ->
- CacheList;
-
- true ->
- ets:setopts(DB, {heir, Heir, DB}),
- CacheList;
+new_cache (ManagerPid, DB, Heir) ->
+ gen_server:cast(ManagerPid, {add, DB, Heir}).
- false ->
- add_cache(DB, Heir),
- [DB|CacheList]
- end.
+delete_cache (ManagerPid, DB) ->
+ gen_server:cast(ManagerPid, {remove, DB}).
-get_timeout(_) ->
- 300000. % 5min.
+get_timeout () ->
+ 120000. % 2min.
diff --git a/src/query/character_turn.erl b/src/query/character_turn.erl
index f406efd..d6d68e1 100644
--- a/src/query/character_turn.erl
+++ b/src/query/character_turn.erl
@@ -58,14 +58,27 @@ parse_input (Req) ->
}.
fetch_data (Input) ->
- Battlemap = timed_cache:fetch(battlemap_db, Input#input.battlemap_id),
+ Battlemap =
+ timed_cache:fetch
+ (
+ battlemap_db,
+ Input#input.player_id,
+ Input#input.battlemap_id
+ ),
BattlemapInst =
timed_cache:fetch
(
battlemap_instance_db,
+ Input#input.player_id,
<<"0">>
),
- MainChar = timed_cache:fetch(character_db, Input#input.char_id),
+ MainChar =
+ timed_cache:fetch
+ (
+ character_db,
+ Input#input.player_id,
+ Input#input.char_id
+ ),
MainCharInst =
battlemap_instance:get_char_instance
(
@@ -78,7 +91,13 @@ fetch_data (Input) ->
TargetCharInst = nothing;
TargetID ->
- TargetChar = timed_cache:fetch(character_db, TargetID),
+ TargetChar =
+ timed_cache:fetch
+ (
+ character_db,
+ Input#input.player_id,
+ TargetID
+ ),
TargetCharInst =
battlemap_instance:get_char_instance
(
@@ -182,6 +201,7 @@ handle (Req) ->
(
battlemap_instance_db,
<<"0">>,
+ Input#input.player_id,
NQueryState#query_state.battlemap_inst
),
%%%% Reply
diff --git a/src/query/load_state.erl b/src/query/load_state.erl
index 9c28d2f..04e633e 100644
--- a/src/query/load_state.erl
+++ b/src/query/load_state.erl
@@ -109,20 +109,26 @@ handle (Req) ->
%%%% Parse
Input = parse_input(Req),
%%%% Fetch
- Battlemap = timed_cache:fetch(battlemap_db, Input#input.battlemap_id),
+ Battlemap =
+ timed_cache:fetch
+ (
+ battlemap_db,
+ Input#input.player_id,
+ Input#input.battlemap_id
+ ),
BattlemapInstance =
timed_cache:fetch
(
battlemap_instance_db,
+ Input#input.player_id,
<<"0">>
-% {Input#input.battlemap_id, Input#input.battlemap_instance_id}
),
Characters =
lists:map
(
fun ({CharID, CharInst}) ->
{
- timed_cache:fetch(character_db, CharID),
+ timed_cache:fetch(character_db, Input#input.player_id, CharID),
CharInst
}
end,