summaryrefslogtreecommitdiff
path: root/js/src/Ice/OutgoingConnectionFactory.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/Ice/OutgoingConnectionFactory.js')
-rw-r--r--js/src/Ice/OutgoingConnectionFactory.js1530
1 files changed, 765 insertions, 765 deletions
diff --git a/js/src/Ice/OutgoingConnectionFactory.js b/js/src/Ice/OutgoingConnectionFactory.js
index 1d0fefe688c..428e8d75092 100644
--- a/js/src/Ice/OutgoingConnectionFactory.js
+++ b/js/src/Ice/OutgoingConnectionFactory.js
@@ -7,312 +7,338 @@
//
// **********************************************************************
-(function(global){
- require("Ice/Class");
- require("Ice/ArrayUtil");
- require("Ice/AsyncResultBase");
- require("Ice/ConnectionI");
- require("Ice/Debug");
- require("Ice/ExUtil");
- require("Ice/HashMap");
- require("Ice/Promise");
- require("Ice/EndpointTypes");
- require("Ice/LocalException");
- require("Ice/Exception");
- require("Ice/ACM");
-
- var Ice = global.Ice || {};
-
- var ArrayUtil = Ice.ArrayUtil;
- var AsyncResultBase = Ice.AsyncResultBase;
- var ConnectionI = Ice.ConnectionI;
- var ConnectionReaper = Ice.ConnectionReaper;
- var Debug = Ice.Debug;
- var ExUtil = Ice.ExUtil;
- var HashMap = Ice.HashMap;
- var Promise = Ice.Promise;
- var EndpointSelectionType = Ice.EndpointSelectionType;
- var FactoryACMMonitor = Ice.FactoryACMMonitor;
-
- var Class = Ice.Class;
+var Ice = require("../Ice/ModuleRegistry").Ice;
+Ice.__M.require(module, "Ice",
+ [
+ "../Ice/Class",
+ "../Ice/ArrayUtil",
+ "../Ice/AsyncResultBase",
+ "../Ice/ConnectionI",
+ "../Ice/Debug",
+ "../Ice/ExUtil",
+ "../Ice/HashMap",
+ "../Ice/Promise",
+ "../Ice/EndpointTypes",
+ "../Ice/LocalException",
+ "../Ice/Exception",
+ "../Ice/ACM"
+ ]);
+
+var ArrayUtil = Ice.ArrayUtil;
+var AsyncResultBase = Ice.AsyncResultBase;
+var ConnectionI = Ice.ConnectionI;
+var ConnectionReaper = Ice.ConnectionReaper;
+var Debug = Ice.Debug;
+var ExUtil = Ice.ExUtil;
+var HashMap = Ice.HashMap;
+var Promise = Ice.Promise;
+var EndpointSelectionType = Ice.EndpointSelectionType;
+var FactoryACMMonitor = Ice.FactoryACMMonitor;
+var Class = Ice.Class;
+
+//
+// Only for use by Instance.
+//
+var OutgoingConnectionFactory = Class({
+ __init__: function(communicator, instance)
+ {
+ this._communicator = communicator;
+ this._instance = instance;
+ this._destroyed = false;
+
+ this._monitor = new FactoryACMMonitor(this._instance, this._instance.clientACM());
+
+ this._connectionsByEndpoint = new ConnectionListMap(); // map<EndpointI, Array<Ice.ConnectionI>>
+ this._pending = new HashMap(); // map<EndpointI, Array<ConnectCallback>>
+ this._pending.keyComparator = HashMap.compareEquals;
+ this._pendingConnectCount = 0;
+
+ this._waitPromise = null;
+ },
+ destroy: function()
+ {
+ if(this._destroyed)
+ {
+ return;
+ }
+
+ this._connectionsByEndpoint.forEach(function(connection)
+ {
+ connection.destroy(ConnectionI.CommunicatorDestroyed);
+ });
+
+ this._destroyed = true;
+ this._communicator = null;
+ this.checkFinished();
+ },
+ waitUntilFinished: function()
+ {
+ this._waitPromise = new Promise();
+ this.checkFinished();
+ return this._waitPromise;
+ },
//
- // Only for use by Instance.
+ // Returns a promise, success callback receives (connection, compress)
//
- var OutgoingConnectionFactory = Class({
- __init__: function(communicator, instance)
- {
- this._communicator = communicator;
- this._instance = instance;
- this._destroyed = false;
-
- this._monitor = new FactoryACMMonitor(this._instance, this._instance.clientACM());
+ create: function(endpts, hasMore, selType)
+ {
+ Debug.assert(endpts.length > 0);
- this._connectionsByEndpoint = new ConnectionListMap(); // map<EndpointI, Array<Ice.ConnectionI>>
- this._pending = new HashMap(); // map<EndpointI, Array<ConnectCallback>>
- this._pending.keyComparator = HashMap.compareEquals;
- this._pendingConnectCount = 0;
+ //
+ // Apply the overrides.
+ //
+ var endpoints = this.applyOverrides(endpts);
- this._waitPromise = null;
- },
- destroy: function()
+ //
+ // Try to find a connection to one of the given endpoints.
+ //
+ try
{
- if(this._destroyed)
+ var compress = { value: false };
+ var connection = this.findConnectionByEndpoint(endpoints, compress);
+ if(connection !== null)
{
- return;
+ return new Promise().succeed(connection, compress.value);
}
-
- this._connectionsByEndpoint.forEach(function(connection)
- {
- connection.destroy(ConnectionI.CommunicatorDestroyed);
- });
-
- this._destroyed = true;
- this._communicator = null;
- this.checkFinished();
- },
- waitUntilFinished: function()
- {
- this._waitPromise = new Promise();
- this.checkFinished();
- return this._waitPromise;
- },
- //
- // Returns a promise, success callback receives (connection, compress)
- //
- create: function(endpts, hasMore, selType)
+ }
+ catch(ex)
{
- Debug.assert(endpts.length > 0);
-
- //
- // Apply the overrides.
- //
- var endpoints = this.applyOverrides(endpts);
+ return new Promise().fail(ex);
+ }
- //
- // Try to find a connection to one of the given endpoints.
- //
- try
+ var cb = new ConnectCallback(this, endpoints, hasMore, selType);
+ return cb.start();
+ },
+ setRouterInfo: function(routerInfo)
+ {
+ var self = this;
+ return Ice.Promise.try(
+ function()
{
- var compress = { value: false };
- var connection = this.findConnectionByEndpoint(endpoints, compress);
- if(connection !== null)
+ if(self._destroyed)
{
- return new Promise().succeed(connection, compress.value);
+ throw new Ice.CommunicatorDestroyedException();
}
+ return routerInfo.getClientEndpoints();
}
- catch(ex)
+ ).then(
+ function(endpoints)
{
- return new Promise().fail(ex);
- }
-
- var cb = new ConnectCallback(this, endpoints, hasMore, selType);
- return cb.start();
- },
- setRouterInfo: function(routerInfo)
- {
- var self = this;
- return Ice.Promise.try(
- function()
+ //
+ // Search for connections to the router's client proxy
+ // endpoints, and update the object adapter for such
+ // connections, so that callbacks from the router can be
+ // received over such connections.
+ //
+ var adapter = routerInfo.getAdapter();
+ var defaultsAndOverrides = self._instance.defaultsAndOverrides();
+ for(var i = 0; i < endpoints.length; ++i)
{
- if(self._destroyed)
+ var endpoint = endpoints[i];
+
+ //
+ // Modify endpoints with overrides.
+ //
+ if(defaultsAndOverrides.overrideTimeout)
{
- throw new Ice.CommunicatorDestroyedException();
+ endpoint = endpoint.changeTimeout(defaultsAndOverrides.overrideTimeoutValue);
}
- return routerInfo.getClientEndpoints();
- }
- ).then(
- function(endpoints)
- {
+
//
- // Search for connections to the router's client proxy
- // endpoints, and update the object adapter for such
- // connections, so that callbacks from the router can be
- // received over such connections.
+ // The Connection object does not take the compression flag of
+ // endpoints into account, but instead gets the information
+ // about whether messages should be compressed or not from
+ // other sources. In order to allow connection sharing for
+ // endpoints that differ in the value of the compression flag
+ // only, we always set the compression flag to false here in
+ // this connection factory.
//
- var adapter = routerInfo.getAdapter();
- var defaultsAndOverrides = self._instance.defaultsAndOverrides();
- for(var i = 0; i < endpoints.length; ++i)
- {
- var endpoint = endpoints[i];
+ endpoint = endpoint.changeCompress(false);
- //
- // Modify endpoints with overrides.
- //
- if(defaultsAndOverrides.overrideTimeout)
+ for(var j = 0; j < self._connectionsByEndpoint.length; ++j)
+ {
+ var connection = self._connectionsByEndpoint[j];
+ if(connection.endpoint().equals(endpoint))
{
- endpoint = endpoint.changeTimeout(defaultsAndOverrides.overrideTimeoutValue);
+ connection.setAdapter(adapter);
}
-
- //
- // The Connection object does not take the compression flag of
- // endpoints into account, but instead gets the information
- // about whether messages should be compressed or not from
- // other sources. In order to allow connection sharing for
- // endpoints that differ in the value of the compression flag
- // only, we always set the compression flag to false here in
- // this connection factory.
- //
- endpoint = endpoint.changeCompress(false);
-
- self._connectionsByEndpoint.forEach(function(connection)
- {
- if(connection.endpoint().equals(endpoint))
- {
- connection.setAdapter(adapter);
- }
- });
}
}
- );
- },
- removeAdapter: function(adapter)
- {
- if(this._destroyed)
- {
- return;
}
- this._connectionsByEndpoint.forEach(function(connection)
+ );
+ },
+ removeAdapter: function(adapter)
+ {
+ if(this._destroyed)
+ {
+ return;
+ }
+ this._connectionsByEndpoint.forEach(function(connection)
+ {
+ if(connection.getAdapter() === adapter)
{
- if(connection.getAdapter() === adapter)
- {
- connection.setAdapter(null);
- }
- });
- },
- flushAsyncBatchRequests: function()
- {
- var promise = new AsyncResultBase(this._communicator, "flushBatchRequests", null, null, null);
- if(this._destroyed)
- {
- promise.succeed();
- return;
- }
+ connection.setAdapter(null);
+ }
+ });
+ },
+ flushAsyncBatchRequests: function()
+ {
+ var promise = new AsyncResultBase(this._communicator, "flushBatchRequests", null, null, null);
+ if(this._destroyed)
+ {
+ promise.succeed();
+ return;
+ }
- Promise.all(
- this._connectionsByEndpoint.map(
- function(connection)
+ Promise.all(
+ this._connectionsByEndpoint.map(
+ function(connection)
+ {
+ if(connection.isActiveOrHolding())
{
- if(connection.isActiveOrHolding())
- {
- return connection.flushBatchRequests().exception(
- function(ex)
+ return connection.flushBatchRequests().exception(
+ function(ex)
+ {
+ if(ex instanceof Ice.LocalException)
{
- if(ex instanceof Ice.LocalException)
- {
- // Ignore
- }
- else
- {
- throw ex;
- }
- });
- }
- })
- ).then(
- function()
- {
- promise.succeed(promise);
- },
- function(ex)
- {
- promise.fail(ex, promise);
- }
- );
- return promise;
- },
- applyOverrides: function(endpts)
- {
- var defaultsAndOverrides = this._instance.defaultsAndOverrides();
- var endpoints = [];
- for(var i = 0; i < endpts.length; ++i)
+ // Ignore
+ }
+ else
+ {
+ throw ex;
+ }
+ });
+ }
+ })
+ ).then(
+ function()
{
- var endpoint = endpts[i];
-
- //
- // Modify endpoints with overrides.
- //
- if(defaultsAndOverrides.overrideTimeout)
- {
- endpoints.push(endpoint.changeTimeout(defaultsAndOverrides.overrideTimeoutValue));
- }
- else
- {
- endpoints.push(endpoint);
- }
+ promise.succeed(promise);
+ },
+ function(ex)
+ {
+ promise.fail(ex, promise);
}
-
- return endpoints;
- },
- findConnectionByEndpoint: function(endpoints, compress)
+ );
+ return promise;
+ },
+ applyOverrides: function(endpts)
+ {
+ var defaultsAndOverrides = this._instance.defaultsAndOverrides();
+ var endpoints = [];
+ for(var i = 0; i < endpts.length; ++i)
{
- if(this._destroyed)
+ var endpoint = endpts[i];
+
+ //
+ // Modify endpoints with overrides.
+ //
+ if(defaultsAndOverrides.overrideTimeout)
{
- throw new Ice.CommunicatorDestroyedException();
+ endpoints.push(endpoint.changeTimeout(defaultsAndOverrides.overrideTimeoutValue));
}
+ else
+ {
+ endpoints.push(endpoint);
+ }
+ }
+
+ return endpoints;
+ },
+ findConnectionByEndpoint: function(endpoints, compress)
+ {
+ if(this._destroyed)
+ {
+ throw new Ice.CommunicatorDestroyedException();
+ }
- var defaultsAndOverrides = this._instance.defaultsAndOverrides();
- Debug.assert(endpoints.length > 0);
+ var defaultsAndOverrides = this._instance.defaultsAndOverrides();
+ Debug.assert(endpoints.length > 0);
- for(var i = 0; i < endpoints.length; ++i)
- {
- var endpoint = endpoints[i];
+ for(var i = 0; i < endpoints.length; ++i)
+ {
+ var endpoint = endpoints[i];
- if(this._pending.has(endpoint))
- {
- continue;
- }
+ if(this._pending.has(endpoint))
+ {
+ continue;
+ }
- var connectionList = this._connectionsByEndpoint.get(endpoint);
- if(connectionList === undefined)
- {
- continue;
- }
+ var connectionList = this._connectionsByEndpoint.get(endpoint);
+ if(connectionList === undefined)
+ {
+ continue;
+ }
- for(var j = 0; j < connectionList.length; ++j)
+ for(var j = 0; j < connectionList.length; ++j)
+ {
+ if(connectionList[j].isActiveOrHolding()) // Don't return destroyed or un-validated connections
{
- if(connectionList[j].isActiveOrHolding()) // Don't return destroyed or un-validated connections
+ if(defaultsAndOverrides.overrideCompress)
{
- if(defaultsAndOverrides.overrideCompress)
- {
- compress.value = defaultsAndOverrides.overrideCompressValue;
- }
- else
- {
- compress.value = endpoint.compress();
- }
- return connectionList[j];
+ compress.value = defaultsAndOverrides.overrideCompressValue;
}
+ else
+ {
+ compress.value = endpoint.compress();
+ }
+ return connectionList[j];
}
}
+ }
- return null;
- },
- incPendingConnectCount: function()
+ return null;
+ },
+ incPendingConnectCount: function()
+ {
+ //
+ // Keep track of the number of pending connects. The outgoing connection factory
+ // waitUntilFinished() method waits for all the pending connects to terminate before
+ // to return. This ensures that the communicator client thread pool isn't destroyed
+ // too soon and will still be available to execute the ice_exception() callbacks for
+ // the asynchronous requests waiting on a connection to be established.
+ //
+
+ if(this._destroyed)
{
- //
- // Keep track of the number of pending connects. The outgoing connection factory
- // waitUntilFinished() method waits for all the pending connects to terminate before
- // to return. This ensures that the communicator client thread pool isn't destroyed
- // too soon and will still be available to execute the ice_exception() callbacks for
- // the asynchronous requests waiting on a connection to be established.
- //
+ throw new Ice.CommunicatorDestroyedException();
+ }
+ ++this._pendingConnectCount;
+ },
+ decPendingConnectCount: function()
+ {
+ --this._pendingConnectCount;
+ Debug.assert(this._pendingConnectCount >= 0);
+ if(this._destroyed && this._pendingConnectCount === 0)
+ {
+ this.checkFinished();
+ }
+ },
+ getConnection: function(endpoints, cb, compress)
+ {
+ if(this._destroyed)
+ {
+ throw new Ice.CommunicatorDestroyedException();
+ }
- if(this._destroyed)
- {
- throw new Ice.CommunicatorDestroyedException();
- }
- ++this._pendingConnectCount;
- },
- decPendingConnectCount: function()
+ //
+ // Reap closed connections
+ //
+ var cons = this._monitor.swapReapedConnections();
+ if(cons !== null)
{
- --this._pendingConnectCount;
- Debug.assert(this._pendingConnectCount >= 0);
- if(this._destroyed && this._pendingConnectCount === 0)
+ for(var i = 0; i < cons.length; ++i)
{
- this.checkFinished();
+ var c = cons[i];
+ this._connectionsByEndpoint.removeConnection(c.endpoint(), c);
+ this._connectionsByEndpoint.removeConnection(c.endpoint().changeCompress(true), c);
}
- },
- getConnection: function(endpoints, cb, compress)
+ }
+
+ //
+ // Try to get the connection.
+ //
+ while(true)
{
if(this._destroyed)
{
@@ -320,639 +346,613 @@
}
//
- // Reap closed connections
+ // Search for a matching connection. If we find one, we're done.
//
- var cons = this._monitor.swapReapedConnections();
- if(cons !== null)
+ var connection = this.findConnectionByEndpoint(endpoints, compress);
+ if(connection !== null)
{
- for(var i = 0; i < cons.length; ++i)
- {
- var c = cons[i];
- this._connectionsByEndpoint.removeConnection(c.endpoint(), c);
- this._connectionsByEndpoint.removeConnection(c.endpoint().changeCompress(true), c);
- }
+ return connection;
}
- //
- // Try to get the connection.
- //
- while(true)
+ if(this.addToPending(cb, endpoints))
{
- if(this._destroyed)
- {
- throw new Ice.CommunicatorDestroyedException();
- }
-
//
- // Search for a matching connection. If we find one, we're done.
+ // A connection is already pending.
//
- var connection = this.findConnectionByEndpoint(endpoints, compress);
- if(connection !== null)
- {
- return connection;
- }
-
- if(this.addToPending(cb, endpoints))
- {
- //
- // A connection is already pending.
- //
- return null;
- }
- else
- {
- //
- // No connection is currently pending to one of our endpoints, so we
- // get out of this loop and start the connection establishment to one of the
- // given endpoints.
- //
- break;
- }
+ return null;
+ }
+ else
+ {
+ //
+ // No connection is currently pending to one of our endpoints, so we
+ // get out of this loop and start the connection establishment to one of the
+ // given endpoints.
+ //
+ break;
}
+ }
- //
- // At this point, we're responsible for establishing the connection to one of
- // the given endpoints. If it's a non-blocking connect, calling nextEndpoint
- // will start the connection establishment. Otherwise, we return null to get
- // the caller to establish the connection.
- //
- cb.nextEndpoint();
+ //
+ // At this point, we're responsible for establishing the connection to one of
+ // the given endpoints. If it's a non-blocking connect, calling nextEndpoint
+ // will start the connection establishment. Otherwise, we return null to get
+ // the caller to establish the connection.
+ //
+ cb.nextEndpoint();
+
+ return null;
+ },
+ createConnection: function(transceiver, endpoint)
+ {
+ Debug.assert(this._pending.has(endpoint) && transceiver !== null);
- return null;
- },
- createConnection: function(transceiver, endpoint)
+ //
+ // Create and add the connection to the connection map. Adding the connection to the map
+ // is necessary to support the interruption of the connection initialization and validation
+ // in case the communicator is destroyed.
+ //
+ var connection = null;
+ try
{
- Debug.assert(this._pending.has(endpoint) && transceiver !== null);
+ if(this._destroyed)
+ {
+ throw new Ice.CommunicatorDestroyedException();
+ }
- //
- // Create and add the connection to the connection map. Adding the connection to the map
- // is necessary to support the interruption of the connection initialization and validation
- // in case the communicator is destroyed.
- //
- var connection = null;
- try
+ connection = new ConnectionI(this._communicator, this._instance, this._monitor, transceiver,
+ endpoint.changeCompress(false), false, null);
+ }
+ catch(ex)
+ {
+ if(ex instanceof Ice.LocalException)
{
- if(this._destroyed)
+ try
{
- throw new Ice.CommunicatorDestroyedException();
+ transceiver.close();
}
-
- connection = new ConnectionI(this._communicator, this._instance, this._monitor, transceiver,
- endpoint.changeCompress(false), false, null);
- }
- catch(ex)
- {
- if(ex instanceof Ice.LocalException)
+ catch(exc)
{
- try
- {
- transceiver.close();
- }
- catch(exc)
- {
- // Ignore
- }
+ // Ignore
}
- throw ex;
}
+ throw ex;
+ }
- this._connectionsByEndpoint.set(connection.endpoint(), connection);
- this._connectionsByEndpoint.set(connection.endpoint().changeCompress(true), connection);
- return connection;
- },
- finishGetConnection: function(endpoints, endpoint, connection, cb)
- {
- // cb is-a ConnectCallback
+ this._connectionsByEndpoint.set(connection.endpoint(), connection);
+ this._connectionsByEndpoint.set(connection.endpoint().changeCompress(true), connection);
+ return connection;
+ },
+ finishGetConnection: function(endpoints, endpoint, connection, cb)
+ {
+ // cb is-a ConnectCallback
- var connectionCallbacks = [];
- if(cb !== null)
- {
- connectionCallbacks.push(cb);
- }
+ var connectionCallbacks = [];
+ if(cb !== null)
+ {
+ connectionCallbacks.push(cb);
+ }
- var i;
- var cc;
- var callbacks = [];
- for(i = 0; i < endpoints.length; ++i)
+ var i;
+ var cc;
+ var callbacks = [];
+ for(i = 0; i < endpoints.length; ++i)
+ {
+ var endpt = endpoints[i];
+ var cbs = this._pending.get(endpt);
+ if(cbs !== undefined)
{
- var endpt = endpoints[i];
- var cbs = this._pending.get(endpt);
- if(cbs !== undefined)
+ this._pending.delete(endpt);
+ for(var j = 0; j < cbs.length; ++j)
{
- this._pending.delete(endpt);
- for(var j = 0; j < cbs.length; ++j)
+ cc = cbs[j];
+ if(cc.hasEndpoint(endpoint))
{
- cc = cbs[j];
- if(cc.hasEndpoint(endpoint))
+ if(connectionCallbacks.indexOf(cc) === -1)
{
- if(connectionCallbacks.indexOf(cc) === -1)
- {
- connectionCallbacks.push(cc);
- }
+ connectionCallbacks.push(cc);
}
- else
+ }
+ else
+ {
+ if(callbacks.indexOf(cc) === -1)
{
- if(callbacks.indexOf(cc) === -1)
- {
- callbacks.push(cc);
- }
+ callbacks.push(cc);
}
}
}
}
+ }
- for(i = 0; i < connectionCallbacks.length; ++i)
- {
- cc = connectionCallbacks[i];
- cc.removeFromPending();
- var idx = callbacks.indexOf(cc);
- if(idx !== -1)
- {
- callbacks.splice(idx, 1);
- }
- }
- for(i = 0; i < callbacks.length; ++i)
- {
- cc = callbacks[i];
- cc.removeFromPending();
- }
-
- var compress;
- var defaultsAndOverrides = this._instance.defaultsAndOverrides();
- if(defaultsAndOverrides.overrideCompress)
- {
- compress = defaultsAndOverrides.overrideCompressValue;
- }
- else
+ for(i = 0; i < connectionCallbacks.length; ++i)
+ {
+ cc = connectionCallbacks[i];
+ cc.removeFromPending();
+ var idx = callbacks.indexOf(cc);
+ if(idx !== -1)
{
- compress = endpoint.compress();
+ callbacks.splice(idx, 1);
}
+ }
+ for(i = 0; i < callbacks.length; ++i)
+ {
+ cc = callbacks[i];
+ cc.removeFromPending();
+ }
- for(i = 0; i < callbacks.length; ++i)
- {
- cc = callbacks[i];
- cc.getConnection();
- }
- for(i = 0; i < connectionCallbacks.length; ++i)
- {
- cc = connectionCallbacks[i];
- cc.setConnection(connection, compress);
- }
+ var compress;
+ var defaultsAndOverrides = this._instance.defaultsAndOverrides();
+ if(defaultsAndOverrides.overrideCompress)
+ {
+ compress = defaultsAndOverrides.overrideCompressValue;
+ }
+ else
+ {
+ compress = endpoint.compress();
+ }
- this.checkFinished();
- },
- finishGetConnectionEx: function(endpoints, ex, cb)
+ for(i = 0; i < callbacks.length; ++i)
{
- // cb is-a ConnectCallback
+ cc = callbacks[i];
+ cc.getConnection();
+ }
+ for(i = 0; i < connectionCallbacks.length; ++i)
+ {
+ cc = connectionCallbacks[i];
+ cc.setConnection(connection, compress);
+ }
- var failedCallbacks = [];
- if(cb !== null)
- {
- failedCallbacks.push(cb);
- }
- var i;
- var cc;
- var callbacks = [];
- for(i = 0; i < endpoints.length; ++i)
+ this.checkFinished();
+ },
+ finishGetConnectionEx: function(endpoints, ex, cb)
+ {
+ // cb is-a ConnectCallback
+
+ var failedCallbacks = [];
+ if(cb !== null)
+ {
+ failedCallbacks.push(cb);
+ }
+ var i;
+ var cc;
+ var callbacks = [];
+ for(i = 0; i < endpoints.length; ++i)
+ {
+ var endpt = endpoints[i];
+ var cbs = this._pending.get(endpt);
+ if(cbs !== undefined)
{
- var endpt = endpoints[i];
- var cbs = this._pending.get(endpt);
- if(cbs !== undefined)
+ this._pending.delete(endpt);
+ for(var j = 0; j < cbs.length; ++j)
{
- this._pending.delete(endpt);
- for(var j = 0; j < cbs.length; ++j)
+ cc = cbs[j];
+ if(cc.removeEndpoints(endpoints))
{
- cc = cbs[j];
- if(cc.removeEndpoints(endpoints))
+ if(failedCallbacks.indexOf(cc) === -1)
{
- if(failedCallbacks.indexOf(cc) === -1)
- {
- failedCallbacks.push(cc);
- }
+ failedCallbacks.push(cc);
}
- else
+ }
+ else
+ {
+ if(callbacks.indexOf(cc) === -1)
{
- if(callbacks.indexOf(cc) === -1)
- {
- callbacks.push(cc);
- }
+ callbacks.push(cc);
}
}
}
}
+ }
- for(i = 0; i < callbacks.length; ++i)
- {
- cc = callbacks[i];
- Debug.assert(failedCallbacks.indexOf(cc) === -1);
- cc.removeFromPending();
- }
- this.checkFinished();
+ for(i = 0; i < callbacks.length; ++i)
+ {
+ cc = callbacks[i];
+ Debug.assert(failedCallbacks.indexOf(cc) === -1);
+ cc.removeFromPending();
+ }
+ this.checkFinished();
- for(i = 0; i < callbacks.length; ++i)
- {
- cc = callbacks[i];
- cc.getConnection();
- }
- for(i = 0; i < failedCallbacks.length; ++i)
- {
- cc = failedCallbacks[i];
- cc.setException(ex);
- }
- },
- addToPending: function(cb, endpoints)
+ for(i = 0; i < callbacks.length; ++i)
+ {
+ cc = callbacks[i];
+ cc.getConnection();
+ }
+ for(i = 0; i < failedCallbacks.length; ++i)
{
- // cb is-a ConnectCallback
+ cc = failedCallbacks[i];
+ cc.setException(ex);
+ }
+ },
+ addToPending: function(cb, endpoints)
+ {
+ // cb is-a ConnectCallback
- //
- // Add the callback to each pending list.
- //
- var found = false;
- var p;
- var i;
- if(cb !== null)
+ //
+ // Add the callback to each pending list.
+ //
+ var found = false;
+ var p;
+ var i;
+ if(cb !== null)
+ {
+ for(i = 0; i < endpoints.length; ++i)
{
- for(i = 0; i < endpoints.length; ++i)
+ p = endpoints[i];
+ var cbs = this._pending.get(p);
+ if(cbs !== undefined)
{
- p = endpoints[i];
- var cbs = this._pending.get(p);
- if(cbs !== undefined)
+ found = true;
+ if(cbs.indexOf(cb) === -1)
{
- found = true;
- if(cbs.indexOf(cb) === -1)
- {
- cbs.push(cb); // Add the callback to each pending endpoint.
- }
+ cbs.push(cb); // Add the callback to each pending endpoint.
}
}
}
+ }
- if(found)
- {
- return true;
- }
+ if(found)
+ {
+ return true;
+ }
- //
- // If there's no pending connection for the given endpoints, we're
- // responsible for its establishment. We add empty pending lists,
- // other callbacks to the same endpoints will be queued.
- //
- for(i = 0; i < endpoints.length; ++i)
+ //
+ // If there's no pending connection for the given endpoints, we're
+ // responsible for its establishment. We add empty pending lists,
+ // other callbacks to the same endpoints will be queued.
+ //
+ for(i = 0; i < endpoints.length; ++i)
+ {
+ p = endpoints[i];
+ if(!this._pending.has(p))
{
- p = endpoints[i];
- if(!this._pending.has(p))
- {
- this._pending.set(p, []);
- }
+ this._pending.set(p, []);
}
+ }
- return false;
- },
- removeFromPending: function(cb, endpoints)
- {
- // cb is-a ConnectCallback
+ return false;
+ },
+ removeFromPending: function(cb, endpoints)
+ {
+ // cb is-a ConnectCallback
- for(var i = 0; i < endpoints.length; ++i)
+ for(var i = 0; i < endpoints.length; ++i)
+ {
+ var p = endpoints[i];
+ var cbs = this._pending.get(p);
+ if(cbs !== undefined)
{
- var p = endpoints[i];
- var cbs = this._pending.get(p);
- if(cbs !== undefined)
+ var idx = cbs.indexOf(cb);
+ if(idx !== -1)
{
- var idx = cbs.indexOf(cb);
- if(idx !== -1)
- {
- cbs.splice(idx, 1);
- }
+ cbs.splice(idx, 1);
}
}
- },
- handleConnectionException: function(ex, hasMore)
+ }
+ },
+ handleConnectionException: function(ex, hasMore)
+ {
+ var traceLevels = this._instance.traceLevels();
+ if(traceLevels.retry >= 2)
{
- var traceLevels = this._instance.traceLevels();
- if(traceLevels.retry >= 2)
+ var s = [];
+ s.push("connection to endpoint failed");
+ if(ex instanceof Ice.CommunicatorDestroyedException)
{
- var s = [];
- s.push("connection to endpoint failed");
- if(ex instanceof Ice.CommunicatorDestroyedException)
+ s.push("\n");
+ }
+ else
+ {
+ if(hasMore)
{
- s.push("\n");
+ s.push(", trying next endpoint\n");
}
else
{
- if(hasMore)
- {
- s.push(", trying next endpoint\n");
- }
- else
- {
- s.push(" and no more endpoints to try\n");
- }
+ s.push(" and no more endpoints to try\n");
}
- s.push(ExUtil.toString(ex));
- this._instance.initializationData().logger.trace(traceLevels.retryCat, s.join(""));
}
- },
- handleException: function(ex, hasMore)
+ s.push(ExUtil.toString(ex));
+ this._instance.initializationData().logger.trace(traceLevels.retryCat, s.join(""));
+ }
+ },
+ handleException: function(ex, hasMore)
+ {
+ var traceLevels = this._instance.traceLevels();
+ if(traceLevels.retry >= 2)
{
- var traceLevels = this._instance.traceLevels();
- if(traceLevels.retry >= 2)
+ var s = [];
+ s.push("couldn't resolve endpoint host");
+ if(ex instanceof Ice.CommunicatorDestroyedException)
+ {
+ s.push("\n");
+ }
+ else
{
- var s = [];
- s.push("couldn't resolve endpoint host");
- if(ex instanceof Ice.CommunicatorDestroyedException)
+ if(hasMore)
{
- s.push("\n");
+ s.push(", trying next endpoint\n");
}
else
{
- if(hasMore)
- {
- s.push(", trying next endpoint\n");
- }
- else
- {
- s.push(" and no more endpoints to try\n");
- }
+ s.push(" and no more endpoints to try\n");
}
- s.push(ExUtil.toString(ex));
- this._instance.initializationData().logger.trace(traceLevels.retryCat, s.join(""));
}
- },
- checkFinished: function()
+ s.push(ExUtil.toString(ex));
+ this._instance.initializationData().logger.trace(traceLevels.retryCat, s.join(""));
+ }
+ },
+ checkFinished: function()
+ {
+ //
+ // Can't continue until the factory is destroyed and there are no pending connections.
+ //
+ if(!this._waitPromise || !this._destroyed || this._pending.size > 0 || this._pendingConnectCount > 0)
{
- //
- // Can't continue until the factory is destroyed and there are no pending connections.
- //
- if(!this._waitPromise || !this._destroyed || this._pending.size > 0 || this._pendingConnectCount > 0)
- {
- return;
- }
-
- var self = this;
- Promise.all(
- self._connectionsByEndpoint.map(
- function(connection)
- {
- return connection.waitUntilFinished().exception(function(ex)
- {
- Debug.assert(false);
- });
- }
- )
- ).then(
- function()
- {
- var cons = self._monitor.swapReapedConnections();
- if(cons !== null)
+ return;
+ }
+
+ var self = this;
+ Promise.all(
+ self._connectionsByEndpoint.map(
+ function(connection)
+ {
+ return connection.waitUntilFinished().exception(function(ex)
+ {
+ Debug.assert(false);
+ });
+ }
+ )
+ ).then(
+ function()
+ {
+ var cons = self._monitor.swapReapedConnections();
+ if(cons !== null)
+ {
+ var arr = [];
+ for(var e = self._connectionsByEndpoint.entries; e !== null; e = e.next)
{
- var arr = [];
- for(var e = self._connectionsByEndpoint.entries; e !== null; e = e.next)
+ var connectionList = e.value;
+ for(var i = 0; i < connectionList.length; ++i)
{
- var connectionList = e.value;
- for(var i = 0; i < connectionList.length; ++i)
+ if(arr.indexOf(connectionList[i]) === -1)
{
- if(arr.indexOf(connectionList[i]) === -1)
- {
- arr.push(connectionList[i]);
- }
+ arr.push(connectionList[i]);
}
}
- Debug.assert(cons.length === arr.length);
- self._connectionsByEndpoint.clear();
}
- else
- {
- Debug.assert(self._connectionsByEndpoint.size === 0);
- }
-
- Debug.assert(self._waitPromise !== null);
- self._waitPromise.succeed();
- self._monitor.destroy()
+ Debug.assert(cons.length === arr.length);
+ self._connectionsByEndpoint.clear();
}
- );
- }
- });
-
- Ice.OutgoingConnectionFactory = OutgoingConnectionFactory;
- global.Ice = Ice;
+ else
+ {
+ Debug.assert(self._connectionsByEndpoint.size === 0);
+ }
+
+ Debug.assert(self._waitPromise !== null);
+ self._waitPromise.succeed();
+ self._monitor.destroy();
+ }
+ );
+ }
+});
+Ice.OutgoingConnectionFactory = OutgoingConnectionFactory;
+module.exports.Ice = Ice;
+
+//
+// Value is a Vector<Ice.ConnectionI>
+//
+var ConnectionListMap = Class(HashMap, {
+ __init__: function(h)
+ {
+ HashMap.call(this, h);
+ this.keyComparator = HashMap.compareEquals;
+ },
+ set: function(key, value)
+ {
+ var list = this.get(key);
+ if(list === undefined)
+ {
+ list = [];
+ HashMap.prototype.set.call(this, key, list);
+ }
+ Debug.assert(value instanceof ConnectionI);
+ list.push(value);
+ return undefined;
+ },
+ removeConnection: function(key, conn)
+ {
+ var list = this.get(key);
+ Debug.assert(list !== null);
+ var idx = list.indexOf(conn);
+ Debug.assert(idx !== -1);
+ list.splice(idx, 1);
+ if(list.length === 0)
+ {
+ this.delete(key);
+ }
+ },
+ map: function(fn)
+ {
+ var arr = [];
+ this.forEach(function(c) { arr.push(fn(c)); });
+ return arr;
+ },
+ forEach: function(fn)
+ {
+ for(var e = this._head; e !== null; e = e._next)
+ {
+ for(var i = 0; i < e.value.length; ++i)
+ {
+ fn(e.value[i]);
+ }
+ }
+ }
+});
+
+var ConnectCallback = Class({
+ __init__: function(f, endpoints, more, selType)
+ {
+ this._factory = f;
+ this._endpoints = endpoints;
+ this._hasMore = more;
+ this._selType = selType;
+ this._promise = new Promise();
+ this._index = 0;
+ this._current = null;
+ },
//
- // Value is a Vector<Ice.ConnectionI>
+ // Methods from ConnectionI_StartCallback
//
- var ConnectionListMap = Class(HashMap, {
- __init__: function(h)
- {
- HashMap.call(this, h);
- this.keyComparator = HashMap.compareEquals;
- },
- set: function(key, value)
+ connectionStartCompleted: function(connection)
+ {
+ connection.activate();
+ this._factory.finishGetConnection(this._endpoints, this._current, connection, this);
+ },
+ connectionStartFailed: function(connection, ex)
+ {
+ Debug.assert(this._current !== null);
+
+ if(ex instanceof Ice.LocalException)
{
- var list = this.get(key);
- if(list === undefined)
+ this._factory.handleConnectionException(ex, this._hasMore || this._index < this._endpoints.length);
+ if(ex instanceof Ice.CommunicatorDestroyedException) // No need to continue.
{
- list = [];
- HashMap.prototype.set.call(this, key, list);
+ this._factory.finishGetConnectionEx(this._endpoints, ex, this);
}
- Debug.assert(value instanceof ConnectionI);
- list.push(value);
- return undefined;
- },
- removeConnection: function(key, conn)
- {
- var list = this.get(key);
- Debug.assert(list !== null);
- var idx = list.indexOf(conn);
- Debug.assert(idx !== -1);
- list.splice(idx, 1);
- if(list.length === 0)
+ else if(this._index < this._endpoints.length) // Try the next endpoint.
{
- this.delete(key);
+ this.nextEndpoint();
}
- },
- map: function(fn)
- {
- var arr = [];
- this.forEach(function(c) { arr.push(fn(c)); });
- return arr;
- },
- forEach: function(fn)
- {
- for(var e = this._head; e !== null; e = e._next)
+ else
{
- for(var i = 0; i < e.value.length; ++i)
- {
- fn(e.value[i]);
- }
+ this._factory.finishGetConnectionEx(this._endpoints, ex, this);
}
}
- });
-
- var ConnectCallback = Class({
- __init__: function(f, endpoints, more, selType)
+ else
{
- this._factory = f;
- this._endpoints = endpoints;
- this._hasMore = more;
- this._selType = selType;
- this._promise = new Promise();
- this._index = 0;
- this._current = null;
- },
+ this._factory.finishGetConnectionEx(this._endpoints, ex, this);
+ }
+ },
+ setConnection: function(connection, compress)
+ {
//
- // Methods from ConnectionI_StartCallback
+ // Callback from the factory: the connection to one of the callback
+ // connectors has been established.
//
- connectionStartCompleted: function(connection)
- {
- connection.activate();
- this._factory.finishGetConnection(this._endpoints, this._current, connection, this);
- },
- connectionStartFailed: function(connection, ex)
+ this._promise.succeed(connection, compress);
+ this._factory.decPendingConnectCount(); // Must be called last.
+ },
+ setException: function(ex)
+ {
+ //
+ // Callback from the factory: connection establishment failed.
+ //
+ this._promise.fail(ex);
+ this._factory.decPendingConnectCount(); // Must be called last.
+ },
+ hasEndpoint: function(endpt)
+ {
+ return this.findEndpoint(endpt) !== -1;
+ },
+ findEndpoint: function(endpt)
+ {
+ for(var index = 0; index < this._endpoints.length; ++index)
{
- Debug.assert(this._current !== null);
-
- if(ex instanceof Ice.LocalException)
+ if(endpt.equals(this._endpoints[index]))
{
- this._factory.handleConnectionException(ex, this._hasMore || this._index < this._endpoints.length);
- if(ex instanceof Ice.CommunicatorDestroyedException) // No need to continue.
- {
- this._factory.finishGetConnectionEx(this._endpoints, ex, this);
- }
- else if(this._index < this._endpoints.length) // Try the next endpoint.
- {
- this.nextEndpoint();
- }
- else
- {
- this._factory.finishGetConnectionEx(this._endpoints, ex, this);
- }
+ return index;
}
- else
+ }
+ return -1;
+ },
+ removeEndpoints: function(endpoints)
+ {
+ for(var i = 0; i < endpoints.length; ++i)
+ {
+ var idx = this.findEndpoint(endpoints[i]);
+ if(idx !== -1)
{
- this._factory.finishGetConnectionEx(this._endpoints, ex, this);
+ this._endpoints.splice(idx, 1);
}
- },
- setConnection: function(connection, compress)
+ }
+ this._index = 0;
+ return this._endpoints.length === 0;
+ },
+ removeFromPending: function()
+ {
+ this._factory.removeFromPending(this, this._endpoints);
+ },
+ start: function()
+ {
+ try
{
//
- // Callback from the factory: the connection to one of the callback
- // connectors has been established.
+ // Notify the factory that there's an async connect pending. This is necessary
+ // to prevent the outgoing connection factory to be destroyed before all the
+ // pending asynchronous connects are finished.
//
- this._promise.succeed(connection, compress);
- this._factory.decPendingConnectCount(); // Must be called last.
- },
- setException: function(ex)
+ this._factory.incPendingConnectCount();
+ }
+ catch(ex)
{
- //
- // Callback from the factory: connection establishment failed.
- //
this._promise.fail(ex);
- this._factory.decPendingConnectCount(); // Must be called last.
- },
- hasEndpoint: function(endpt)
- {
- return this.findEndpoint(endpt) !== -1;
- },
- findEndpoint: function(endpt)
- {
- for(var index = 0; index < this._endpoints.length; ++index)
- {
- if(endpt.equals(this._endpoints[index]))
- {
- return index;
- }
- }
- return -1;
- },
- removeEndpoints: function(endpoints)
- {
- for(var i = 0; i < endpoints.length; ++i)
- {
- var idx = this.findEndpoint(endpoints[i]);
- if(idx !== -1)
- {
- this._endpoints.splice(idx, 1);
- }
- }
- this._index = 0;
- return this._endpoints.length === 0;
- },
- removeFromPending: function()
- {
- this._factory.removeFromPending(this, this._endpoints);
- },
- start: function()
+ return;
+ }
+
+ this.getConnection();
+ return this._promise;
+ },
+ getConnection: function()
+ {
+ try
{
- try
+ //
+ // Ask the factory to get a connection.
+ //
+ var compress = { value: false };
+ var connection = this._factory.getConnection(this._endpoints, this, compress);
+ if(connection === null)
{
//
- // Notify the factory that there's an async connect pending. This is necessary
- // to prevent the outgoing connection factory to be destroyed before all the
- // pending asynchronous connects are finished.
+ // A null return value from getConnection indicates that the connection
+ // is being established and that everthing has been done to ensure that
+ // the callback will be notified when the connection establishment is
+ // done.
//
- this._factory.incPendingConnectCount();
- }
- catch(ex)
- {
- this._promise.fail(ex);
return;
}
- this.getConnection();
- return this._promise;
- },
- getConnection: function()
+ this._promise.succeed(connection, compress.value);
+ this._factory.decPendingConnectCount(); // Must be called last.
+ }
+ catch(ex)
{
- try
- {
- //
- // Ask the factory to get a connection.
- //
- var compress = { value: false };
- var connection = this._factory.getConnection(this._endpoints, this, compress);
- if(connection === null)
+ this._promise.fail(ex);
+ this._factory.decPendingConnectCount(); // Must be called last.
+ }
+ },
+ nextEndpoint: function()
+ {
+ var connection = null;
+ try
+ {
+ Debug.assert(this._index < this._endpoints.length);
+ this._current = this._endpoints[this._index++];
+ connection = this._factory.createConnection(this._current.connect(), this._current);
+ var self = this;
+ connection.start().then(
+ function()
{
- //
- // A null return value from getConnection indicates that the connection
- // is being established and that everthing has been done to ensure that
- // the callback will be notified when the connection establishment is
- // done.
- //
- return;
- }
-
- this._promise.succeed(connection, compress.value);
- this._factory.decPendingConnectCount(); // Must be called last.
- }
- catch(ex)
- {
- this._promise.fail(ex);
- this._factory.decPendingConnectCount(); // Must be called last.
- }
- },
- nextEndpoint: function()
+ self.connectionStartCompleted(connection);
+ },
+ function(ex)
+ {
+ self.connectionStartFailed(connection, ex);
+ });
+ }
+ catch(ex)
{
- var connection = null;
- try
- {
- Debug.assert(this._index < this._endpoints.length);
- this._current = this._endpoints[this._index++];
- connection = this._factory.createConnection(this._current.connect(), this._current);
- var self = this;
- connection.start().then(
- function()
- {
- self.connectionStartCompleted(connection);
- },
- function(ex)
- {
- self.connectionStartFailed(connection, ex);
- });
- }
- catch(ex)
- {
- this.connectionStartFailed(connection, ex);
- }
+ this.connectionStartFailed(connection, ex);
}
- });
-
-}(typeof (global) === "undefined" ? window : global));
+ }
+});