All files / src/Ice TcpEndpointI.js

84.86% Statements 213/251
72.41% Branches 42/58
87.5% Functions 14/16
84.86% Lines 213/251

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 25241x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 3675x 3675x 3675x 3675x 3675x 41x 41x 41x 41x 41x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 41x 41x 41x 41x 41x 41x 1x 1x 41x 41x 41x 41x 41x 41x 41x 2390x   2390x 2390x 2390x 2390x 2390x 2390x 2390x 2390x 2390x 2390x 2390x 2390x 41x 41x 41x 41x 41x 1261x 1255x 1261x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1261x 41x 41x 41x 41x 41x 41x     41x 41x 41x 41x 41x 41x 41x 1007x 337x 1007x 670x 670x 670x 670x 670x 670x 670x 670x 670x 670x 1007x 41x 41x 1734x 1734x 1734x 41x 41x 335x 335x 335x 41x 41x 41x 41x 41x 1155x 1155x 1155x 1155x 1155x 1155x 1155x 1155x 1155x 1155x 1155x 1155x 14x 4x 14x 10x 10x 14x 1155x 1155x     1155x 1155x 41x 41x 2169x 174x 174x 1995x 2169x     1995x 2169x     1995x 2169x   2169x     1995x 2169x   2169x     1995x 1995x 2169x 41x 41x 155x 155x 155x 155x 41x 41x 9328x 9328x 9328x 9328x 9328x 41x 41x 446x 446x 446x 446x 41x 41x 178x 171x 171x 7x 11x 6x     6x 6x   6x 6x 6x 6x 6x     6x     6x 178x 1x     1x 1x 1x     7x 178x 41x 41x                     41x  
// Copyright (c) ZeroC, Inc.
 
import { HashUtil } from "./HashUtil.js";
import { StringUtil } from "./StringUtil.js";
import { TCPEndpointInfo } from "./Endpoint.js";
import { ParseException } from "./LocalExceptions.js";
import { IPEndpointI } from "./IPEndpointI.js";
import { EndpointInfo as SSLEndpointInfo } from "./SSL/EndpointInfo.js";
import { TcpTransceiver } from "./TcpTransceiver.js";
 
const defaultTimeout = 60000; // 60,000 milliseconds (1 minute)
 
export class TcpEndpointI extends IPEndpointI {
    constructor(instance, host, port, sourceAddress, timeout, connectionId, compress) {
        super(instance, host, port, sourceAddress, connectionId);
        // The timeout is not used in Ice 3.8 or greater.
        this._timeout = timeout === undefined ? (instance ? defaultTimeout : undefined) : timeout;
        this._compress = compress === undefined ? false : compress;
    }
 
    //
    // Return the endpoint information.
    //
    getInfo() {
        let info = new TCPEndpointInfo(
            this._timeout,
            this._compress,
            this._host,
            this._port,
            this._sourceAddr,
            this.type(),
            this.secure(),
        );
 
        if (this.secure()) {
            info = new SSLEndpointInfo(info);
        }
 
        return info;
    }
 
    //
    // Return the timeout for the endpoint in milliseconds. 0 means
    // non-blocking, -1 means no timeout.
    //
    timeout() {
        return this._timeout;
    }
 
    //
    // Return a new endpoint with a different timeout value, provided
    // that timeouts are supported by the endpoint. Otherwise the same
    // endpoint is returned.
    //
    changeTimeout(timeout) {
        if (timeout === this._timeout) {
            return this;
        } else {
            return new TcpEndpointI(
                this._instance,
                this._host,
                this._port,
                this._sourceAddr,
                timeout,
                this._connectionId,
                this._compress,
            );
        }
    }
 
    //
    // Return a new endpoint with a different connection id.
    //
    changeConnectionId(connectionId) {
        if (connectionId === this._connectionId) {
            return this;
        } else {
            return new TcpEndpointI(
                this._instance,
                this._host,
                this._port,
                this._sourceAddr,
                this._timeout,
                connectionId,
                this._compress,
            );
        }
    }
 
    //
    // Return true if the endpoints support bzip2 compress, or false
    // otherwise.
    //
    compress() {
        return this._compress;
    }
 
    //
    // Return a new endpoint with a different compression value,
    // provided that compression is supported by the
    // endpoint. Otherwise the same endpoint is returned.
    //
    changeCompress(compress) {
        if (compress === this._compress) {
            return this;
        } else {
            return new TcpEndpointI(
                this._instance,
                this._host,
                this._port,
                this._sourceAddr,
                this._timeout,
                this._connectionId,
                compress,
            );
        }
    }
 
    connectable() {
        // TCP endpoints are not connectable when running in a browser, SSL isn't currently supported.
        return TcpTransceiver !== null && !this.secure();
    }
 
    connect() {
        console.assert(!this.secure());
        return new TcpTransceiver(this._instance, this.getAddress(), this._sourceAddr);
    }
 
    //
    // Convert the endpoint to its string form
    //
    options() {
        //
        // 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.
        //
        let s = super.options();
 
        // We don't print the timeout when it's the default; this timeout is purely for backwards compatibility and
        // has no effect with Ice 3.8 or greater.
        if (this._timeout != defaultTimeout) {
            if (this._timeout == -1) {
                s += " -t infinite";
            } else {
                s += " -t " + this._timeout;
            }
        }
 
        if (this._compress) {
            s += " -z";
        }
        return s;
    }
 
    compareTo(p) {
        if (this === p) {
            return 0;
        }
 
        if (p === null) {
            return 1;
        }
 
        if (!(p instanceof TcpEndpointI)) {
            return this.type() < p.type() ? -1 : 1;
        }
 
        if (this._timeout < p._timeout) {
            return -1;
        } else if (p._timeout < this._timeout) {
            return 1;
        }
 
        if (!this._compress && p._compress) {
            return -1;
        } else if (!p._compress && this._compress) {
            return 1;
        }
 
        return super.compareTo(p);
    }
 
    streamWriteImpl(s) {
        super.streamWriteImpl(s);
        s.writeInt(this._timeout);
        s.writeBool(this._compress);
    }
 
    hashCode() {
        let h = super.hashCode();
        h = HashUtil.addNumber(h, this._timeout);
        h = HashUtil.addBoolean(h, this._compress);
        return h;
    }
 
    initWithStream(s) {
        super.initWithStream(s);
        this._timeout = s.readInt();
        this._compress = s.readBool();
    }
 
    checkOption(option, argument, endpoint) {
        if (super.checkOption(option, argument, endpoint)) {
            return true;
        }
 
        if (option === "-t") {
            if (argument === null) {
                throw new ParseException(`no argument provided for -t option in endpoint ${endpoint}`);
            }
 
            if (argument == "infinite") {
                this._timeout = -1;
            } else {
                let invalid = false;
                try {
                    this._timeout = StringUtil.toInt32(argument);
                } catch {
                    invalid = true;
                }
                if (invalid || this._timeout < 1) {
                    throw new ParseException(`invalid timeout value '${argument}' in endpoint ${endpoint}`);
                }
            }
        } else if (option === "-z") {
            if (argument !== null) {
                throw new ParseException(`unexpected argument ${argument}' provided for -z option in ${endpoint}`);
            }
 
            this._compress = true;
        } else {
            return false;
        }
        return true;
    }
 
    createEndpoint(host, port, connectionId) {
        return new TcpEndpointI(
            this._instance,
            host,
            port,
            this._sourceAddr,
            this._timeout,
            connectionId,
            this._compress,
        );
    }
}