All files / src/Ice InstanceExtensions.js

81.54% Statements 327/401
55.73% Branches 34/61
95.45% Functions 21/22
81.54% Lines 327/401

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 40241x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 9996x 9996x 9996x 9996x 41x 41x 41x 16030x 16030x 16030x 41x 41x 41x 6660x 6660x 6660x 41x 41x 41x 891x     891x 891x 891x 41x 41x 41x 314x     314x 314x 314x 41x 41x 41x 1519x     1519x 1519x 1519x 41x 41x 41x 1619x     1619x 1619x 1619x 41x 41x 41x 31x     31x 31x 31x 41x 41x 41x 1039x     1039x 1039x 1039x 41x 41x 41x 1387x     1387x 1387x 1387x 41x 41x 41x 626x     626x 626x 626x 41x 41x 41x 362x 362x 41x 41x 41x 346x 346x 41x 41x 41x 8837x 8837x 41x 41x 41x 2654x 2654x 41x 41x 41x 3541x 41x 41x 41x 2x     2x 2x 41x 41x 41x 4x     4x 4x 41x 41x 41x   41x 41x 41x 67x 67x     67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 28x     28x 28x 67x 67x 39x 39x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x       67x   67x 67x 67x 67x 67x 67x 67x       67x   67x 67x 67x 67x 67x 67x 67x   67x 67x 67x 67x 67x 67x   67x   67x         67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 72x 72x 72x 72x         72x 72x 2x 2x 70x 72x 72x 67x     67x 67x 67x 67x 67x     67x 67x 67x 2x 2x 67x                       41x 41x 41x 67x       67x 2x 2x 2x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 67x                 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 67x         41x 41x 41x 41x 67x 41x 41x 41x  
// Copyright (c) ZeroC, Inc.
 
import { Instance, StateDestroyInProgress, StateDestroyed } from "./Instance.js";
import { DefaultsAndOverrides } from "./DefaultsAndOverrides.js";
import { EndpointFactoryManager } from "./EndpointFactoryManager.js";
import { ImplicitContext } from "./ImplicitContext.js";
import { LocatorManager } from "./LocatorManager.js";
import { ObjectAdapterFactory } from "./ObjectAdapterFactory.js";
import { OutgoingConnectionFactory } from "./OutgoingConnectionFactory.js";
import { Properties } from "./Properties.js";
import { ReferenceFactory } from "./ReferenceFactory.js";
import { RetryQueue } from "./RetryQueue.js";
import { RouterManager } from "./RouterManager.js";
import { Timer } from "./Timer.js";
import { TraceLevels } from "./TraceLevels.js";
import { LocalException } from "./LocalException.js";
import { CommunicatorDestroyedException, InitializationException, ParseException } from "./LocalExceptions.js";
import { getProcessLogger } from "./ProcessLogger.js";
import { ToStringMode } from "./ToStringMode.js";
import { ProtocolInstance } from "./ProtocolInstance.js";
import { TcpEndpointFactory } from "./TcpEndpointFactory.js";
import { WSEndpointFactory } from "./WSEndpointFactory.js";
import { Promise } from "./Promise.js";
import { ConnectionOptions } from "./ConnectionOptions.js";
import { StringUtil } from "./StringUtil.js";
import { FileLogger } from "./FileLogger.js";
 
import { Ice as Ice_Router } from "./Router.js";
const { RouterPrx } = Ice_Router;
import { Ice as Ice_Locator } from "./Locator.js";
const { LocatorPrx } = Ice_Locator;
 
import { Ice as Ice_EndpointTypes } from "./EndpointTypes.js";
const { TCPEndpointType, WSEndpointType, SSLEndpointType, WSSEndpointType } = Ice_EndpointTypes;
 
Instance.prototype.initializationData = function () {
    // No check for destruction. It must be possible to access the initialization data after destruction.
    //
    // This value is immutable.
    return this._initData;
};
 
