// coding: utf-8
// Copyright 2008 Grégory Burri
//
// This file is part of Euphorik.
//
// Euphorik is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Euphorik is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Euphorik. If not, see .
/*jslint laxbreak:true */
/**
* Permet de gérer les événements (push serveur).
* Principe de fonctionnement :
* - La page courante créer un objet euphorik.PageEvent en indiquant le nom de la page
* - La page courante attend un événement en appelant 'waitEvent' et en donnant deux fonctions :
* - 'funSend' une fonction qui renvoie les données à envoyer avant l'attente, par exemple {"dernierMess" : 23}
* "header" et "page" seront ajoutés aux données
* - 'funsReceive' un ensemble de fonctions à appeler en fonction du "reply" sur serveur
*
* l'information envoyé est sous la forme :
* {
* "header" : {"action" : "wait_event", "version" : },
* "page" :
* [..]
* }
* l'information reçu est sous la forme :
* {
* "reply" :
* [..]
* }
* @page la page courante pour laquelle on écoute des événements (un string)
* @util le helper 'util'
*/
euphorik.PageEvent = function(page, util) {
this.page = page;
this.util = util;
// l'objet JSONHttpRequest représentant la connexion d'attente
this.attenteCourante = undefined;
// le multhreading du pauvre, merci javascript de m'offrire autant de primitives pour la gestion de la concurrence...
this.stop = false;
};
/**
* Arrête l'attente courante s'il y en a une.
*/
euphorik.PageEvent.prototype.stopAttenteCourante = function() {
this.stop = true;
if (this.attenteCourante) {
this.attenteCourante.abort();
}
};
/**
* Attend un événement lié à la page.
* @funSend une fonction renvoyant les données json à envoyer
* @funsReceive est un objet comprenant les fonctions à appeler en fonction du "reply"
* les fonctions acceptent un paramètre correspondant au données reçues.
* exemple : {"new_message" : function(data){ ... }}
*/
euphorik.PageEvent.prototype.waitEvent = function(funSend, funsReceive) {
this.stopAttenteCourante();
this.stop = false;
var thisPageEvent = this;
// on doit conserver l'ordre des valeurs de l'objet JSON (le serveur les veut dans l'ordre définit dans le protocole)
// TODO : ya pas mieux ?
var dataToSend = {
"header" : { "action" : "wait_event", "version" : euphorik.conf.versionProtocole },
"page" : this.page
};
var poulpe = funSend();
objectEach(poulpe, function(k, v) {
dataToSend[k] = v;
});
this.attenteCourante = jQuery.ajax({
type: "POST",
url: "request",
dataType: "json",
// TODO : doit disparaitre
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
data: this.util.jsonVersAction(dataToSend),
success:
function(data) {
funsReceive[data.reply](data);
// rappel de la fonction dans 100 ms
setTimeout(function(){ thisPageEvent.waitEvent2(funSend, funsReceive); }, 100);
},
error:
function(XMLHttpRequest, textStatus, errorThrown) {
;; console.log("Connexion perdue dans PageEvent.prototype.waitEvent()");
setTimeout(function(){ thisPageEvent.waitEvent2(funSend, funsReceive); }, 1000);
}
});
};
/**
* Si un stopAttenteCourante survient un peu n'importe quand il faut imédiatement arreter de boucler.
*/
euphorik.PageEvent.prototype.waitEvent2 = function(funSend, funsReceive) {
if (this.stop) {
return;
}
this.waitEvent(funSend, funsReceive);
};