X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=modules%2Ferl%2Feuphorik_protocole.erl;h=cdd0f6b6b36dafcd52310c9f283268293f0c97d3;hp=fcce9c345131bf55e8f342e80ed5c3ffd20bb802;hb=8bec0dac79e750d0040de8a009c6ae864479642e;hpb=a2f6d159387e456847fb4e7607b4663ab8fa94fc diff --git a/modules/erl/euphorik_protocole.erl b/modules/erl/euphorik_protocole.erl index fcce9c3..cdd0f6b 100755 --- a/modules/erl/euphorik_protocole.erl +++ b/modules/erl/euphorik_protocole.erl @@ -66,10 +66,9 @@ loginUser({ok, User}, IP) -> euphorik_bd:update_date_derniere_connexion(User#user.id), json_reponse_login_ok(User); loginUser(_, _) -> - % ajoute un délais d'attente (TODO : un autre moyen plus élégant ?) - receive after 1000 -> - erreur("Erreur login") - end. + % ajoute un délais d'attente + timer:sleep(1000), + erreur("Erreur login"). % Renvoie un string() représentant un cookie en base 36. Il y a 10^32 possibillités. @@ -124,12 +123,14 @@ profile( % Renvoie les messages appropriés. % last_message id et cookie sont facultatifs wait_event([{page, "chat"} | Data]) -> + % traitement des inputs Cookie = case lists:keysearch(cookie, 1, Data) of {value, {_, C}} -> C; _ -> inconnu end, Last_message_id = case lists:keysearch(last_message_id, 1, Data) of {value, {_, Id}} -> Id; _ -> 0 end, {value, {_, Message_count}} = lists:keysearch(message_count, 1, Data), Main_page = case lists:keysearch(main_page, 1, Data) of {value, {_, P}} -> P; _ -> 1 end, + Troll_id = case lists:keysearch(troll_id, 1, Data) of {value, {_, T}} -> T; _ -> 0 end, {value, {_, {array, Conversations_json}}} = lists:keysearch(conversations, 1, Data), - Conversations = lists:map( + Racines_conversations = lists:map( fun({struct, [{root, Racine}, {page, Page} | Reste]}) -> Last_mess_conv = case Reste of [{last_message_id, L}] -> L; _ -> 0 end, {Racine, Page, Last_mess_conv} @@ -139,60 +140,19 @@ wait_event([{page, "chat"} | Data]) -> User = case euphorik_bd:user_by_cookie(Cookie) of {ok, U} -> U; _ -> inconnu - end, - {struct, [ - {reply, "new_message"}, - {conversations, {array, - % accrochez-vous ca va siouxer ;) - lists:map( - fun({Conv, Plus}) -> - {struct, [ - {last_page, not Plus}, - {messages, {array, - lists:map( - fun({Mess, Repond_a}) -> - Est_proprietaire = User =/= inconnu andalso User#user.id =:= Mess#minichat.auteur_id, - A_repondu_a_message = User =/= inconnu andalso euphorik_bd:a_repondu_a_message(User#user.id, Mess#minichat.id), - Est_une_reponse_a_user = User =/= inconnu andalso euphorik_bd:est_une_reponse_a_user(User#user.id, Mess#minichat.id), - {ok, User_mess } = euphorik_bd:user_by_id(Mess#minichat.auteur_id), - {struct, [ - {id, Mess#minichat.id}, - {user_id, User_mess#user.id}, - {date, format_date(Mess#minichat.date)}, - {system, Mess#minichat.auteur_id =:= 0}, - {owner, Est_proprietaire}, - {answered, A_repondu_a_message}, - {is_a_reply, Est_une_reponse_a_user}, - {nick, Mess#minichat.pseudo}, - {login, User_mess#user.login}, - {content, Mess#minichat.contenu}, - {answer_to, {array, lists:map( - fun(Id_mess) -> - {ok, M} = euphorik_bd:message_by_id(Id_mess), - {ok, User_reponse} = euphorik_bd:user_by_mess(M#minichat.id), - {struct, [{id, M#minichat.id}, {nick, M#minichat.pseudo}, {login, User_reponse#user.login}]} - end, - Repond_a - )}}, - {ek_master, User_mess#user.ek_master} - ]} - end, - Conv - ) - }} - ]} - end, - euphorik_minichat_conversation:conversations( - Conversations, - Message_count, - Last_message_id, - Main_page - ) - ) - }} - ]}; + end, + case {mnesia:subscribe({table, minichat, detailed}), mnesia:subscribe({table, troll, detailed})} of + {{error, E}, _} -> E; + {_, {error, E}} -> E; + _ -> + % attente d'événements + R = wait_event_page_chat(User, Racines_conversations, Message_count, Last_message_id, Main_page, Troll_id), + mnesia:unsubscribe({table, minichat, detailed}), + mnesia:unsubscribe({table, troll, detailed}), + R + end; wait_event([{page, "admin"}, {last_troll, Last_troll}]) -> - case euphorik_bd:trolls(Last_troll) of + case euphorik_bd:trolls_attente(Last_troll) of {mod, Troll} -> {struct, [ @@ -235,7 +195,91 @@ wait_event([{page, "admin"}, {last_troll, Last_troll}]) -> end; wait_event(_) -> erreur("Page inconnue"). - + + +wait_event_page_chat(User, Racines_conversations, Message_count, Last_message_id, Main_page, Troll_id) -> + % est-ce qu'il y a des nouveaux messages ? + case euphorik_minichat_conversation:conversations(Racines_conversations, Message_count, Last_message_id, Main_page) of + vide -> + % est-ce que le troll est à jour ? + case euphorik_bd:current_troll() of + Current when is_record(Current, troll), Current#troll.id =/= Troll_id -> + {struct, [ + {reply, "new_troll"}, + {troll_id, Current#troll.id}, + {content, Current#troll.content} + ]}; + _ -> + wait_event_bd_page_chat(), + % TODO : l'appel est-il bien tail-recursive ? + wait_event_page_chat(User, Racines_conversations, Message_count, Last_message_id, Main_page, Troll_id) + end; + Conversations -> + % accrochez-vous ca va siouxer ;) + {struct, [ + {reply, "new_messages"}, + {conversations, {array, + lists:map( + fun({Conv, Plus}) -> + {struct, [ + {last_page, not Plus}, + {messages, {array, + lists:map( + fun({Mess, Repond_a}) -> + Est_proprietaire = User =/= inconnu andalso User#user.id =:= Mess#minichat.auteur_id, + A_repondu_a_message = User =/= inconnu andalso euphorik_bd:a_repondu_a_message(User#user.id, Mess#minichat.id), + Est_une_reponse_a_user = User =/= inconnu andalso euphorik_bd:est_une_reponse_a_user(User#user.id, Mess#minichat.id), + {ok, User_mess } = euphorik_bd:user_by_id(Mess#minichat.auteur_id), + {struct, [ + {id, Mess#minichat.id}, + {user_id, User_mess#user.id}, + {date, format_date(Mess#minichat.date)}, + {system, Mess#minichat.auteur_id =:= 0}, + {owner, Est_proprietaire}, + {answered, A_repondu_a_message}, + {is_a_reply, Est_une_reponse_a_user}, + {nick, Mess#minichat.pseudo}, + {login, User_mess#user.login}, + {content, Mess#minichat.contenu}, + {answer_to, {array, lists:map( + fun(Id_mess) -> + {ok, M} = euphorik_bd:message_by_id(Id_mess), + {ok, User_reponse} = euphorik_bd:user_by_mess(M#minichat.id), + {struct, [{id, M#minichat.id}, {nick, M#minichat.pseudo}, {login, User_reponse#user.login}]} + end, + Repond_a + )}}, + {ek_master, User_mess#user.ek_master} + ]} + end, + Conv + ) + }} + ]} + end, + Conversations + ) + }} + ]} + end. + + +% Attend un événement lié à la page 'chat'. +wait_event_bd_page_chat() -> + receive % attente d'un post + {mnesia_table_event, {write, minichat, _Message, [], _}} -> + ok; + {mnesia_table_event, {write, troll, Troll, [Old_troll | _], _}} when Troll#troll.date_post =/= undefined, Old_troll#troll.date_post == undefined -> + ok; + _ -> + wait_event_bd_page_chat() + % 60 minutes de timeout (le cas ou il n'y a que des consultations et jamais de post) + % Après 60 minutes de connexion, le client doit donc reétablir une connexion + % TODO : pour être mieux : quand le socket se ferme alors un message devrait être envoyé et débloquer ce receive (demande en cours sur la mailing list de yaws) + after 1000 * 60 * 60 -> + timeout + end. + % Un utilisateur envoie un message put_message(