Browse files

Updated handlers.

  • Loading branch information...
1 parent 86aa43b commit 122c8f8cf96d8101590751bebe070843cfea8bcd @Licenser Licenser committed Feb 14, 2013
View
7 apps/snarl/src/snarl_zmq_handler.erl → apps/snarl/src/snarl_tcp_handler.erl
@@ -1,4 +1,4 @@
--module(snarl_zmq_handler).
+-module(snarl_tcp_handler).
-include("snarl.hrl").
@@ -126,6 +126,11 @@ message({user, revoke, User, Permission}, State) ->
message({user, revoke_all, User, Permission}, State) ->
{reply, snarl_user:revoke_all(ensure_binary(User), Permission), State};
+message({token, delete, Token}, State) when
+ is_binary(Token) ->
+ {reply, snarl_token:delete(Token), State};
+
+
%%%===================================================================
%%% Resource Functions
%%%===================================================================
View
10 apps/snarl/src/snarl_token.erl
@@ -9,7 +9,8 @@
-export([
ping/0,
get/1,
- add/1
+ add/1,
+ delete/1
]).
-ignore_xref([ping/0]).
@@ -37,9 +38,16 @@ add(User) ->
duplicate
end.
+delete(Token) ->
+ do_write(Token, delete).
+
+
%%%===================================================================
%%% Internal Functions
%%%===================================================================
+do_write(User, Op) ->
+ snarl_entity_write_fsm:write({snarl_token_vnode, snarl_token}, User, Op).
+
do_write(Token, Op, Val) ->
snarl_entity_write_fsm:write({snarl_token_vnode, snarl_token}, Token, Op, Val).
View
118 apps/snarl/src/snarl_token_vnode.erl
@@ -4,10 +4,11 @@
-include_lib("riak_core/include/riak_core_vnode.hrl").
-export([
- repair/3,
- get/3,
- add/4
- ]).
+ repair/3,
+ get/3,
+ delete/3,
+ add/4
+ ]).
-export([start_vnode/1,
init/1,
@@ -25,11 +26,12 @@
handle_exit/3]).
-ignore_xref([
- start_vnode/1,
- repair/3,
- get/3,
- add/4
- ]).
+ start_vnode/1,
+ repair/3,
+ get/3,
+ delete/3,
+ add/4
+ ]).
-define(TIMEOUT, 43200000).
@@ -39,15 +41,15 @@
-define(TIMEOUT_CYCLE, 100).
-record(state, {
- tokens,
- partition,
- node,
- cnt = 0,
- access_cnt = 0,
- timeout,
- timeout_limit,
- timeout_cycle
- }).
+ tokens,
+ partition,
+ node,
+ cnt = 0,
+ access_cnt = 0,
+ timeout,
+ timeout_limit,
+ timeout_cycle
+ }).
-define(MASTER, snarl_token_vnode_master).
@@ -71,19 +73,25 @@ repair(IdxNode, Token, Obj) ->
get(Preflist, ReqID, Token) ->
?PRINT({get, Preflist, ReqID, Token}),
riak_core_vnode_master:command(Preflist,
- {get, ReqID, Token},
- {fsm, undefined, self()},
- ?MASTER).
+ {get, ReqID, Token},
+ {fsm, undefined, self()},
+ ?MASTER).
%%%===================================================================
%%% API - writes
%%%===================================================================
add(Preflist, ReqID, Token, User) ->
riak_core_vnode_master:command(Preflist,
- {add, ReqID, Token, User},
- {fsm, undefined, self()},
- ?MASTER).
+ {add, ReqID, Token, User},
+ {fsm, undefined, self()},
+ ?MASTER).
+
+delete(Preflist, ReqID, Token) ->
+ riak_core_vnode_master:command(Preflist,
+ {delete, ReqID, Token},
+ {fsm, undefined, self()},
+ ?MASTER).
%%%===================================================================
%%% VNode
@@ -114,35 +122,41 @@ handle_command({get, ReqID, Token}, _Sender, #state{tokens = Tokens0} = State) -
?PRINT({handle_command, get, ReqID, Token}),
NodeIdx = {State#state.partition, State#state.node},
{Tokens1, Res} = case dict:find(Token, Tokens0) of
- error ->
- {Tokens0,
- {ok, ReqID, NodeIdx, not_found}};
- {ok, {_, V}} ->
- {dict:update(Token, fun({_, User}) ->
- {now(), User}
- end, Tokens0),
-
- {ok, ReqID, NodeIdx, V}}
- end,
- {reply,
+ error ->
+ {Tokens0,
+ {ok, ReqID, NodeIdx, not_found}};
+ {ok, {_, V}} ->
+ {dict:update(Token, fun({_, User}) ->
+ {now(), User}
+ end, Tokens0),
+
+ {ok, ReqID, NodeIdx, V}}
+ end,
+ {reply,
Res,
State#state{tokens = Tokens1}};
+handle_command({delete, {ReqID, _Coordinator}, Token}, _Sender, State) ->
+ {reply, {ok, ReqID},
+ State#state{
+ tokens = dict:erase(Token, State#state.tokens)
+ }};
+
handle_command({add, {ReqID, Coordinator}, Token, User}, _Sender, State) ->
T0 = statebox:new(fun snarl_token_state:new/0),
T1 = statebox:modify({fun snarl_token_state:user/2, [User]}, T0),
-
+
VC0 = vclock:fresh(),
VC = vclock:increment(Coordinator, VC0),
TObject = #snarl_obj{val=T1, vclock=VC},
State1 = expire(State),
Ts0 = dict:store(Token, {now(), TObject}, State1#state.tokens),
-
+
{reply, {ok, ReqID, Token}, State1#state{
- tokens = Ts0,
- access_cnt = State1#state.access_cnt + 1,
- cnt = State1#state.cnt + 1
- }};
+ tokens = Ts0,
+ access_cnt = State1#state.access_cnt + 1,
+ cnt = State1#state.cnt + 1
+ }};
handle_command(Message, _Sender, State) ->
?PRINT({unhandled_command, Message}),
@@ -171,17 +185,17 @@ encode_handoff_item(Token, {_, Data}) ->
is_empty(State) ->
case dict:size(State#state.tokens) of
- 0 ->
- {true, State};
- _ ->
- {true, State}
+ 0 ->
+ {true, State};
+ _ ->
+ {true, State}
end.
delete(State) ->
{ok, State#state{tokens = dict:new()}}.
handle_coverage({list, ReqID}, _KeySpaces, _Sender, State) ->
- {reply,
+ {reply,
{ok, ReqID, {State#state.partition,State#state.node}, dict:fetch_keys(State#state.tokens)},
State};
@@ -201,10 +215,10 @@ terminate(_Reason, _State) ->
dflt_env(N, D) ->
case application:get_env(N) of
- undefined ->
- D;
- {ok, V} ->
- V
+ undefined ->
+ D;
+ {ok, V} ->
+ V
end.
expire(#state{cnt = Cnt, timeout_limit = Limit} = State) when Limit < Cnt ->
@@ -214,8 +228,8 @@ expire(#state{access_cnt = Cnt, timeout_cycle = Cycle} = State) when Cycle < Cnt
expire(#state{tokens = Tokens, timeout = Timeout} = State) ->
Tokens1 = dict:filter(fun(_K, {Timer, _V}) ->
- timer:now_diff(Timer, now()) =< Timeout
- end, Tokens),
+ timer:now_diff(Timer, now()) =< Timeout
+ end, Tokens),
State#state{
access_cnt = 0,
tokens = Tokens1,
View
3 apps/snarl/src/snarl_user_vnode.erl
@@ -232,9 +232,10 @@ handle_command({add, {ReqID, Coordinator}, UUID, User}, _Sender, State) ->
User0 = statebox:new(fun snarl_user_state:new/0),
User1 = statebox:modify({fun snarl_user_state:name/2, [User]}, User0),
User2 = statebox:modify({fun snarl_user_state:uuid/2, [UUID]}, User1),
+ User3 = statebox:modify({fun snarl_user_state:grant/2, [[<<"user">>, UUID, <<"...">>]]}, User2),
VC0 = vclock:fresh(),
VC = vclock:increment(Coordinator, VC0),
- UserObj = #snarl_obj{val=User2, vclock=VC},
+ UserObj = #snarl_obj{val=User3, vclock=VC},
snarl_db:put(State#state.db, <<"user">>, UUID, UserObj),
{reply, {ok, ReqID}, State};
View
2 rel/files/app.config
@@ -7,7 +7,7 @@
{service, "snarl"},
{ip, "{{snarl_ip}}"},
{port, {{snarl_port}} },
- {handler, snarl_zmq_handler}
+ {handler, snarl_tcp_handler}
]},
{lager,
[{handlers, [

0 comments on commit 122c8f8

Please sign in to comment.