Source code for /src/CManager.js.txt
// =====================================================================//
// Author: Mark Qian <markqian@hotmail.com> //
// WWW: http://www.coolshare.com/ //
// Copyright (c) 2006, Mark Qian //
// //
// You must contact Mark Qian to get a permission of use //
// in case you want to make any use of the codes except viewing it //
// on Mark's site. //
//======================================================================//
var idCount = 0;
///////////////////////////////////////////////////////////////////
//
// I simply create a concrete Communicator
// Manager, IframeCManager.
//
//////////////////////////////////////////////////////////////////
function CManager(a, max, timeout) {
this.a = a;
if (!timeout) {
timeout = 30000;
}
this.commList = new Array();
this.getCommunicator = function (enforce) {
var st = this.getMSeconds();
var i=0;
while (true) {
for (var i=0; i<this.commList.length; i++) {
if (this.commList[i].state==0 && this.match()) {
return this.commList[i];
}
}
if (enforce) {
//enforce to get a Commnicator outside of pool
var comm = new DSCommunicator();
comm.init(w, url, params, handler);
return comm;
}
var et = this.getMSeconds();
if ((et-st)>timeout) {
alert("Timeout when waiting for available Communicator");
break;
}
//loop until find vacancy: block the current thread
}
}
this.findCommunicator = function (id) {
for (var i=0; i<this.commList.length; i++) {
if (this.commList[i].id==id) {
return this.commList[i];
}
}
return null;
}
this.getMSeconds = function()
{
var dt = new Date();
var res = 0;
res += dt.getMilliseconds();
res += 1000*dt.getSeconds();
res += 1000*60*dt.getMinutes();
res += 1000*3600*dt.getHours();
return 0+res/1000;
}
this.init = function(a) {
if (!a) {
//Create a variety of Communicators here
} else {
//create one type of Communicator
for (var i=0; i<max; i++) {
this.commList[this.commList.length] = eval("new "+a+"Communicator()");
}
}
}
this.init(this.a);
this.match = function(create) {
//the logic to determent which type of Communicator is the
//best for this request. Add a new one of the best type to
//the commList if create is true
//....
return true;
}
}