Blame view

js/app/controllers/SampModule.js 7.96 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/** 
 * 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.<br/>";
		msg += 'You can use Java WebStart to start a JSAMP hub by just clicking here: <a href="http://astrojs.github.io/sampjs/hub/webhub.jnlp">start hub</a><br/>';
		msg += 'Or use internal hub included in tools like <a href="http://www.star.bris.ac.uk/~mbt/topcat/topcat-full.jnlp">TOPCAT</a> or <a href="http://aladin.u-strasbg.fr/java/nph-aladin.pl?frame=get&id=aladin.jnlp">Aladin</a>';
		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);
6de48c3d   Benjamin Renard   Implement data up...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
	},

	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;
	},
});