(no commit message)
[euphorik.git] / js / comet.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18
19 /*jslint laxbreak:true */
20
21 /**
22 * Permet de gérer les événements (push serveur).
23 * Principe de fonctionnement :
24 * - La page courante créer un objet euphorik.PageEvent en indiquant le nom de la page
25 * - La page courante attend un événement en appelant 'waitEvent' et en donnant deux fonctions :
26 * - 'funSend' une fonction qui renvoie les données à envoyer avant l'attente, par exemple {"dernierMess" : 23}
27 * "header" et "page" seront ajoutés aux données
28 * - 'funsReceive' un ensemble de fonctions à appeler en fonction du "reply" sur serveur
29 *
30 * l'information envoyé est sous la forme :
31 * {
32 * "header" : {"action" : "wait_event", "version" : <v> },
33 * "page" : <page>
34 * [..]
35 * }
36 * l'information reçu est sous la forme :
37 * {
38 * "reply" : <reply>
39 * [..]
40 * }
41 * @page la page courante pour laquelle on écoute des événements (un string)
42 * @util le helper 'util'
43 */
44 euphorik.PageEvent = function(page, util) {
45 this.page = page;
46 this.util = util;
47
48 // l'objet JSONHttpRequest représentant la connexion d'attente
49 this.attenteCourante = undefined;
50
51 // le multhreading du pauvre, merci javascript de m'offrire autant de primitives pour la gestion de la concurrence...
52 this.stop = false;
53 };
54
55 /**
56 * Arrête l'attente courante s'il y en a une.
57 */
58 euphorik.PageEvent.prototype.stopAttenteCourante = function() {
59 this.stop = true;
60
61 if (this.attenteCourante) {
62 this.attenteCourante.abort();
63 }
64 };
65
66 /**
67 * Attend un événement lié à la page.
68 * @funSend une fonction renvoyant les données json à envoyer
69 * @funsReceive est un objet comprenant les fonctions à appeler en fonction du "reply"
70 * les fonctions acceptent un paramètre correspondant au données reçues.
71 * exemple : {"new_message" : function(data){ ... }}
72 */
73 euphorik.PageEvent.prototype.waitEvent = function(funSend, funsReceive) {
74 this.stopAttenteCourante();
75
76 this.stop = false;
77
78 var thisPageEvent = this;
79
80 // on doit conserver l'ordre des valeurs de l'objet JSON (le serveur les veut dans l'ordre définit dans le protocole)
81 // TODO : ya pas mieux ?
82 var dataToSend = {
83 "header" : { "action" : "wait_event", "version" : euphorik.conf.versionProtocole },
84 "page" : this.page
85 };
86 var poulpe = funSend();
87 objectEach(poulpe, function(k, v) {
88 dataToSend[k] = v;
89 });
90
91 this.attenteCourante = jQuery.ajax({
92 type: "POST",
93 url: "request",
94 dataType: "json",
95 // TODO : doit disparaitre
96 timeout: 180000, // timeout de 3min. Gros HACK pas beau. FIXME problème décrit ici : http://groups.google.com/group/jquery-en/browse_thread/thread/8724e64af3333a76
97 data: this.util.jsonVersAction(dataToSend),
98 success:
99 function(data) {
100 funsReceive[data.reply](data);
101
102 // rappel de la fonction dans 100 ms
103 setTimeout(function(){ thisPageEvent.waitEvent2(funSend, funsReceive); }, 100);
104 },
105 error:
106 function(XMLHttpRequest, textStatus, errorThrown) {
107 ;; console.log("Connexion perdue dans PageEvent.prototype.waitEvent()");
108 setTimeout(function(){ thisPageEvent.waitEvent2(funSend, funsReceive); }, 1000);
109 }
110 });
111 };
112
113 /**
114 * Si un stopAttenteCourante survient un peu n'importe quand il faut imédiatement arreter de boucler.
115 */
116 euphorik.PageEvent.prototype.waitEvent2 = function(funSend, funsReceive) {
117 if (this.stop) {
118 return;
119 }
120 this.waitEvent(funSend, funsReceive);
121 };