Skip to content
Browse files

Choose avatar after login

  • Loading branch information...
1 parent b7a6e56 commit 3b0c46c0d25150884ccd23ad54011a183d24166f @brendonh committed Feb 20, 2011
Showing with 85 additions and 27 deletions.
  1. +34 −14 gateway/sandbox.erl
  2. +29 −6 gateway/src/rkgate_conn.erl
  3. +22 −7 gateway/src/rkgate_login.erl
View
48 gateway/sandbox.erl
@@ -1,11 +1,11 @@
#! /usr/bin/env escript
-%%! -smp enable -pa ebin -pa ../deps/bson/ebin -boot start_sasl
+%%! -smp enable -pa ebin -pa ../deps/bson/ebin
main([]) ->
Options = [binary, {packet, 2}, {active, true}],
{ok, Socket} = gen_tcp:connect(localhost, 13770, Options),
- Doc = {username, <<"brendonh">>, password, <<"arthur">>},
+ Doc = {username, <<"brendonh">>, password, <<"arthur">>, rpcid, 1234},
BSON = bson_binary:put_document(Doc),
@@ -15,20 +15,40 @@ main([]) ->
gen_tcp:send(Socket, Packet),
- flush().
+ {ok, User} = get_rpc_reply(1234),
+
+ {[Avatar|_Rest]} = bson:lookup(avatars, User),
+
+ {AvatarID} = bson:lookup('_id', Avatar),
+
+ io:format("Avatar ID: ~p~n", [AvatarID]),
+
+ BSON2 = bson_binary:put_document({id, AvatarID, rpcid, 5432}),
+
+ Packet2 = <<2:2/integer-big-unsigned-unit:8, BSON2/binary>>,
+
+ gen_tcp:send(Socket, Packet2),
+
+ {ok, StatusDoc} = get_rpc_reply(5432),
+
+ io:format("Avatar reply: ~p~n", [StatusDoc]),
+
+ %receive after 1000 -> ok end,
+ ok.
-flush() ->
+get_rpc_reply(ID) ->
receive
- {tcp, _Port, Bin} ->
- <<Type:2/integer-big-unsigned-unit:8, Content/binary>> = Bin,
+ {tcp, _Port, Bin}=Message ->
+ <<1:2/integer-big-unsigned-unit:8, Content/binary>> = Bin,
{Doc, <<>>} = bson_binary:get_document(Content),
- io:format("Message ~p: ~p~n", [Type, bson:fields(Doc)]);
- Any ->
- io:format("Unknown: ~p~n", [Any]),
- flush()
- after
- 2000 ->
- true
- end.
+ case bson:lookup(rpcid, Doc) of
+ {ID} -> {ok, Doc};
+ _ ->
+ self() ! Message,
+ get_rpc_reply(ID)
+ end
+ after 2000 ->
+ io:format("Timeout~n")
+ end.
View
35 gateway/src/rkgate_conn.erl
@@ -20,7 +20,9 @@
-record(state, {
socket,
- userID=none
+ userID=none,
+ avatar=none,
+ space=none
}).
%%====================================================================
@@ -115,24 +117,44 @@ handle_message(login, Content, #state{userID=none}=State) ->
{Un, Pw} when is_binary(Un) andalso is_binary(Pw) ->
{ok, UserDoc} = rkgate_login:login(Username, Password),
- {{UID}} = bson:lookup('_id', UserDoc),
+ {UID} = bson:lookup('_id', UserDoc),
- ?DBG({login, Username, rkgate_util:format_oid(UID)}),
+ ?DBG({login, Username}),
NewState = State#state{userID=UID},
% XXX TODO: Generalize safety
- Avatars = [bson:include(['_id', name], A) ||
+ Avatars = [bson:include(['_id', name, location], A) ||
A <- rkgate_login:get_avatars(UID)],
- rpc_reply(Doc, [{uid, rkgate_util:format_oid(UID)},
- {avatars, Avatars}], NewState),
+ rpc_reply(Doc, [{avatars, Avatars}], NewState),
{noreply, NewState};
_ ->
{stop, normal, State}
end;
+handle_message(avatar, Content, #state{avatar=none}=State) ->
+ {Doc, <<>>} = bson_binary:get_document(Content),
+ {AvatarID} = bson:lookup(id, Doc),
+ case AvatarID of
+ X when is_binary(X) ->
+ {ok, AvatarDoc} = rkgate_login:avatar(
+ State#state.userID, AvatarID),
+ {AID} = bson:lookup('_id', AvatarDoc),
+ ?DBG({avatar, AID}),
+ NewState = State#state{avatar=AID},
+ rpc_reply(Doc, [{success, 1}], NewState),
+ {noreply, NewState};
+ _ ->
+ {stop, normal, State}
+ end;
+
+
handle_message(login, _Content, State) ->
?DBG(repeated_login_attempt),
+ {noreply, State};
+
+handle_message(avatar, _Content, State) ->
+ ?DBG(repeated_avatar_attempt),
{noreply, State}.
@@ -167,5 +189,6 @@ rpc_reply(RPCDoc, Reply, State) ->
%% XXX TODO: Not this!
lookup_type(0) -> login;
lookup_type(1) -> rpc_reply;
+lookup_type(2) -> avatar;
lookup_type(_) -> unknown.
View
29 gateway/src/rkgate_login.erl
@@ -9,15 +9,14 @@
-include("rkgate_util.hrl").
--export([login/2, get_avatars/1]).
+-export([login/2, get_avatars/1, avatar/2]).
-define(QUERY(T, P), rkgate_mongo:find(T, P)).
login(Username, Password) ->
- case ?QUERY(reckoner.users, {username, Username}) of
+ case rkgate_mongo:find(reckoner.users, {'_id', Username}) of
[UserDoc] ->
{Pass} = bson:lookup(password, UserDoc),
- ?DBG({checking, Pass, Password}),
case verify_password(Pass, Password) of
true ->
{ok, UserDoc};
@@ -31,9 +30,25 @@ login(Username, Password) ->
{error, multiple_results}
end.
-get_avatars(UID) ->
- ?QUERY(reckoner.avatars, {owner, {UID}}).
-
verify_password(Real, Real) -> true;
verify_password(_, _) -> false.
-
+
+get_avatars(UID) ->
+ rkgate_mongo:find(reckoner.avatars, {owner, UID}).
+
+
+avatar(UserID, AvatarID) ->
+ case rkgate_mongo:find(reckoner.avatars, {'_id', AvatarID}) of
+ [AvatarDoc] ->
+ {Owner} = bson:lookup(owner, AvatarDoc),
+ case Owner of
+ UserID ->
+ {ok, AvatarDoc};
+ Other ->
+ ?DBG({invalid_avatar, Other}),
+ {error, not_owner}
+ end;
+ Other ->
+ ?DBG({not_found, Other}),
+ {error, not_found}
+ end.

0 comments on commit 3b0c46c

Please sign in to comment.
Something went wrong with that request. Please try again.