Yariv Sadan wrote:

> Yeah, it looks similar in the way it uses processes for user sessions.
> Unfortunately, the documentation doesn't say much about how to use it
> to build full apps.

I know your code is really only a proof of concept, but I noticed that 
your gen_key() function doesn't test for key collisions. The code I've 
been testing is a little more complicated. It walks the mnesia table to 
find the max id and adds one then encodes to/from a modified base64. The 
modification to base64 is to replace $+ with $- and $/ with $_. 
Encryption/decryption of the integer id is added to avoid making the ids 
guessable, but this is frankly all a little heavy handed.

I wont post all the code as there's about three or four versions/ways of 
doing things in the same file and not all of it works at the moment as I 
keep changing my mind on how to approach problems. Anyway, use anything 
that's proves useful.

Really it comes down to how likely a key/id collision is and how to 
guarrantee that a unique key can be generated in near O(1) time for a 
large number of sessions.


Jeff.
-------------
%% creates a new state record with a unique id
new(SessionId, OldId) when OldId =:= undefined orelse is_integer(OldId) ->
     Now = now(),
     F = fun() ->
                 MaxId = mnesia:foldl(fun(Elem, Max) ->
                                              case Elem#wstate.id of
                                                  M when M > Max -> M;
                                                  _ -> Max
                                              end
                                      end, 0, wstate),
                 WS = #wstate{id              = MaxId + 1,
                              session_id      = SessionId,
                              proceededby     = OldId,
                              created         = Now,
                              last_touched    = Now,
                              values          = undefined
                             },
                 mnesia:write(wstate, WS, write),
                 WS
         end,
     {atomic, Rec} = mnesia:transaction(F),
     Rec.


%% Given the numeric id encodes for use in web pages.
encode(undefined) ->
     undefined;
encode(Id) ->
     IdBin = <<Id:128/integer-big>>,
     IVec = crypto:rand_bytes(16),
     IdCrypt = crypto:aes_cbc_128_encrypt(?KEY, IVec, IdBin),
     MagicStr = <<IVec/binary, IdCrypt/binary>>,
     B64 = http_base_64:encode(binary_to_list(MagicStr)),
     sub_slash_and_plus(B64).

sub_slash_and_plus(Str) when is_list(Str) ->
     lists:map(fun(Elem) ->
                       case Elem of
                           $+ -> $-;
                                     $/ -> $_;
                                     L  -> L
                              end
               end, Str).



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"erlyweb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to