All files / src/Ice Reference.js

87.47% Statements 915/1046
88.68% Branches 196/221
79.12% Functions 72/91
87.47% Lines 915/1046

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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 104741x 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 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 1290x 41x 41x 15661x 15661x 41x 41x 1275x 1275x 41x 41x 3553x 3553x 41x 41x 4023x 4023x 41x 41x 4244x 4244x 41x 41x 4929x 4929x 41x 41x 4022x 4022x 41x 41x 19850x 19850x 41x 41x 3560x 3560x 41x 41x 3758x 3758x 41x 41x 4219x 4219x 41x 41x 41x 41x 41x 41x 41x 15x 4x 4x 15x 15x 4x 15x 11x 11x 15x 15x 41x 41x 31x 31x 31x 31x 41x 41x 9x 9x 9x 9x 41x 41x 19x 19x 19x 19x 41x 41x 14x 14x 14x 14x 41x 41x 12x 12x 12x 12x 41x 41x 196x 196x 196x 196x 196x       196x 196x 196x 196x 196x 196x 196x 196x 41x 41x 41x 41x 41x 2101x 2101x 41x 41x 41x 41x 41x 189x 189x 189x 189x 189x 189x 189x 189x 189x 189x 189x       189x 189x 189x 189x 189x 189x 181x 181x 181x 189x 189x 189x 41x 41x 41x 41x 41x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x       110x 110x 110x 110x 110x                               110x 110x 110x 110x 110x 110x 110x 110x       110x 110x       110x 110x       110x 110x       110x 110x       110x 110x 110x 1x 1x 1x 1x 110x 110x 110x 110x 101x 110x 9x 9x 9x 110x 110x 110x 110x 110x 41x 41x 295x 295x 295x 295x 295x 1x 1x 294x 295x 5x 5x 289x 295x 3x 3x 286x 295x 1x 1x 285x 295x     285x 295x 1x 1x 284x 295x 1x 1x 283x 283x 295x 41x 41x 416x 416x 416x 416x 416x 41x 41x 41x 41x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 41x 41x     41x 41x     41x 41x     41x 41x     41x 41x 35x 35x 41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x     41x 41x 2x 2x 2x 2x 41x 41x 1x 1x 41x 41x     41x 41x     41x 41x     41x 41x     41x 41x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 41x 41x 8x 8x     8x 8x 8x 8x 8x 41x 41x 28x 28x 41x 41x 16x     16x 13x 13x 16x     3x 16x 41x 41x 41x 41x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 1254x 203x 203x 1254x 756x 756x 1254x 1254x 1254x 1254x 41x 41x 1322x 1322x 41x 41x 14719x 14719x 41x 41x 3057x 3057x 41x 41x 40x 40x 41x 41x 2626x 2626x 41x 41x 3151x 3151x 41x 41x 35x 35x 41x 41x 14x 14x 41x 41x 21x 21x 21x 21x 41x 41x 12x 12x 12x 1x 1x 1x 1x 12x 12x 12x 41x 41x 2x     2x 2x 2x 2x 2x 41x 41x 60x     60x 60x 60x 60x 60x 60x 41x 41x 168x 168x     168x 168x 168x 168x 41x 41x 32x 32x     32x 32x 32x 32x 41x 41x 8x 8x 8x 8x 41x 41x 5x 5x 5x 5x 41x 41x 27x 27x 27x 27x 41x 41x 12x 12x 12x 12x 12x 41x 41x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 41x 41x 7615x 7615x 41x 41x 6722x 6722x 41x 41x 189x 189x 189x 189x 155x 155x 156x 156x 155x 187x 34x 34x 189x 41x 41x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 19x 25x 25x 25x 25x 25x 19x 108x                                 108x 108x 41x 41x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 7x 7x 7x 41x 41x 196x 196x 196x 196x 41x 41x 448x 156x 156x 447x     292x 447x 12x 12x 280x 448x 14x 14x 448x 11x 11x 447x 1x 1x 447x 1x 1x 447x 1x 1x 447x 1x 1x 447x 2x 2x 447x 1x 1x 248x 448x 41x 41x 3553x 3553x 3553x 3553x 41x 41x 105x 105x 41x 41x 41x 1275x 1275x 41x 41x 3553x 3553x 3553x 10x 10x 10x 10x 10x 10x 10x 8x 8x 8x 8x     10x 10x 3553x 3543x 3543x 3553x 3553x 3553x 3553x 41x 41x 3558x 455x 455x 455x 3103x 3103x 3103x 3103x 3103x 1093x 1093x 2x 2x 2x 1091x 1091x 1091x 29x         29x 29x 29x 29x 15x 15x                   15x 15x 15x 14x 14x 1091x 3103x 3103x 3108x     3558x 41x 41x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 41x 41x 404x 404x 404x 404x 404x 404x 41x 41x 1159x 1159x 1159x 1159x 1255x 1255x 1159x 41x 41x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1525x 1525x 1525x 1525x 1647x 1647x 1647x 1647x 1647x 1525x 1525x 1554x 29x 29x 29x 1554x       1554x 1554x 1554x 1554x 41x 41x 1554x 1554x 2x 2x 1552x 1552x 1552x 1552x 1552x 1552x 1554x 1523x 1523x 1523x 1523x 1523x 1523x 1523x 1523x 1523x 1554x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1552x 1554x 41x 41x 41x 41x 1552x 1552x 1552x 1552x 1552x 1552x 41x 41x 1504x 1504x 1504x 1504x 1504x 1504x 6x 6x 1504x 1504x 41x 41x 75x 64x 64x 75x 75x 48x 48x 48x 27x 27x 27x 27x 27x 27x 27x 75x 41x  
// Copyright (c) ZeroC, Inc.
 
