/** * Project  : AMDA-NG * Name : SampModule.js * @class amdaDesktop.SampModule * @extends amdaDesktop.SampModule * @brief Samp Module controller definition * @author Benjamin RENARD * $Id: SampModule.js 1698 2013-07-17 10:54:37Z benjamin $ ***************************************************************************** * FT Id : Date : Name - Description ******************************************************************************* * 23/04/2012: BRE - file creation */ Ext.define('amdaDesktop.SampModule', { extend: 'Ext.util.Observable', connector : null, ready : false, constructor: function() { this.addEvents({ "connected" : true, "disconnected" : true, "clientregister" : true, "clientunregister" : true, "clientmetachange" : true, "clientsubs" : true, "uploadfile" : true }); this.callParent(arguments); }, destroy: function() { if (this.isConnected()) this.disconnect(); }, loadScript : function(successfn) { var me = this; if (this.ready) { successfn.call(me); return; } var connectorURL = Ext.Loader.getPath('samp')+"/jsamp/samp.js"; var onLoadConnector = function () { var href = window.location.href; var baseurl = href.replace('desktop.php',''); var iconURL = baseurl+'js/resources/images/amda_samp.jpg'; var meta = { "samp.name": "AMDA", "samp.description.text": "AMDA (Automated Multi Dataset Analysis) : Versatile web tool for space physics", "samp.icon.url": iconURL }; var cc = new samp.ClientTracker(); var callHandler = cc.callHandler; callHandler["samp.app.ping"] = function(senderId, message, isCall) { if (isCall) { return {text: "ping to you, " + cc.getName(senderId)}; } }; callHandler["table.load.votable"] = function(senderId, message, isCall) { var clientName = cc.getName(senderId); var url = message["samp.params"]["url"]; me.receiveFile(clientName, url, "votable"); }; callHandler["table.load.cdf"] = function(senderId, message, isCall) { var clientName = cc.getName(senderId); var url = message["samp.params"]["url"]; me.receiveFile(clientName, url, "cdf"); }; callHandler["table.load.netcdf"] = function(senderId, message, isCall) { var clientName = cc.getName(senderId); var url = message["samp.params"]["url"]; me.receiveFile(clientName, url, "netcdf"); }; callHandler["samp.hub.event.shutdown"] = function(senderId, message, isCall) { me.disconnect(); }; cc.onchange = function(id, type, data) { switch (type) { case 'register' : me.fireEvent('clientregister',me,id); break; case 'unregister' : me.fireEvent('clientunregister',me,id); break; case 'meta' : me.fireEvent('clientmetachange',me,id,data,cc.subs[id]); break; case 'subs' : me.fireEvent('clientsubs',me,id,data); break; } }; logCc = { receiveNotification: function(senderId, message) { var handled = cc.receiveNotification(senderId, message); //console.log("notification: " + message["samp.mtype"] + // " from " + cc.getName(senderId)); }, receiveCall: function(senderId, msgId, message) { var handled = cc.receiveCall(senderId, msgId, message); //console.log("call: " + message["samp.mtype"] + // " from " + cc.getName(senderId)); }, receiveResponse: function(responderId, msgTag, response) { var handled = cc.receiveResponse(responderId, msgTag, response); // ("response: " + msgTag + // " from " + cc.getName(responderId)); }, init: function(connection) { cc.init(connection); } }; var subs = cc.calculateSubscriptions(); me.connector = new samp.Connector("AMDA", meta, logCc, subs); me.connector.onreg = function(conn) { me.fireEvent('connected',me,true); }; me.connector.onunreg = function(conn) { me.fireEvent('disconnected',me,true); }; me.connector.onregerror = function(message) { me.showStartHubMsg(); }; me.ready = true; successfn.call(me); }; var onErrorConnector = function () { myDesktopApp.errorMsg("Cannot load jsamp script"); me.fireEvent('connected',me,false); }; Ext.Loader.injectScriptElement(connectorURL, onLoadConnector, onErrorConnector, me); }, showStartHubMsg : function() { var msg = "Cannot connect to a SAMP hub.
"; msg += 'You can use Java WebStart to start a JSAMP hub by just clicking here: start hub
'; msg += 'Or use internal hub included in tools like TOPCAT or Aladin'; myDesktopApp.errorMsg(msg); }, connect : function() { if (!this.connector || !this.ready) return false; try { this.connector.register(); return true; } catch(err) { myDesktopApp.errorMsg(err); return false; } }, disconnect : function() { if (!this.connector || !this.ready) return false; try { this.connector.unregister(); this.fireEvent('disconnected',this,true); return true; } catch(err) { myDesktopApp.errorMsg(err); return false; } }, isConnected : function() { if (!this.connector || !this.ready) return false; try { return this.connector.connection; } catch(err) { myDesktopApp.errorMsg(err); return false; } }, notififyMessage : function(message,clientId) { if (!this.connector || !this.ready) return false; try { if (!clientId || clientId == 'hub') this.connector.connection.notifyAll([message]); else this.connector.connection.notify([clientId,message]); return true; } catch(err) { myDesktopApp.errorMsg(err); return false; } }, sendVOTable : function(url,clientId) { var message = new samp.Message('table.load.votable', {"url": url}); return this.notififyMessage(message,clientId); }, sendFITS : function(url,name) { var message = new samp.Message('image.load.fits', {"url": url, "name": name}); return this.notififyMessage(message); }, sendAladinScript : function(ascript) { var message = new samp.Message('script.aladin.send', {"script": ascript}); return this.notififyMessage(message); }, getSubscribedClients : function(mType) { if (!this.connector || !this.ready) return []; return this.connector.connection.getSubscribedClients([mType]); }, isSubscribed : function(subs, mtype) { if (!this.connector || !this.ready) return []; return samp.isSubscribed(subs, mtype); }, receiveFile : function(clientName, url, format) { this.fireEvent('uploadfile',this,clientName, url, format); }, loadFile : function(url, onLoad, onError) { if (!this.connector || !this.ready) { if (onError) onError("Not connected to a SAMP hub"); return; } var proxifiedUrl = this.connector.connection.translateUrl(url); var xobj = null; if ((typeof XMLHttpRequest != "undefined") && (typeof FileReader != "undefined")) { xobj = new XMLHttpRequest(); } else { if (onError) onError("Not supported by your browser"); return; } xobj.open('GET', proxifiedUrl, true); xobj.responseType = "blob"; xobj.onload = function() { var blob = xobj.response; var reader = new FileReader(); reader.addEventListener("load", function () { if (onLoad) onLoad(reader.result); }, false); reader.addEventListener("error", function () { if (onError) onError("Cannot load data received from SAMP"); }, false); reader.readAsText(blob,"UTF-8"); }; xobj.onerror = function () { if (onError) onError("An error occurred during the SAMP request"); }; xobj.send(null); return true; }, });