All files / src/Ice OpaqueEndpoint.js

64.47% Statements 196/304
92.1% Branches 35/38
40.9% Functions 9/22
64.47% Lines 196/304

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 30541x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 23x 23x 23x 23x 23x 41x 41x 41x 41x 41x 10x 10x 10x 10x 41x 41x 41x 41x 41x 1x 1x 41x 41x 41x 41x 41x 33x 33x 41x 41x 29x 29x 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 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 7x 7x 7x 7x 7x 7x 41x 41x 41x 41x 41x                                                       41x 41x                                                                                           41x 41x 50x 50x 21x 1x 1x 21x 2x 2x 18x 18x 18x 18x 18x 21x 1x 1x 17x 21x     17x 17x 17x 17x 50x 50x 17x 1x 1x 17x 1x 1x 17x 271x 1x 1x 1x 1x 271x 14x 14x 14x 50x 50x 11x     11x 11x 11x         11x 11x 50x 50x 1x 1x 50x 50x 41x 41x 22x 22x 22x 22x 2x 2x 22x 1x 1x 22x 41x 41x 1x 1x 1x 41x  
// Copyright (c) ZeroC, Inc.
 
import { Base64 } from "./Base64.js";
import { ParseException } from "./LocalExceptions.js";
import { HashUtil } from "./HashUtil.js";
import { StringUtil } from "./StringUtil.js";
import { EndpointI } from "./EndpointI.js";
import { Encoding_1_0, encodingVersionToString, stringToEncodingVersion } from "./Protocol.js";
import { OpaqueEndpointInfo } from "./Endpoint.js";
 
export class OpaqueEndpointI extends EndpointI {
    constructor(type) {
        super();
        this._rawEncoding = Encoding_1_0;
        this._type = type === undefined ? -1 : type;
        this._rawBytes = null;
    }
 
    //
    // Marshal the endpoint
    //
    streamWrite(s) {
        s.startEncapsulation(this._rawEncoding, null);
        s.writeBlob(this._rawBytes);
        s.endEncapsulation();
    }
 
    //
    // Return the endpoint information.
    //
    getInfo() {
        return new OpaqueEndpointInfo(this._type, this._rawEncoding, this._rawBytes);
    }
 
    //
    // Return the endpoint type
    //
    type() {
        return this._type;
    }
 
    protocol() {
        return "opaque";
    }
 
    //
    // Return the timeout for the endpoint in milliseconds. 0 means
    // non-blocking, -1 means no timeout.
    //
    timeout() {
        return -1;
    }
 
    //
    // Return a new endpoint with a different timeout value, provided
    // that timeouts are supported by the endpoint. Otherwise the same
    // endpoint is returned.
    //
    changeTimeout() {
        return this;
    }
 
    //
    // Return a new endpoint with a different connection id.
    //
    changeConnectionId() {
        return this;
    }
 
    //
    // Return true if the endpoints support bzip2 compress, or false
    // otherwise.
    //
    compress() {
        return false;
    }
 
    //
    // Return a new endpoint with a different compression value,
    // provided that compression is supported by the
    // endpoint. Otherwise the same endpoint is returned.
    //
    changeCompress() {
        return this;
    }
 
    //
    // Return true if the endpoint is secure.
    //
    secure() {
        return false;
    }
 
    //
    // Get the encoded endpoint.
    //
    rawBytes() {
        return this._rawBytes; // Returns a Uint8Array
    }
 
    //
    // Return a server side transceiver for this endpoint, or null if a
    // transceiver can only be created by an acceptor. In case a
    // transceiver is created, this operation also returns a new
    // "effective" endpoint, which might differ from this endpoint,
    // for example, if a dynamic port number is assigned.
    //
    transceiver(endpoint) {
        endpoint.value = null;
        return null;
    }
 
    //
    // Return an acceptor for this endpoint, or null if no acceptors
    // is available. In case an acceptor is created, this operation
    // also returns a new "effective" endpoint, which might differ
    // from this endpoint, for example, if a dynamic port number is
    // assigned.
    //
    acceptor(endpoint) {
        endpoint.value = this;
        return null;
    }
 
    connect() {
        return null;
    }
 
    hashCode() {
        let h = 5381;
        h = HashUtil.addNumber(h, this._type);
        h = HashUtil.addHashable(h, this._rawEncoding);
        h = HashUtil.addArray(h, this._rawBytes, HashUtil.addNumber);
        return h;
    }
 