import { ArrayUtil } from "./ArrayUtil.js";
import { BatchRequestQueue } from "./BatchRequestQueue.js";
import { FixedRequestHandler } from "./FixedRequestHandler.js";
import { EndpointSelectionType } from "./EndpointSelectionType.js";
import { HashUtil } from "./HashUtil.js";
import { Ice as Ice_Identity } from "./Identity.js";
const { Identity } = Ice_Identity;
import { identityToString } from "./IdentityToString.js";
import { MapUtil } from "./MapUtil.js";
import { ObjectPrx } from "./ObjectPrx.js";
import { OpaqueEndpointI } from "./OpaqueEndpoint.js";
import { ReferenceMode } from "./ReferenceMode.js";
import { StringUtil } from "./StringUtil.js";
import {
    Encoding_1_0,
    Encoding_1_1,
    Protocol_1_0,
    encodingVersionToString,
    protocolVersionToString,
} from "./Protocol.js";
import { FixedProxyException, NoEndpointException } from "./LocalExceptions.js";
import { Promise } from "./Promise.js";
import { ConnectRequestHandler } from "./ConnectRequestHandler.js";
 
const _emptyContext = new Map();
const _emptyEndpoints = [];
 
export class Reference {
    constructor(instance, communicator, identity, facet, mode, protocol, encoding, invocationTimeout, context) {
        //
        // Validate string arguments.
        //
        console.assert(identity === undefined || identity.name !== null);
        console.assert(identity === undefined || identity.category !== null);
        console.assert(facet === undefined || facet !== null);
 
        this._instance = instance;
        this._communicator = communicator;
        this._mode = mode;
        this._identity = identity;
        this._context = context === undefined || context === null ? _emptyContext : context;
        this._facet = facet;
        this._protocol = protocol;
        this._encoding = encoding;
        this._invocationTimeout = invocationTimeout;
    }
 
    getMode() {
        return this._mode;
    }
 
    get isBatch() {
        return this._mode == ReferenceMode.ModeBatchOneway || this._mode == ReferenceMode.ModeBatchDatagram;
    }
 
    get isTwoway() {
        return this._mode == ReferenceMode.ModeTwoway;
    }
 
    getProtocol() {
        return this._protocol;
    }
 
    getEncoding() {
        return this._encoding;
    }
 
    getIdentity() {
        return this._identity;
    }
 
    getFacet() {
        return this._facet;
    }
 
    getInstance() {
        return this._instance;
    }
 
    getContext() {
        return this._context; // Map
    }
 
    getInvocationTimeout() {
        return this._invocationTimeout;
    }
 
