From 7ca600c2b2de65d9f63576927a7f8e8e7958dc14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Wed, 21 Sep 2011 09:14:34 +0000
Subject: [PATCH] Improvements to parallell processing in Discojuice...

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2923 44740490-163a-0410-bde0-09ae8108e29a
---
 .../www/discojuice/discojuice.control.js      | 101 +++++++++++++-----
 .../www/discojuice/discojuice.misc.js         |   7 ++
 2 files changed, 82 insertions(+), 26 deletions(-)

diff --git a/modules/discojuice/www/discojuice/discojuice.control.js b/modules/discojuice/www/discojuice/discojuice.control.js
index 16effc9d3..3e94e03dc 100644
--- a/modules/discojuice/www/discojuice/discojuice.control.js
+++ b/modules/discojuice/www/discojuice/discojuice.control.js
@@ -27,6 +27,19 @@ DiscoJuice.Control = {
 	"extensionResponse": null,
 	
 	"quickEntry": null,
+
+	// Waiter Notification Callback Registry
+	"wncr": [],
+	
+	
+	"registerCallback": function (callback) {
+		this.wncr.push(callback);
+		return (this.wncr.length - 1);
+	},
+	
+	"runCallback": function (i) {
+		if (this.wncr[i] && typeof this.wncr[i] === 'function') this.wncr[i]();
+	},
 	
 	/*
 	 * Fetching JSON Metadata using AJAX.
@@ -62,7 +75,9 @@ DiscoJuice.Control = {
 			parameters.entityID = discosettings.spentityid;
 		}
 		
+		that.parent.Utils.log('Setting up load() waiter');
 		waiter = DiscoJuice.Utils.waiter(function() {
+			that.parent.Utils.log('load() waiter EXECUTE');
 			that.postLoad();
 		}, 10000);
 		
@@ -73,7 +88,6 @@ DiscoJuice.Control = {
 					var j = i+1;
 					$.getJSON(curmdurl, parameters, function(data) {
 						that.data = $.merge(that.data, data);
-						console.log(data);
 						that.parent.Utils.log('Successfully loaded metadata (' + data.length + ') (' + j + ' of ' + metadataurls.length + ')');
 						notifyCompleted();
 					});
@@ -115,20 +129,25 @@ DiscoJuice.Control = {
 			that.filterCountrySetup();
 		}
 
+		that.getCountry(); // Syncronous
+		that.readCookie(); // Syncronous
+		that.readExtensionResponse(); // Reading response set by the Browser extension
+
+		that.parent.Utils.log('Setting up postLoad() waiter');
 		waiter = DiscoJuice.Utils.waiter(function() {
-			that.postLoad();
+			that.prepareData();
+			that.searchboxSetup();
+			that.parent.Utils.log('postLoad() waiter EXECUTE');
 		}, 10000);
+		
+		that.discoReadSetup(waiter);
+		that.discoSubReadSetup(waiter);
+		
+		waiter.startTimer();
+		
 
 
-		that.getCountry(); // Syncronous
-		that.readCookie(); // Syncronous
-		that.readExtensionResponse(); // ...
-		
-		that.prepareData();
 		
-		that.discoReadSetup(); 		// Asyncronous
-		that.discoSubReadSetup();	// Asyncronous
-		that.searchboxSetup();		// Asyncronous
 		
 	},
 	
@@ -149,6 +168,9 @@ DiscoJuice.Control = {
 		}
 	},
 	
+	/*
+	 * Reading response set by the Browser extension
+	 */
 	"readExtensionResponse": function() {
 	
 		if (!this.extensionResponse) return;
@@ -218,7 +240,7 @@ DiscoJuice.Control = {
 		this.parent.Utils.log('DiscoJuice setWeight failer (no entries found for) ' + entityID + ' # ' + subID);
 	},
 	
-	"discoResponse": function(sender, entityID, subID) {
+	"discoResponse": function(sender, entityID, subID, cid) {
 		this.parent.Utils.log('DiscoResponse Received from [' + sender  + '] entityID: ' + entityID + ' subID: ' + subID);
 		
 		var settings = this.parent.Utils.options.get('disco');
@@ -234,6 +256,14 @@ DiscoJuice.Control = {
 		
 		this.setWeight(-100, entityID, subID);
 		this.prepareData();
+		
+		if (cid) {
+			this.runCallback(cid);			
+		} else {
+			// Fallback; if response endpoint is not yet updated to support passing a callback ID reference.
+			this.prepareData();
+		}
+		
 	},
 	
 	"calculateDistance": function() {
@@ -509,7 +539,7 @@ DiscoJuice.Control = {
 	},
 	
 	// Setup an iframe to read discovery cookies from other domains
-	"discoReadSetup": function() {
+	"discoReadSetup": function(waiter) {
 		var settings = this.parent.Utils.options.get('disco');
 		
 		if (!settings) return;
@@ -520,20 +550,31 @@ DiscoJuice.Control = {
 		var stores = settings.stores;
 		var i;
 		var currentStore;
+		var callbackid;
+		var returnurlwithparams;
 		
 		if (!stores) return;
 		
 		for(i = 0; i < stores.length; i++) {
-			currentStore = stores[i];
-			this.parent.Utils.log('Setting up DisoJuice Read from Store [' + currentStore + ']');
-			iframeurl = currentStore + '?entityID=' + escape(spentityid) + '&isPassive=true&returnIDParam=entityID&return=' + escape(returnurl);
-			html = '<iframe src="' + iframeurl + '" style="display: none"></iframe>';
-			this.ui.addContent(html);
+			
+			waiter.runAction(function (notifyCompleted) {
+			
+				callbackid = this.registerCallback(notifyCompleted);
+				returnurlwithparams = returnurl + 'cid=' + callbackid;
+				
+				currentStore = stores[i];
+				this.parent.Utils.log('Setting up DisoJuice Read from Store [' + currentStore + ']');
+				iframeurl = currentStore + '?entityID=' + escape(spentityid) + '&isPassive=true&returnIDParam=entityID&return=' + escape(returnurlwithparams);
+				html = '<iframe src="' + iframeurl + '" style="display: none"></iframe>';
+				this.ui.addContent(html);
+				
+			});
+
 		}
 	},
 	
 	// Setup an iframe to read discovery cookies from other domains
-	"discoSubReadSetup": function() {
+	"discoSubReadSetup": function(waiter) {
 		var settings = this.parent.Utils.options.get('disco');
 		
 		if (!settings) return;
@@ -544,18 +585,25 @@ DiscoJuice.Control = {
 		var stores = settings.subIDstores;
 		var i;
 		var currentStore;
+		var callbackid;
 		
 		if (!stores) return;
 		
 		for(var idp in stores) {
-			returnurl = settings.url + 'entityID=' + escape(idp);
-			currentStore = stores[idp];
-			this.parent.Utils.log('Setting up SubID DisoJuice Read from Store [' + idp + '] =>  [' + currentStore + ']');
-			iframeurl = currentStore + '?entityID=' + escape(spentityid) + '&isPassive=true&returnIDParam=subID&return=' + escape(returnurl);
-			this.parent.Utils.log('iFrame URL is  [' + iframeurl + ']');
-			this.parent.Utils.log('return URL is  [' + returnurl + ']');
-			html = '<iframe src="' + iframeurl + '" style="display: none"></iframe>';
-			this.ui.addContent(html);
+			
+			waiter.runAction(function (notifyCompleted) {
+			
+				callbackid = this.registerCallback(notifyCompleted);
+				returnurl = settings.url + 'entityID=' + escape(idp) + '&callbackid=' + callbackid;
+				
+				currentStore = stores[idp];
+				this.parent.Utils.log('Setting up SubID DisoJuice Read from Store [' + idp + '] =>  [' + currentStore + ']');
+				iframeurl = currentStore + '?entityID=' + escape(spentityid) + '&isPassive=true&returnIDParam=subID&return=' + escape(returnurl);
+				this.parent.Utils.log('iFrame URL is  [' + iframeurl + ']');
+				this.parent.Utils.log('return URL is  [' + returnurl + ']');
+				html = '<iframe src="' + iframeurl + '" style="display: none"></iframe>';
+				this.ui.addContent(html);
+			});
 		}
 	},
 
@@ -625,6 +673,7 @@ DiscoJuice.Control = {
 			
 			// Ping
 			function ping (event) {
+				console.log('Search box detected a change. Executing refresh...')
 				my.counter++;
 				setTimeout(function() {
 					if (--my.counter === 0) {
diff --git a/modules/discojuice/www/discojuice/discojuice.misc.js b/modules/discojuice/www/discojuice/discojuice.misc.js
index fb5bc03c9..7707b482e 100644
--- a/modules/discojuice/www/discojuice/discojuice.misc.js
+++ b/modules/discojuice/www/discojuice/discojuice.misc.js
@@ -217,10 +217,17 @@ DiscoJuice.Utils = {
 		}
 		
 		function startTimer() {
+			if (parallellActions.length === 0) {
+				console.log('Executing because no action is scheduled....');
+				if (!executed) execute();
+				return;
+			}
+			
 			setTimeout(function() {
 				console.log('Action timeout!');
 				if (!executed) execute();
 			}, waitSeconds);
+			
 		}
 		
 
-- 
GitLab