    options() {
        let s = "";
        s += " -t " + this._type;
        s += " -e " + encodingVersionToString(this._rawEncoding);
        s += " -v " + Base64.encode(this._rawBytes);
        return s;
    }
 
    //
    // Compare endpoints for sorting purposes
    //
    equals(p) {
        if (!(p instanceof OpaqueEndpointI)) {
            return false;
        }

        if (this === p) {
            return true;
        }

        if (this._type !== p._type) {
            return false;
        }

        if (!this._rawEncoding.equals(p._rawEncoding)) {
            return false;
        }

        if (this._rawBytes.length !== p._rawBytes.length) {
            return false;
        }
        for (let i = 0; i < this._rawBytes.length; i++) {
            if (this._rawBytes[i] !== p._rawBytes[i]) {
                return false;
            }
        }

        return true;
    }
 
    compareTo(p) {
        if (this === p) {
            return 0;
        }

        if (p === null) {
            return 1;
        }

        if (!(p instanceof OpaqueEndpointI)) {
            return this.type() < p.type() ? -1 : 1;
        }

        if (this._type < p._type) {
            return -1;
        } else if (p._type < this._type) {
            return 1;
        }

        if (this._rawEncoding.major < p._rawEncoding.major) {
            return -1;
        } else if (p._rawEncoding.major < this._rawEncoding.major) {
            return 1;
        }

        if (this._rawEncoding.minor < p._rawEncoding.minor) {
            return -1;
        } else if (p._rawEncoding.minor < this._rawEncoding.minor) {
            return 1;
        }

        if (this._rawBytes.length < p._rawBytes.length) {
            return -1;
        } else if (p._rawBytes.length < this._rawBytes.length) {
            return 1;
        }
        for (let i = 0; i < this._rawBytes.length; i++) {
            if (this._rawBytes[i] < p._rawBytes[i]) {
                return -1;
            } else if (p._rawBytes[i] < this._rawBytes[i]) {
                return 1;
            }
        }

        return 0;
    }
 
    checkOption(option, argument, endpoint) {
        switch (option.charAt(1)) {
            case "t": {
                if (this._type > -1) {
                    throw new ParseException(`multiple -t options in endpoint ${endpoint}`);
                }
                if (argument === null) {
                    throw new ParseException(`no argument provided for -t option in endpoint ${endpoint}`);
                }
 
                let type;
 
                try {
                    type = StringUtil.toInt32(argument);
                } catch {
                    throw new ParseException(`invalid type value '${argument}' in endpoint ${endpoint}`);
                }
 
                if (type < 0 || type > 65535) {
                    throw new ParseException(`type value '${argument}' out of range in endpoint ${endpoint}`);
                }
 
                this._type = type;
                return true;
            }
 
            case "v": {
                if (this._rawBytes) {
                    throw new ParseException(`multiple -v options in endpoint ${endpoint}`);
                }
                if (argument === null || argument.length === 0) {
                    throw new ParseException(`no argument provided for -v option in endpoint ${endpoint}`);
                }
                for (let i = 0; i < argument.length; ++i) {
                    if (!Base64.isBase64(argument.charAt(i))) {
                        throw new ParseException(
                            `invalid base64 character '${argument.charAt(i)}' (ordinal ${argument.charCodeAt(i)}) in endpoint ${endpoint}`,
                        );
                    }
                }
                this._rawBytes = Base64.decode(argument);
                return true;
            }
 
            case "e": {
                if (argument === null) {
                    throw new ParseException(`no argument provided for -e option in endpoint ${endpoint}`);
                }
                try {
                    this._rawEncoding = stringToEncodingVersion(argument);
                } catch (ex) {
                    throw new ParseException(`invalid encoding version ${argument}' in endpoint ${endpoint}`, {
                        cause: ex,
                    });
                }
                return true;
            }
 
            default: {
                return false;
            }
        }
    }
 
    initWithOptions(args) {
        super.initWithOptions(args);
        console.assert(this._rawEncoding);
 
        if (this._type < 0) {
            throw new ParseException(`no -t option in endpoint '${this}'`);
        }
        if (this._rawBytes === null || this._rawBytes.length === 0) {
            throw new ParseException(`no -v option in endpoint '${this}'`);
        }
    }
 
    initWithStream(s) {
        this._rawEncoding = s.getEncoding();
        this._rawBytes = s.readBlob(s.getEncapsulationSize());
    }
}