    getCommunicator() {
        return this._communicator;
    }
 
    //
    // The change* methods (here and in derived classes) create
    // a new reference based on the existing one, with the
    // corresponding value changed.
    //
    changeContext(newContext) {
        if (newContext === undefined || newContext === null) {
            newContext = _emptyContext;
        }
        const r = this._instance.referenceFactory().copy(this);
        if (newContext.size === 0) {
            r._context = _emptyContext;
        } else {
            r._context = new Map(newContext);
        }
        return r;
    }
 
    changeMode(newMode) {
        const r = this._instance.referenceFactory().copy(this);
        r._mode = newMode;
        return r;
    }
 
    changeIdentity(newIdentity) {
        const r = this._instance.referenceFactory().copy(this);
        r._identity = new Identity(newIdentity.name, newIdentity.category);
        return r;
    }
 
    changeFacet(newFacet) {
        const r = this._instance.referenceFactory().copy(this);
        r._facet = newFacet;
        return r;
    }
 
    changeInvocationTimeout(newInvocationTimeout) {
        const r = this._instance.referenceFactory().copy(this);
        r._invocationTimeout = newInvocationTimeout;
        return r;
    }
 
    changeEncoding(newEncoding) {
        const r = this._instance.referenceFactory().copy(this);
        r._encoding = newEncoding;
        return r;
    }
 
    hashCode() {
        let h = 5381;
        h = HashUtil.addNumber(h, this._mode);
        h = HashUtil.addHashable(h, this._identity);
        if (this._context !== null && this._context !== undefined) {
            for (const [key, value] of this._context) {
                h = HashUtil.addString(h, key);
                h = HashUtil.addString(h, value);
            }
        }
        h = HashUtil.addString(h, this._facet);
        h = HashUtil.addHashable(h, this._protocol);
        h = HashUtil.addHashable(h, this._encoding);
        h = HashUtil.addNumber(h, this._invocationTimeout);
 
        return h;
    }
 
    //
    // Utility methods
    //
    isFixed() {
        return false;
    }
 
    //
    // Marshal the reference.
    //
    streamWrite(s) {
        //
        // Don't write the identity here. Operations calling streamWrite
        // write the identity.
        //
 
        //
        // For compatibility with the old FacetPath.
        //
        if (this._facet.length === 0) {
            s.writeSize(0); // Empty string sequence
        } else {
            s.writeSize(1); // String sequence with one element
            s.writeString(this._facet);
        }
 
        s.writeByte(this._mode);
 
        s.writeBool(false); // Reserved for secure (not used anymore).
 
        if (!s.getEncoding().equals(Encoding_1_0)) {
            this._protocol._write(s);
            this._encoding._write(s);
        }
 
        // Derived class writes the remainder of the reference.
    }
 
    //
    // Convert the reference to its string form.
    //
    toString() {
        //
        // WARNING: Certain features, such as proxy validation in Glacier2,
        // depend on the format of proxy strings. Changes to toString() and
        // methods called to generate parts of the reference string could break
        // these features. Please review for all features that depend on the
        // format of proxyToString() before changing this and related code.
        //
        const s = [];
 
        const toStringMode = this._instance.toStringMode();
 
        //
        // If the encoded identity string contains characters which
        // the reference parser uses as separators, then we enclose
        // the identity string in quotes.
        //
 
        const id = identityToString(this._identity, toStringMode);
        if (id.search(/[ :@]/) != -1) {
            s.push('"');
            s.push(id);
            s.push('"');
        } else {
            s.push(id);
        }
 
        if (this._facet.length > 0) {
            //
            // If the encoded facet string contains characters which
            // the reference parser uses as separators, then we enclose
            // the facet string in quotes.
            //
            s.push(" -f ");
            const fs = StringUtil.escapeString(this._facet, "", toStringMode);
            if (fs.search(/[ :@]/) != -1) {
                s.push('"');
                s.push(fs);
                s.push('"');
            } else {
                s.push(fs);
            }
        }
 
        switch (this._mode) {
            case ReferenceMode.ModeTwoway: {
                // Don't print the default mode.
                break;
            }
 
            case ReferenceMode.ModeOneway: {
                s.push(" -o");
                break;
            }
 
            case ReferenceMode.ModeBatchOneway: {
                s.push(" -O");
                break;
            }
 
            case ReferenceMode.ModeDatagram: {
                s.push(" -d");
                break;
            }
 
            case ReferenceMode.ModeBatchDatagram: {
                s.push(" -D");
                break;
            }
 
            default: {
                console.assert(false);
                break;
            }
        }
 
        if (!this._protocol.equals(Protocol_1_0)) {
            // We print the protocol unless it's 1.0.
            s.push(" -p ");
            s.push(protocolVersionToString(this._protocol));
        }
 
        // We print the encoding if it's not 1.1 or if Ice.Default.EncodingVersion is set to something other than 1.1.
        if (
            !this._encoding.equals(Encoding_1_1) ||
            !this._instance.defaultsAndOverrides().defaultEncoding.equals(Encoding_1_1)
        ) {
            s.push(" -e ");
            s.push(encodingVersionToString(this._encoding));
        }
 
        return s.join("");
 
        // Derived class writes the remainder of the string.
    }
 