Instance.prototype.traceLevels = function () {
    // This value is immutable.
    console.assert(this._traceLevels !== null);
    return this._traceLevels;
};
 
Instance.prototype.defaultsAndOverrides = function () {
    // This value is immutable.
    console.assert(this._defaultsAndOverrides !== null);
    return this._defaultsAndOverrides;
};
 
Instance.prototype.routerManager = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._routerManager !== null);
    return this._routerManager;
};
 
Instance.prototype.locatorManager = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._locatorManager !== null);
    return this._locatorManager;
};
 
Instance.prototype.referenceFactory = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._referenceFactory !== null);
    return this._referenceFactory;
};
 
Instance.prototype.outgoingConnectionFactory = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._outgoingConnectionFactory !== null);
    return this._outgoingConnectionFactory;
};
 
Instance.prototype.objectAdapterFactory = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._objectAdapterFactory !== null);
    return this._objectAdapterFactory;
};
 
Instance.prototype.retryQueue = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._retryQueue !== null);
    return this._retryQueue;
};
 
Instance.prototype.timer = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._timer !== null);
    return this._timer;
};
 
Instance.prototype.endpointFactoryManager = function () {
    if (this._state === StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    console.assert(this._endpointFactoryManager !== null);
    return this._endpointFactoryManager;
};
 
Instance.prototype.messageSizeMax = function () {
    // This value is immutable.
    return this._messageSizeMax;
};
 
Instance.prototype.batchAutoFlushSize = function () {
    // This value is immutable.
    return this._batchAutoFlushSize;
};
 
Instance.prototype.classGraphDepthMax = function () {
    // This value is immutable.
    return this._classGraphDepthMax;
};
 
Instance.prototype.toStringMode = function () {
    // this value is immutable
    return this._toStringMode;
};
 
Instance.prototype.getImplicitContext = function () {
    return this._implicitContext;
};
 
Instance.prototype.setDefaultLocator = function (locator) {
    if (this._state == StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    this._referenceFactory = this._referenceFactory.setDefaultLocator(locator);
};
 
Instance.prototype.setDefaultRouter = function (router) {
    if (this._state == StateDestroyed) {
        throw new CommunicatorDestroyedException();
    }
 
    this._referenceFactory = this._referenceFactory.setDefaultRouter(router);
};
 
Instance.prototype.setLogger = function (logger) {
    this._initData.logger = logger;
};
 
Instance.prototype.finishSetup = function (communicator) {
    try {
        if (this._initData.properties === null) {
            this._initData.properties = new Properties();
        }
 
        this._clientConnectionOptions = new ConnectionOptions(
            this._initData.properties.getIcePropertyAsInt("Ice.Connection.Client.ConnectTimeout"),
            this._initData.properties.getIcePropertyAsInt("Ice.Connection.Client.CloseTimeout"),
            this._initData.properties.getIcePropertyAsInt("Ice.Connection.Client.IdleTimeout"),
            this._initData.properties.getIcePropertyAsInt("Ice.Connection.Client.EnableIdleCheck") > 0,
            this._initData.properties.getIcePropertyAsInt("Ice.Connection.Client.InactivityTimeout"),
        );
 
        const programName = this._initData.properties.getIceProperty("Ice.ProgramName");
        const logFile = this._initData.properties.getIceProperty("Ice.LogFile");
        if (logFile.length > 0) {
            if (FileLogger === null) {
                throw new InitializationException("Ice.LogFile property is not supported in Web Browsers");
            }
            this._initData.logger = new FileLogger(programName, logFile);
        }
 
        if (this._initData.logger === null) {
            this._initData.logger = getProcessLogger(programName);
        }
 
        this._traceLevels = new TraceLevels(this._initData.properties);
 
        this._defaultsAndOverrides = new DefaultsAndOverrides(this._initData.properties, this._initData.logger);
 
        // The maximum size of an Ice protocol message in bytes. This is limited to 0x7fffffff, which corresponds to
        // the maximum value of a 32-bit signed integer.
        const messageSizeMaxUpperLimit = 0x7fffffff;
        let messageSizeMax = this._initData.properties.getIcePropertyAsInt("Ice.MessageSizeMax");
        if (messageSizeMax > messageSizeMaxUpperLimit / 1024) {
            throw new InitializationException(
                `Ice.MessageSizeMax '${messageSizeMax}' is too large, it must be less than or equal to '${messageSizeMaxUpperLimit / 1024}' KiB`,
            );
        } else if (messageSizeMax < 1) {
            this._messageSizeMax = messageSizeMaxUpperLimit;
        } else {
            // The property is specified in kibibytes (KiB); _messageSizeMax is stored in bytes.
            this._messageSizeMax = messageSizeMax * 1024;
        }
 
        let batchAutoFlushSize = this._initData.properties.getIcePropertyAsInt("Ice.BatchAutoFlushSize");
        if (batchAutoFlushSize > messageSizeMaxUpperLimit / 1024) {
            throw new InitializationException(
                `Ice.BatchAutoFlushSize '${batchAutoFlushSize}' is too large, it must be less than or equal to '${messageSizeMaxUpperLimit / 1024}' KiB`,
            );
        } else if (batchAutoFlushSize < 1) {
            this._batchAutoFlushSize = messageSizeMaxUpperLimit;
        } else {
            // The property is specified in kibibytes (KiB); _batchAutoFlushSize is stored in bytes.
            this._batchAutoFlushSize = batchAutoFlushSize * 1024;
        }
 
        let classGraphDepthMax = this._initData.properties.getIcePropertyAsInt("Ice.ClassGraphDepthMax");
        if (classGraphDepthMax < 1) {
            this._classGraphDepthMax = 0x7fffffff;
        } else {
            this._classGraphDepthMax = classGraphDepthMax;
        }
 
        const toStringModeStr = this._initData.properties.getIceProperty("Ice.ToStringMode");
        if (toStringModeStr === "ASCII") {
            this._toStringMode = ToStringMode.ASCII;
        } else if (toStringModeStr === "Compat") {
            this._toStringMode = ToStringMode.Compat;
        } else if (toStringModeStr !== "Unicode") {
            throw new InitializationException(
                `illegal value '${toStringModeStr}' in property Ice.ToStringMode; expected 'Unicode', 'ASCII', or 'Compat'`,
            );
        }
 
        this._implicitContext = ImplicitContext.create(this._initData.properties.getIceProperty("Ice.ImplicitContext"));
 
        this._routerManager = new RouterManager();
 
        this._locatorManager = new LocatorManager(this._initData.properties);
 
        this._referenceFactory = new ReferenceFactory(this, communicator);
 
        this._endpointFactoryManager = new EndpointFactoryManager(this);
 
        const tcpInstance = new ProtocolInstance(this, TCPEndpointType, "tcp", false);
        const tcpEndpointFactory = new TcpEndpointFactory(tcpInstance);
        this._endpointFactoryManager.add(tcpEndpointFactory);
 
        const wsInstance = new ProtocolInstance(this, WSEndpointType, "ws", false);
        const wsEndpointFactory = new WSEndpointFactory(wsInstance, tcpEndpointFactory.clone(wsInstance));
        this._endpointFactoryManager.add(wsEndpointFactory);
 
        const sslInstance = new ProtocolInstance(this, SSLEndpointType, "ssl", true);
        const sslEndpointFactory = new TcpEndpointFactory(sslInstance);
        this._endpointFactoryManager.add(sslEndpointFactory);
 
        const wssInstance = new ProtocolInstance(this, WSSEndpointType, "wss", true);
        const wssEndpointFactory = new WSEndpointFactory(wssInstance, sslEndpointFactory.clone(wssInstance));
        this._endpointFactoryManager.add(wssEndpointFactory);
 
        this._outgoingConnectionFactory = new OutgoingConnectionFactory(communicator, this);
 
        this._objectAdapterFactory = new ObjectAdapterFactory(this, communicator);
 
        this._retryQueue = new RetryQueue(this);
        const retryIntervals = this._initData.properties.getIcePropertyAsList("Ice.RetryIntervals");
        if (retryIntervals.length > 0) {
            this._retryIntervals = [];
 
            for (let i = 0; i < retryIntervals.length; i++) {
                let v;
                try {
                    v = StringUtil.toInt32(retryIntervals[i]);
                } catch {
                    throw new ParseException(
                        `invalid value '${retryIntervals[i]}' in property Ice.RetryIntervals; expected a list of integers`,
                    );
                }
                // If -1 is the first value, no retry and wait intervals.
                if (i === 0 && v === -1) {
                    break;
                }
 
                this._retryIntervals[i] = v > 0 ? v : 0;
            }
        } else {
            this._retryIntervals = [0];
        }
 
        this._timer = new Timer(this._initData.logger);
 
        const router = communicator.propertyToProxy("Ice.Default.Router");
        if (router !== null) {
            this._referenceFactory = this._referenceFactory.setDefaultRouter(new RouterPrx(router));
        }
 
        const loc = communicator.propertyToProxy("Ice.Default.Locator");
        if (loc !== null) {
            this._referenceFactory = this._referenceFactory.setDefaultLocator(new LocatorPrx(loc));
        }
    } catch (ex) {
        if (ex instanceof LocalException) {
            this.destroy().catch(err => {
                if (this._initData.logger !== null) {
                    this._initData.logger.warning(err);
                } else {
                    console.error(err);
                }
            });
        }
        throw ex;
    }
};
 
Instance.prototype.destroy = async function () {
    if (this._state == StateDestroyInProgress) {
        // Destroy is in progress, wait for it to be done. This is necessary in case destroy() is called multiple times.
        console.assert(this._destroyPromise !== null);
        return this._destroyPromise;
    } else if (this._state == StateDestroyed) {
        // Already destroyed.
        return;
    }
    this._state = StateDestroyInProgress;
    this._destroyPromise = new Promise();
 
    try {
        if (this._outgoingConnectionFactory !== null) {
            this._outgoingConnectionFactory.destroy();
        }
 
        if (this._objectAdapterFactory !== null) {
            this._objectAdapterFactory.destroy();
        }
 
        if (this._outgoingConnectionFactory !== null) {
            await this._outgoingConnectionFactory.waitUntilFinished();
        }
 
        if (this._retryQueue) {
            this._retryQueue.destroy();
        }
 
        if (this._timer) {
            this._timer.destroy();
        }
 
        if (this._routerManager) {
            this._routerManager.destroy();
        }
        if (this._locatorManager) {
            this._locatorManager.destroy();
        }
 
        if (this._initData.properties.getIcePropertyAsInt("Ice.Warn.UnusedProperties") > 0) {
            const unusedProperties = this._initData.properties.getUnusedProperties();
            if (unusedProperties.length > 0) {
                const message = [];
                message.push("The following properties were set but never read:");
                unusedProperties.forEach(p => message.push("\n    ", p));
                this._initData.logger.warning(message.join(""));
            }
        }
 
        this._objectAdapterFactory = null;
        this._outgoingConnectionFactory = null;
        this._retryQueue = null;
        this._timer = null;
 
        this._referenceFactory = null;
        this._routerManager = null;
        this._locatorManager = null;
        this._endpointFactoryManager = null;
 
        this._state = StateDestroyed;
        this._destroyPromise.resolve();
    } catch (ex) {
        this._state = StateDestroyed;
        this._destroyPromise.reject(ex);
        throw ex;
    }
};
 
Object.defineProperty(Instance.prototype, "clientConnectionOptions", {
    get: function () {
        return this._clientConnectionOptions;
    },
    enumerable: true,
});