1 /*
  2  * 	Copyright (C) 2012-2013 DFKI GmbH
  3  * 	Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
  4  * 	German Research Center for Artificial Intelligence
  5  * 	http://www.dfki.de
  6  * 
  7  * 	Permission is hereby granted, free of charge, to any person obtaining a 
  8  * 	copy of this software and associated documentation files (the 
  9  * 	"Software"), to deal in the Software without restriction, including 
 10  * 	without limitation the rights to use, copy, modify, merge, publish, 
 11  * 	distribute, sublicense, and/or sell copies of the Software, and to 
 12  * 	permit persons to whom the Software is furnished to do so, subject to 
 13  * 	the following conditions:
 14  * 
 15  * 	The above copyright notice and this permission notice shall be included 
 16  * 	in all copies or substantial portions of the Software.
 17  * 
 18  * 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 19  * 	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 20  * 	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 21  * 	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 22  * 	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 23  * 	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 24  * 	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 25  */
 26 
 27 /**
 28  * This is a Worker script that is used as "threaded queue" for managing SCION event execution:
 29  * 
 30  * New events for SCION are queued in order to allow raising new events from within SCION-event-processing.
 31  * 
 32  * <p>
 33  * This "threaded queue" receives messages from the "Android environment engine" of
 34  * {@link mmir.DialogEngine} - see manager/dialog/engineConfig.js::_androidFactory.
 35  * 
 36  * <p>
 37  * The message / work flow is as follows:
 38  * <ul>
 39  * 	<li>on receiving <code>newJob</code>: add new event-job to the queue</li>
 40  * 	<li>on receiving <code>readyForJob</code>:
 41  * 		<li>if queue is empty: does nothing</li>
 42  * 		<li>if queue is not empty: take next job from queue and send it as a <code>toDo</code> message to SCION</li>
 43  * 	</li>
 44  * </ul>
 45  * 
 46  * @module workers/scionRaiseQueue
 47  */
 48 
 49 /**
 50  * @private
 51  */
 52 var queue = [],
 53 	readyForJob = true;
 54 
 55 /**
 56  * Handler for received messages.
 57  * 
 58  * @function
 59  * @param {Object} e the message object with<br>
 60  * 			{String} e.data.command the message type / name (e.g. <tt>readyForJob</tt>)<br>
 61  * 			{Object} [e.data.job] the job object (e.g. in case of command <tt>newJob</tt> the job that will be queued)
 62  * 
 63  * @public
 64  */
 65 self.onmessage = function(e){
 66   switch(e.data.command){
 67     case 'readyForJob':
 68       readyForJob = true;
 69       distributeJobs();
 70       break;
 71     case 'newJob':
 72       queue.push(e.data.job);
 73       distributeJobs();
 74       break;
 75   }
 76 };
 77 
 78 /**
 79  * Handler for sending messages.
 80  * 
 81  * <p>
 82  * In case of <code>readyForJob</code>: 
 83  * take next job and send it to SCION, i.e. post a message <tt>e</tt> with <br>
 84  * 
 85  * 	{String} e.data.command = <tt>toDo</tt>)<br>
 86  * 	{Object} e.data.toDo the job object (that was added before to the queue with command / message <tt>newJob</tt>)
 87  *  
 88  * 
 89  * @function
 90  * @private
 91  */
 92 function distributeJobs(){
 93 	if (readyForJob && queue.length>0){
 94 		readyForJob = false;
 95 		var job = queue.shift();
 96 		postMessage({command: 'toDo',
 97 			toDo : job});
 98 	}
 99 };