    equals(r) {
        //
        // Note: if(this === r) and type test are performed by each non-abstract derived class.
        //
 
        if (this._mode !== r._mode) {
            return false;
        }
 
        if (!this._identity.equals(r._identity)) {
            return false;
        }
 
        if (!MapUtil.equals(this._context, r._context)) {
            return false;
        }
 
        if (this._facet !== r._facet) {
            return false;
        }
 
        if (!this._protocol.equals(r._protocol)) {
            return false;
        }
 
        if (!this._encoding.equals(r._encoding)) {
            return false;
        }
 
        if (this._invocationTimeout !== r._invocationTimeout) {
            return false;
        }
 
        return true;
    }
 
    copyMembers(r) {
        //
        // Copy the members that are not passed to the constructor.
        //
        r._context = this._context;
    }
}
 
export class FixedReference extends Reference {
    constructor(
        instance,
        communicator,
        identity,
        facet,
        mode,
        protocol,
        encoding,
        connection,
        invocationTimeout,
        context,
    ) {
        super(instance, communicator, identity, facet, mode, protocol, encoding, invocationTimeout, context);
        this._fixedConnection = connection;
    }
 
    getEndpoints() {
        return _emptyEndpoints;
    }
 
    getAdapterId() {
        return "";
    }
 
    getRouterInfo() {
        return null;
    }
 
    getLocatorInfo() {
        return null;
    }
 
    getCacheConnection() {
        return true;
    }
 
    getEndpointSelection() {
        return EndpointSelectionType.Random;
    }
 
    getLocatorCacheTimeout() {
        return 0;
    }
 
    getConnectionId() {
        return "";
    }
 
    changeAdapterId() {
        throw new FixedProxyException();
    }
 
    changeEndpoints() {
        throw new FixedProxyException();
    }
 
    changeLocator() {
        throw new FixedProxyException();
    }
 
    changeRouter() {
        throw new FixedProxyException();
    }
 
    changeCacheConnection() {
        throw new FixedProxyException();
    }
 
    changeEndpointSelection() {
        throw new FixedProxyException();
    }
 
    changeLocatorCacheTimeout() {
        throw new FixedProxyException();
    }
 
    changeConnectionId() {
        throw new FixedProxyException();
    }
 
    changeConnection(newConnection) {
        const r = this.getInstance().referenceFactory().copy(this);
        r._fixedConnection = newConnection;
        return r;
    }
 
    isFixed() {
        return true;
    }
 
    isIndirect() {
        return false;
    }
 
    isWellKnown() {
        return false;
    }
 
    streamWrite() {
        throw new FixedProxyException();
    }
 
    toProperty() {
        throw new FixedProxyException();
    }
 
    clone() {
        const r = new FixedReference(
            this.getInstance(),
            this.getCommunicator(),
            this.getIdentity(),
            this.getFacet(),
            this.getMode(),
            this.getProtocol(),
            this.getEncoding(),
            this._fixedConnection,
            this.getInvocationTimeout(),
            this.getContext(),
        );
        this.copyMembers(r);
        return r;
    }
 
