SAMPManager.js 6.38 KB
Ext.define('treps.controller.SAMP.SAMPManager', {
	singleton : true,

	requires: [
		'treps.store.SAMPClients'
	],

	connector          : null,

	onConnect          : null,

	onDisconnect       : null,

	onReceiveFile      : null,

	onClientsUpdate    : null,

	constructor: function() {
	},

	initSAMP: function()
	{
		var me = this;

		if (this.connector != null)
			return;

		Ext.Loader.loadScript({
			url: treps.Constants.SAMP_LIB,
			scope: this,
			onLoad: function() {
				//init meta data
				var href = window.location.href;
				var iconURL = href+'resources/images/samp/treps.jpg';

				var meta = {
					"samp.name": treps.Constants.APP_TITLE,
					"samp.description.text": treps.Constants.APP_DESCRIPTION,
					"samp.icon.url": iconURL
				};

				//init handlers
				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) 
				{
					var record = null;;
					var clientsStore = me.getClientsStore();
					if (clientsStore != null)
						record = clientsStore.getById(id);

					switch (type)
					{
						case 'register' :
							clientsStore.add({id : id});

							if (me.onClientsUpdate != null)
								me.onClientsUpdate();
							break;
						case 'unregister' :
							if (record)
    								clientsStore.remove(record);

							if (me.onClientsUpdate != null)
								me.onClientsUpdate();
							break;
						case 'meta' :
							if (!record)
							{
								clientsStore.add({id : id});
								record = clientsStore.getById(id);
							}

    							if (record)
    							{
    								record.set('description',data['samp.description.text']);
    								record.set('icon',data['samp.icon.url']);
    								record.set('name',data['samp.name']);

								if (cc.subs[id])
								{
									record.set('acceptVOTable', me.isSubscribed(cc.subs[id],"table.load.votable"));
									record.set('acceptCDF', me.isSubscribed(cc.subs[id],"table.load.cdf"));
									record.set('acceptNetCDF', me.isSubscribed(cc.subs[id],"table.load.netcdf"));
								}
    							}
 
							if (me.onClientsUpdate != null)
								me.onClientsUpdate();
							break;
						case 'subs' :
							if (record)
							{
								record.set('acceptVOTable', me.isSubscribed(cc.subs[id],"table.load.votable"));
								record.set('acceptCDF', me.isSubscribed(cc.subs[id],"table.load.cdf"));
								record.set('acceptNetCDF', me.isSubscribed(cc.subs[id],"table.load.netcdf"));
							}

							if (me.onClientSubs != null)
								me.onClientsUpdate();
							break;
					}
				};

				logCc = {
					receiveNotification: function(senderId, message) {
						var handled = cc.receiveNotification(senderId, message);
					},
					receiveCall: function(senderId, msgId, message) {
						var handled = cc.receiveCall(senderId, msgId, message);
					},
					receiveResponse: function(responderId, msgTag, response) {
						var handled = cc.receiveResponse(responderId, msgTag, response);
					},
					init: function(connection) {
						cc.init(connection);
					}
				};

				//connect to SAMP + MTypes subscription
				var subs = cc.calculateSubscriptions();

				this.connector = new samp.Connector("TREPS", meta, logCc, subs);

				this.connector.onreg = function(conn) {
					if (me.onConnect != null)
						me.onConnect();
				};
			
				this.connector.onunreg = function(conn) {
					if (me.onDisconnect != null)
						me.onDisconnect();
				};
			},
			onError: function() {
				treps.Messages.showError("SAMP library not reachable!");
			}
                });
	},

	getClientsStore : function()
	{
		var store = Ext.getStore('SAMPClientsStore');
		if (store != null)
			return store;
		return Ext.create('treps.store.SAMPClients',
			{
				storeId : 'SAMPClientsStore'
			}
		);
	},

	connect : function()
	{
		if (!this.connector)
			return false;
		try
		{
			this.connector.register();
			return true;
		}
		catch(err)
		{
			treps.Messages.showError("SAMP connector error : "+err);
			return false;
		}
	},

	isConnected : function()
	{
		if (!this.connector)
			return false;
		try
		{
			return (this.connector.connection != null);
		}
		catch(err)
		{
			treps.Messages.showError("SAMP connector error : "+err);
			return false;
		}
	},
	
	disconnect : function()
	{
		if (!this.connector)
			return false;
		try
		{
			this.connector.unregister();
			var clientsStore = this.getClientsStore();
			clientsStore.removeAll();
			return true;
		}
		catch(err)
		{
			treps.Messages.showError("SAMP connector error : "+err);
			return false;
		}
	},

	isSubscribed : function(subs, mtype)
	{
		if (!this.connector)
			return false;
		return samp.isSubscribed(subs, mtype);
	},

	notififyMessage : function(message,clientId)
	{
		if (!this.connector)
			return false;
		try
		{
			if (!clientId || clientId == 'hub')
				this.connector.connection.notifyAll([message]);
			else
				this.connector.connection.notify([clientId,message]);
			return true;
		}
		catch(err)
		{
			treps.Messages.showError("SAMP connector error : "+err);
			return false;
		}
	},

	receiveFile : function(clientName, url, format)
	{
		if (this.onReceiveFile != null)
			this.onReceiveFile(clientName, url, format);
	},

	sendFile : function(clientId, format, url)
	{
		var mType = '';
		switch (format)
		{
			case 'votable' :
				mType = 'table.load.votable';
				break;
			case 'cdf' :
				mType = 'table.load.cdf';
				break;
			case 'netcdf' :
				mType = 'table.load.netcdf';
				break;
			default:
				return false;
		}

		var message = new samp.Message(mType, {"url": url});
		return this.notififyMessage(message,clientId);
	}
});