All files / src/Ice TraceUtil.js

89.47% Statements 255/285
84.78% Branches 39/46
90.9% Functions 10/11
89.47% Lines 255/285

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 28641x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 2518x 2518x 2518x 2518x 2518x 2518x 2518x 2518x 2518x 2518x 3x 3x 2518x 2518x 2518x 2518x 41x 2466x 2466x 2466x 2466x 84x 84x 2466x 2466x 2466x 41x 13x 13x 13x 13x 13x 45x 45x 45x 13x 41x 2372x 2372x 2372x 2372x 2372x 2372x 2372x 2372x 2372x 2372x 2372x 2357x 2357x 2353x 2353x 2357x 2357x 2372x 2372x 2372x 2372x 7x 7x 2372x 2372x 8x 8x 8x 8x 2372x 2372x 41x 2511x 2511x 2511x 2511x 2511x 2511x 2511x 2235x 2235x 2235x 2511x 2511x       2511x 2511x 276x 276x 276x 2511x 2511x       2511x 2511x 2511x 2511x 2511x 164x 164x 164x 164x 108x 108x 164x 2511x 2511x 2511x 2507x 2507x 2511x 41x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x 5298x       5298x 5298x       5298x 5298x       5298x 5298x 5298x 5298x 5298x 5298x 41x 5298x 5298x 5298x 5298x 5298x 5298x 447x 447x 447x 5298x 5298x 2466x 2466x 2466x 5298x 5298x 13x 13x 13x 5298x 5298x 2372x 2372x 2372x 5298x 5298x     5298x 5298x 5298x 5298x 5298x 5298x 5298x 4x 4x 5298x 5298x 5298x 5298x 41x 10596x 10596x 10596x 4932x 10596x 26x 10596x 4744x 10596x 400x 10596x 494x 10596x   10596x 10596x 41x 41x 41x 3545x 2589x 2589x 2589x 2589x 2589x 2589x 2589x 2589x 2589x 2589x 2589x 3545x 41x 41x 3697x 2709x 2709x 2709x 2709x 2709x 2709x 2709x 2709x 2709x 2709x 3697x 41x 41x                         41x  
// Copyright (c) ZeroC, Inc.
 
import { Ice as Ice_Identity } from "./Identity.js";
const { Identity } = Ice_Identity;
import { StringUtil } from "./StringUtil.js";
import { Protocol } from "./Protocol.js";
import { Ice as Ice_OperationMode } from "./OperationMode.js";
import { identityToString } from "./IdentityToString.js";
 
import { Encoding_1_0, encodingVersionToString } from "./Protocol.js";
import { Ice as Ice_ReplyStatus } from "./ReplyStatus.js";
const { ReplyStatus } = Ice_ReplyStatus;
import { InputStream } from "./InputStream.js";
 
import { Ice as Ice_BuiltinSequences } from "./BuiltinSequences.js";
const { StringSeqHelper } = Ice_BuiltinSequences;
 
const OperationMode = Ice_OperationMode.OperationMode;
 
function printIdentityFacetOperation(s, inputStream) {
    const toStringMode = inputStream.instance.toStringMode();
 
    const identity = new Identity();
    identity._read(inputStream);
    s.push(`\nidentity = ${identityToString(identity, toStringMode)}`);
 
    const facet = StringSeqHelper.read(inputStream);
    s.push("\nfacet = ");
    if (facet.length > 0) {
        s.push(StringUtil.escapeString(facet[0], "", toStringMode));
    }
 
    const operation = inputStream.readString();
    s.push(`\noperation = ${operation}`);
}
 
function printRequest(s, inputStream) {
    const requestId = inputStream.readInt();
    s.push(`\nrequest id = ${requestId}`);
    if (requestId === 0) {
        s.push(" (oneway)");
    }
 
    printRequestHeader(s, inputStream);
}
 
function printBatchRequest(s, inputStream) {
    const batchRequestNum = inputStream.readInt();
    s.push(`\nnumber of requests = ${batchRequestNum}`);
 
    for (let i = 0; i < batchRequestNum; ++i) {
        s.push(`\nrequest #${i}:`);
        printRequestHeader(s, inputStream);
    }
}
 
function printReply(s, inputStream) {
    const requestId = inputStream.readInt();
    s.push(`\nrequest id = ${requestId}`);
 
    // This creates a new enumerator (e.g. "18": 18) if one doesn't exist yet.
    const replyStatus = ReplyStatus.valueOf(inputStream.readByte());
    s.push(`\nreply status = ${replyStatus}`);
 
    switch (replyStatus) {
        case ReplyStatus.Ok:
        case ReplyStatus.UserException: {
            const encodingVersion = inputStream.skipEncapsulation();
            if (!encodingVersion.equals(Encoding_1_0)) {
                s.push(`\nencoding = ${encodingVersionToString(encodingVersion)}`);
            }
            break;
        }
 
        case ReplyStatus.ObjectNotExist:
        case ReplyStatus.FacetNotExist:
        case ReplyStatus.OperationNotExist:
            printIdentityFacetOperation(s, inputStream);
            break;
 
        default: {
            const message = inputStream.readString();
            s.push(`\nmessage = ${message}`);
            break;
        }
    }
}
 