    getRequestHandler() {
        const mode = this.getMode();
        if (mode == ReferenceMode.ModeDatagram || mode == ReferenceMode.ModeBatchDatagram) {
            throw new NoEndpointException(new ObjectPrx(this));
        }
 
        // Throw in case our connection is already destroyed.
        this._fixedConnection.throwException();
        return new FixedRequestHandler(this, this._fixedConnection);
    }
 
    get batchRequestQueue() {
        return this._fixedConnection.getBatchRequestQueue();
    }
 
    equals(rhs) {
        if (this === rhs) {
            return true;
        }
        if (!(rhs instanceof FixedReference)) {
            return false;
        }
        if (!super.equals(rhs)) {
            return false;
        }
        return this._fixedConnection == rhs._fixedConnection;
    }
}
 
export class RoutableReference extends Reference {
    constructor(
        instance,
        communicator,
        identity,
        facet,
        mode,
        protocol,
        encoding,
        endpoints,
        adapterId,
        locatorInfo,
        routerInfo,
        cacheConnection,
        endpointSelection,
        locatorCacheTimeout,
        invocationTimeout,
        context,
    ) {
        super(instance, communicator, identity, facet, mode, protocol, encoding, invocationTimeout, context);
        this._endpoints = endpoints;
        this._adapterId = adapterId;
        this._locatorInfo = locatorInfo;
        this._routerInfo = routerInfo;
        this._cacheConnection = cacheConnection;
        this._endpointSelection = endpointSelection;
        this._locatorCacheTimeout = locatorCacheTimeout;
 
        if (this._endpoints === null) {
            this._endpoints = _emptyEndpoints;
        }
        if (this._adapterId === null) {
            this._adapterId = "";
        }
        this._connectionId = "";
        this.setBatchRequestQueue();
        console.assert(this._adapterId.length === 0 || this._endpoints.length === 0);
    }
 
    getEndpoints() {
        return this._endpoints;
    }
 
    getAdapterId() {
        return this._adapterId;
    }
 
    getRouterInfo() {
        return this._routerInfo;
    }
 
    getLocatorInfo() {
        return this._locatorInfo;
    }
 
    getCacheConnection() {
        return this._cacheConnection;
    }
 
    getEndpointSelection() {
        return this._endpointSelection;
    }
 
    getLocatorCacheTimeout() {
        return this._locatorCacheTimeout;
    }
 
    getConnectionId() {
        return this._connectionId;
    }
 
    changeMode(newMode) {
        const r = super.changeMode(newMode);
        r.setBatchRequestQueue();
        return r;
    }
 
    changeEncoding(newEncoding) {
        const r = super.changeEncoding(newEncoding);
        if (r !== this) {
            if (r._locatorInfo !== null && !r._locatorInfo.getLocator().ice_getEncodingVersion().equals(newEncoding)) {
                r._locatorInfo = this.getInstance()
                    .locatorManager()
                    .find(r._locatorInfo.getLocator().ice_encodingVersion(newEncoding));
            }
        }
        return r;
    }
 
    changeAdapterId(newAdapterId) {
        if (this._adapterId === newAdapterId) {
            return this;
        }
        const r = this.getInstance().referenceFactory().copy(this);
        r._adapterId = newAdapterId;
        r._endpoints = _emptyEndpoints;
        return r;
    }
 
    changeEndpoints(newEndpoints) {
        if (ArrayUtil.equals(newEndpoints, this._endpoints, (e1, e2) => e1.equals(e2))) {
            return this;
        }
        const r = this.getInstance().referenceFactory().copy(this);
        r._endpoints = newEndpoints;
        r._adapterId = "";
        r.applyOverrides(r._endpoints);
        return r;
    }
 
    changeLocator(newLocator) {
        const newLocatorInfo = this.getInstance().locatorManager().find(newLocator);
        if (newLocatorInfo !== null && this._locatorInfo !== null && newLocatorInfo.equals(this._locatorInfo)) {
            return this;
        }
        const r = this.getInstance().referenceFactory().copy(this);
        r._locatorInfo = newLocatorInfo;
        return r;
    }
 
