/*
 * anet.Rpc
 * Objet de gestion de la communication client/serveur.
 */
anet.Rpc = function(url, distantObjectName, callbackObjectName) {
	// URL de connexion au serveur
	this._url = url;
	// Nom de l'objet distant sur lequel se connecter
	this._distantObjectName = distantObjectName;
	// Nom de l'objet local servant a la reponse
	this._callbackObjectName = (callbackObjectName && callbackObjectName.length) ? callbackObjectName : distantObjectName;
	// Duree du timeout
	this._timeout = null;
	// session statique
	this._staticSession = null;
	// code a executer pour recuperer dynamiquement la session (chaine de caractere interpretable)
	this._dynamicSession = null;
	// code a executer a chaque fois qu'une communication commence
	this._startCommunicationCallback = null;
	// code a executer a chaque fois qu'une communication se termine
	this._stopCommunicationCallback = null;
};

anet.Rpc.prototype = {
	setTimeout: function(timeout) {
		this._timeout = timeout;
	},
	setStaticSession: function(staticSession) {
		this._staticSession = staticSession;
	},
	setDynamicSession: function(dynamicSession) {
		this._dynamicSession = dynamicSession;
	},
	setCommunicationCallbacks: function(startHdlr, stopHdlr) {
		this._startCommunicationCallback = startHdlr;
		this._stopCommunicationCallback = stopHdlr;
	},
	call: function(method, serverParams, clientData) {
		// constitution du JSON d'envoi de donnees
		var data = {
			object:		this._distantObjectName,
			method:		method,
			session:	(this._dynamicSession && this._dynamicSession.length) ?
						eval(this._dynamicSession.toString()) :
						this._staticSession.toString(),
			params:		serverParams
		};
		requestData = encodeURIComponent(data.toJSONString());
		// creation des la connexion
		var index = anet.ajax._createConnection();
		anet.ajax._connections[index].rpc = this;
		anet.ajax._connections[index].handler = method;
		anet.ajax._connections[index].data = clientData;
		anet.ajax._connections[index].http.open("POST", this._url, true);
		anet.ajax._connections[index].http.setRequestHeader("Content-length", requestData.length);
		anet.ajax._connections[index].http.setRequestHeader("Connection", "close");
		// gestion du timeout
		if (this._timeout)
			anet.ajax._connections[index].timer = setTimeout(function() {
				anet.ajax._abortConnection(index);
			}, this._timeout * 1000);
		// traitement de la communication
		anet.ajax._connections[index].http.onreadystatechange = function() {
			var http = anet.ajax._connections[index].http;
			if (http.readyState != 4)
				return;
			// fin de communication
			//if (anet.ajax._connections[index].rpc._stopCommunicationCallback)
			//	eval(anet.ajax._connections[index].rpc._stopCommunicationCallback);
			if (anet.ajax._connections[index].timer)
				clearTimeout(anet.ajax._connections[index].timer);
			// on effectue les traitements si on a defini un objet local
			if (anet.ajax._connections[index].rpc._callbackObjectName) {
				// constitution du JSON de recuperation des donnees
				var json = http.responseText.parseJSON();
				var callbackData = {
					HTTP:		http.status,
					method:		anet.ajax._connections[index].handler,
					data:		anet.ajax._connections[index].data,
					error:		json.error,
					response:	json.response
				};
				anet.ajax._connections[index].callbackData = callbackData;
				// gestion des erreurs de communication
				if (http.status == 200 && !json.error) {
					// tout s'est bien passe, on appelle la methode de l'objet de retour
					eval(anet.ajax._connections[index].rpc._callbackObjectName + "." +
					     anet.ajax._connections[index].handler + "Response" +
					     "(anet.ajax._connections[" + index + "].callbackData)");
				} else if (eval(anet.ajax._connections[index].rpc._callbackObjectName + ".error")) {
					eval(anet.ajax._connections[index].rpc._callbackObjectName +
					     ".error(anet.ajax._connections[" + index + "].callbackData)");
				}
/*
				} else if (anet.ajax._connections[index].rpc._errorCallbackName) {
					// erreur durant la communication, on appelle la methode d'erreur
					eval(anet.ajax._connections[index].rpc._callbackObjectName + "." +
					     anet.ajax._connections[index].rpc._errorCallbackName +
					     "(anet.ajax._connections[" + index + "].callbackData)");
				}
*/
			}
			anet.ajax._deleteConnection(index);
		};
		// debut de communication
		//if (this._startCommunicationCallback)
		//	eval(this._startCommunicationCallback);
		anet.ajax._connections[index].http.send(requestData);
	}
};