function printRequestHeader(s, inputStream) {
    printIdentityFacetOperation(s, inputStream);
 
    const mode = inputStream.readByte();
    s.push(`\nmode = ${mode} `);
    switch (OperationMode.valueOf(mode)) {
        case OperationMode.Normal: {
            s.push("(normal)");
            break;
        }
 
        case OperationMode.Nonmutating: {
            s.push("(nonmutating)");
            break;
        }
 
        case OperationMode.Idempotent: {
            s.push("(idempotent)");
            break;
        }
 
        default: {
            s.push("(unknown)");
            break;
        }
    }
 
    let sz = inputStream.readSize();
    s.push("\ncontext = ");
    while (sz-- > 0) {
        const key = inputStream.readString();
        const value = inputStream.readString();
        s.push(`${key}/${value}`);
        if (sz > 0) {
            s.push(", ");
        }
    }
 
    const ver = inputStream.skipEncapsulation();
    if (!ver.equals(Encoding_1_0)) {
        s.push(`\nencoding = ${encodingVersionToString(ver)}`);
    }
}
 
function printHeader(s, inputStream) {
    inputStream.readByte(); // Don't bother printing the magic number
    inputStream.readByte();
    inputStream.readByte();
    inputStream.readByte();
 
    //        const pMajor = inputStream.readByte();
    //        const pMinor = inputStream.readByte();
    //        s.push("\nprotocol version = " + pMajor + "." + pMinor);
    inputStream.readByte(); // major
    inputStream.readByte(); // minor
 
    //        const eMajor = inputStream.readByte();
    //        const eMinor = inputStream.readByte();
    //        s.push("\nencoding version = " + eMajor + "." + eMinor);
    inputStream.readByte(); // major
    inputStream.readByte(); // minor
 
    const type = inputStream.readByte();
 
    s.push(`\nmessage type = ${type} (${getMessageTypeAsString(type)})`);
    const compress = inputStream.readByte();
    s.push(`\ncompression status = ${compress} `);
    switch (compress) {
        case 0: {
            s.push("(not compressed; do not compress response, if any)");
            break;
        }
 
        case 1: {
            s.push("(not compressed; compress response, if any)");
            break;
        }
 
        case 2: {
            s.push("(compressed; compress response, if any)");
            break;
        }
 
        default: {
            s.push("(unknown)");
            break;
        }
    }
 
    const size = inputStream.readInt();
    s.push(`\nmessage size = ${size}`);
    return type;
}
 
function printMessage(s, inputStream, connection) {
    const type = printHeader(s, inputStream);
 
    switch (type) {
        case Protocol.closeConnectionMsg:
        case Protocol.validateConnectionMsg: {
            // We're done.
            break;
        }
 
        case Protocol.requestMsg: {
            printRequest(s, inputStream);
            break;
        }
 
        case Protocol.requestBatchMsg: {
            printBatchRequest(s, inputStream);
            break;
        }
 
        case Protocol.replyMsg: {
            printReply(s, inputStream);
            break;
        }
 
        default: {
            break;
        }
    }
 
    console.assert(connection !== null);
 
    s.push(`\ntransport = ${connection.type()}\n`);
    let connectionId = connection.endpoint().connectionId();
    if (connectionId.length > 0) {
        s.push(`connection ID = ${connectionId}\n`);
    }
    s.push(connection.toString());
 
    return type;
}
 
function getMessageTypeAsString(type) {
    switch (type) {
        case Protocol.requestMsg:
            return "request";
        case Protocol.requestBatchMsg:
            return "batch request";
        case Protocol.replyMsg:
            return "reply";
        case Protocol.closeConnectionMsg:
            return "close connection";
        case Protocol.validateConnectionMsg:
            return "validate connection";
        default:
            return "unknown";
    }
}
 
export class TraceUtil {
    static traceSend(outputStream, instance, connection, logger, traceLevels) {
        if (traceLevels.protocol >= 1) {
            const p = outputStream.pos;
            const inputStream = new InputStream(instance, outputStream.getEncoding(), outputStream.buffer);
            inputStream.pos = 0;
 
            const s = [];
            const type = printMessage(s, inputStream, connection);
 
            logger.trace(traceLevels.protocolCat, "sending " + getMessageTypeAsString(type) + " " + s.join(""));
 
            outputStream.pos = p;
        }
    }
 
    static traceRecv(inputStream, connection, logger, traceLevels) {
        if (traceLevels.protocol >= 1) {
            const p = inputStream.pos;
            inputStream.pos = 0;
 
            const s = [];
            const type = printMessage(s, inputStream, connection);
 
            logger.trace(traceLevels.protocolCat, "received " + getMessageTypeAsString(type) + " " + s.join(""));
 
            inputStream.pos = p;
        }
    }
 
    static trace(heading, inputStream, connection, logger, traceLevels) {
        if (traceLevels.protocol >= 1) {
            const p = inputStream.pos;
            inputStream.pos = 0;

            const s = [];
            s.push(heading);
            printMessage(s, inputStream, connection);

            logger.trace(traceLevels.protocolCat, s.join(""));
            inputStream.pos = p;
        }
    }
}