    changeRouter(newRouter) {
        const newRouterInfo = this.getInstance().routerManager().find(newRouter);
        if (newRouterInfo !== null && this._routerInfo !== null && newRouterInfo.equals(this._routerInfo)) {
            return this;
        }
        const r = this.getInstance().referenceFactory().copy(this);
        r._routerInfo = newRouterInfo;
        return r;
    }
 
    changeCacheConnection(newCache) {
        const r = this.getInstance().referenceFactory().copy(this);
        r._cacheConnection = newCache;
        return r;
    }
 
    changeEndpointSelection(newType) {
        const r = this.getInstance().referenceFactory().copy(this);
        r._endpointSelection = newType;
        return r;
    }
 
    changeLocatorCacheTimeout(newTimeout) {
        const r = this.getInstance().referenceFactory().copy(this);
        r._locatorCacheTimeout = newTimeout;
        return r;
    }
 
    changeConnectionId(id) {
        const r = this.getInstance().referenceFactory().copy(this);
        r._connectionId = id;
        r._endpoints = this._endpoints.map(endpoint => endpoint.changeConnectionId(id));
        return r;
    }
 
    changeConnection(newConnection) {
        return new FixedReference(
            this.getInstance(),
            this.getCommunicator(),
            this.getIdentity(),
            this.getFacet(),
            this.getMode(),
            this.getProtocol(),
            this.getEncoding(),
            newConnection,
            this.getInvocationTimeout(),
            this.getContext(),
        );
    }
 
    isIndirect() {
        return this._endpoints.length === 0;
    }
 
    isWellKnown() {
        return this._endpoints.length === 0 && this._adapterId.length === 0;
    }
 
    streamWrite(s) {
        super.streamWrite(s);
 
        s.writeSize(this._endpoints.length);
        if (this._endpoints.length > 0) {
            console.assert(this._adapterId.length === 0);
            this._endpoints.forEach(endpoint => {
                s.writeShort(endpoint.type());
                endpoint.streamWrite(s);
            });
        } else {
            s.writeString(this._adapterId); // Adapter id.
        }
    }
 
    toString() {
        //
        // WARNING: Certain features, such as proxy validation in Glacier2,
        // depend on the format of proxy strings. Changes to toString() and
        // methods called to generate parts of the reference string could break
        // these features. Please review for all features that depend on the
        // format of proxyToString() before changing this and related code.
        //
        const s = [];
        s.push(super.toString());
        if (this._endpoints.length > 0) {
            this._endpoints.forEach(endpoint => {
                const endp = endpoint.toString();
                if (endp !== null && endp.length > 0) {
                    s.push(":");
                    s.push(endp);
                }
            });
        } else if (this._adapterId.length > 0) {
            s.push(" @ ");

            //
            // If the encoded adapter id string contains characters which
            // the reference parser uses as separators, then we enclose
            // the adapter id string in quotes.
            //
            const a = StringUtil.escapeString(this._adapterId, null, this._instance.toStringMode());
            if (a.search(/[ :@]/) != -1) {
                s.push('"');
                s.push(a);
                s.push('"');
            } else {
                s.push(a);
            }
        }
        return s.join("");
    }
 
    toProperty(prefix) {
        const properties = new Map();
 
        properties.set(prefix, this.toString());
        properties.set(prefix + ".ConnectionCached", this._cacheConnection ? "1" : "0");
        properties.set(
            prefix + ".EndpointSelection",
            this._endpointSelection === EndpointSelectionType.Random ? "Random" : "Ordered",
        );
 
        properties.set(prefix + ".LocatorCacheTimeout", String(this._locatorCacheTimeout));
        properties.set(prefix + ".InvocationTimeout", String(this.getInvocationTimeout()));
 
        if (this._routerInfo !== null) {
            this._routerInfo
                .getRouter()
                ._getReference()
                .toProperty(prefix + ".Router")
                .forEach((value, key) => properties.set(key, value));
        }
 
        if (this._locatorInfo !== null) {
            this._locatorInfo
                .getLocator()
                ._getReference()
                .toProperty(prefix + ".Locator")
                .forEach((value, key) => properties.set(key, value));
        }
 
        return properties;
    }
 
    hashCode() {
        let h = super.hashCode();
        h = HashUtil.addString(h, this._adapterId);
        return h;
    }
 
    equals(rhs) {
        if (this === rhs) {
            return true;
        }
        if (!(rhs instanceof RoutableReference)) {
            return false;
        }
 
        if (!super.equals(rhs)) {
            return false;
        }
 
        if (this._locatorInfo === null ? rhs._locatorInfo !== null : !this._locatorInfo.equals(rhs._locatorInfo)) {
            return false;
        }
        if (this._routerInfo === null ? rhs._routerInfo !== null : !this._routerInfo.equals(rhs._routerInfo)) {
            return false;
        }
        if (this._cacheConnection !== rhs._cacheConnection) {
            return false;
        }
        if (this._endpointSelection !== rhs._endpointSelection) {
            return false;
        }
        if (this._locatorCacheTimeout !== rhs._locatorCacheTimeout) {
            return false;
        }
        if (this._connectionId !== rhs._connectionId) {
            return false;
        }
        if (!ArrayUtil.equals(this._endpoints, rhs._endpoints, (e1, e2) => e1.equals(e2))) {
            return false;
        }
        if (this._adapterId !== rhs._adapterId) {
            return false;
        }
        return true;
    }
 
    getRequestHandler() {
        const handler = new ConnectRequestHandler(this);
        this.getConnection(handler);
        return handler;
    }
 
    get batchRequestQueue() {
        return this._batchRequestQueue;
    }
 
    // Sets or resets _batchRequestQueue based on _mode.
    setBatchRequestQueue() {
        this._batchRequestQueue = this.isBatch ? new BatchRequestQueue(this._instance) : null;
    }
 
    getConnection(handler) {
        const p = new Promise(); // success callback receives (connection)
 
        if (this._routerInfo !== null) {
            //
            // If we route, we send everything to the router's client
            // proxy endpoints.
            //
            this._routerInfo
                .getClientEndpoints()
                .then(endpoints => {
                    if (endpoints.length > 0) {
                        this.applyOverrides(endpoints);
                        this.createConnection(endpoints).then(p.resolve, p.reject);
                    } else {
                        this.getConnectionNoRouterInfo(p);
                    }
                })
                .catch(p.reject);
        } else {
            this.getConnectionNoRouterInfo(p);
        }
 
        p.then(connection => handler.setConnection(connection)).catch(ex => handler.setException(ex));
        return p;
    }
 
    getConnectionNoRouterInfo(p) {
        if (this._endpoints.length > 0) {
            this.createConnection(this._endpoints).then(p.resolve).catch(p.reject);
            return;
        }
 
        if (this._locatorInfo !== null) {
            this._locatorInfo
                .getEndpoints(this, null, this._locatorCacheTimeout)
                .then(values => {
                    const [endpoints, cached] = values;
                    if (endpoints.length === 0) {
                        p.reject(new NoEndpointException(new ObjectPrx(this)));
                        return;
                    }
 
                    this.applyOverrides(endpoints);
                    this.createConnection(endpoints).then(p.resolve, ex => {
                        if (ex instanceof NoEndpointException) {
                            //
                            // No need to retry if there's no endpoints.
                            //
                            p.reject(ex);
                        } else {
                            console.assert(this._locatorInfo !== null);
                            this.getLocatorInfo().clearCache(this);
                            if (cached) {
                                const traceLevels = this.getInstance().traceLevels();
                                if (traceLevels.retry >= 2) {
                                    this.getInstance()
                                        .initializationData()
                                        .logger.trace(
                                            traceLevels.retryCat,
                                            "connection to cached endpoints failed\n" +
                                                "removing endpoints from cache and trying again\n" +
                                                ex.toString(),
                                        );
                                }
                                this.getConnectionNoRouterInfo(p); // Retry.
                                return;
                            }
                            p.reject(ex);
                        }
                    });
                })
                .catch(p.reject);
        } else {
            p.reject(new NoEndpointException(new ObjectPrx(this)));
        }
    }
 
    clone() {
        const r = new RoutableReference(
            this.getInstance(),
            this.getCommunicator(),
            this.getIdentity(),
            this.getFacet(),
            this.getMode(),
            this.getProtocol(),
            this.getEncoding(),
            this._endpoints,
            this._adapterId,
            this._locatorInfo,
            this._routerInfo,
            this._cacheConnection,
            this._endpointSelection,
            this._locatorCacheTimeout,
            this._invocationTimeout,
        );
        this.copyMembers(r);
        return r;
    }
 
    copyMembers(rhs) {
        //
        // Copy the members that are not passed to the constructor.
        //
        super.copyMembers(rhs);
        rhs._connectionId = this._connectionId;
    }
 
    applyOverrides(endpoints) {
        //
        // Apply the endpoint overrides to each endpoint.
        //
        for (let i = 0; i < endpoints.length; ++i) {
            endpoints[i] = endpoints[i].changeConnectionId(this._connectionId);
        }
    }
 
    filterEndpoints(allEndpoints) {
        //
        // Filter out opaque endpoints or endpoints which can't connect.
        //
        let endpoints = allEndpoints.filter(e => !(e instanceof OpaqueEndpointI) && e.connectable());
 
        //
        // Sort the endpoints according to the endpoint selection type.
        //
        switch (this.getEndpointSelection()) {
            case EndpointSelectionType.Random: {
                //
                // Shuffle the endpoints.
                //
                for (let i = endpoints.length; i > 0; --i) {
                    const e = endpoints[i - 1];
                    const rand = Math.floor(Math.random() * i);
                    endpoints[i - 1] = endpoints[rand];
                    endpoints[rand] = e;
                }
                break;
            }
            case EndpointSelectionType.Ordered: {
                // Nothing to do.
                break;
            }
            default: {
                console.assert(false);
                break;
            }
        }
 
        return endpoints;
    }
 
    createConnection(allEndpoints) {
        const endpoints = this.filterEndpoints(allEndpoints);
        if (endpoints.length === 0) {
            return Promise.reject(new NoEndpointException(new ObjectPrx(this)));
        }
 
        //
        // Finally, create the connection.
        //
        const promise = new Promise();
        const factory = this.getInstance().outgoingConnectionFactory();
        if (this.getCacheConnection() || endpoints.length == 1) {
            //
            // Get an existing connection or create one if there's no
            // existing connection to one of the given endpoints.
            //
            const cb = new CreateConnectionCallback(this, null, promise);
            factory
                .create(endpoints, false, this.getEndpointSelection())
                .then(connection => cb.setConnection(connection))
                .catch(ex => cb.setException(ex));
        } else {
            //
            // Go through the list of endpoints and try to create the
            // connection until it succeeds. This is different from just
            // calling create() with the given endpoints since this might
            // create a new connection even if there's an existing
            // connection for one of the endpoints.
            //
            const cb = new CreateConnectionCallback(this, endpoints, promise);
            factory
                .create([endpoints[0]], true, this.getEndpointSelection())
                .then(connection => cb.setConnection(connection))
                .catch(ex => cb.setException(ex));
        }
        return promise;
    }
}
 
class CreateConnectionCallback {
    constructor(r, endpoints, promise) {
        this.ref = r;
        this.endpoints = endpoints;
        this.promise = promise;
        this.i = 0;
        this.exception = null;
    }
 
    setConnection(connection) {
        //
        // If we have a router, set the object adapter for this router
        // (if any) to the new connection, so that callbacks from the
        // router can be received over this new connection.
        //
        if (this.ref.getRouterInfo() !== null && this.ref.getRouterInfo().getAdapter() !== null) {
            connection.setAdapter(this.ref.getRouterInfo().getAdapter());
        }
        this.promise.resolve(connection);
    }
 
    setException(ex) {
        if (this.exception === null) {
            this.exception = ex;
        }
 
        if (this.endpoints === null || ++this.i === this.endpoints.length) {
            this.promise.reject(this.exception);
            return;
        }
 
        this.ref
            .getInstance()
            .outgoingConnectionFactory()
            .create([this.endpoints[this.i]], this.i != this.endpoints.length - 1, this.ref.getEndpointSelection())
            .then(connection => this.setConnection(connection))
            .catch(ex => this.setException(ex));
    }
}