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 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 8x 8x 8x 380x 397x 8x 8x 8x 8x 8x 8x 8x 36x 9x 9x 12x 12x 12x 12x 12x 12x 6x 6x 12x 12x 20x 20x 20x 20x 20x 14x 14x 14x 14x 14x 14x 14x 48x 6x 6x 42x 42x 42x 14x 46x 14x 15x 15x 14x 14x 14x 14x 26x 26x 26x 26x 26x 26x 8x 32x 8x 8x 8x 82x 33x 105x 8x 32x 32x 8x 8x 8x 8x 8x 8x 8x 8x 9x 9x 9x 8x 8x 7x 7x 7x 1x 1x 1x 5x 5x | "use strict"; /*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TnConnector = void 0; const core_1 = require("@coaty/core"); const grpc_js_1 = require("@grpc/grpc-js"); const fs_1 = require("fs"); const path = require("path"); const agent_info_1 = require("../agent.info"); const tn_communication_controller_1 = require("../controller/tn-communication-controller"); const tn_consensus_controller_1 = require("../controller/tn-consensus-controller"); const tn_lifecycle_controller_1 = require("../controller/tn-lifecycle-controller"); const routing_service_1 = require("../service/routing-service"); const tn_communication_service_1 = require("../service/tn-communication-service"); const tn_consensus_service_1 = require("../service/tn-consensus-service"); const tn_lifecycle_service_1 = require("../service/tn-lifecycle-service"); const grpc_package_1 = require("./grpc-package"); class TnConnector { constructor(options) { this._isCoatyAgentOnline = false; this.debug = options.debug.extend("TnConnector"); this._serviceImpls = new Map(); this._setOptionsWithDefaults(options); } get coatyAgent() { return this._coatyAgent; } get options() { return this._options; } static async start(options) { const tnc = new TnConnector(options); tnc.debug("Starting up with options %j", tnc.options); tnc._resolveCoatyAgent(false); tnc._createGrpcPackage(); tnc._createGrpcServer(); await tnc._startGrpcServer(); return tnc; } async stop(gracefully = true) { await Promise.allSettled(Array.from(this._serviceImpls.values()).map(s => s.onStopping())); await this._stopGrpcServer(gracefully); this._shutdownCoatyAgent(); } async restartCoatyCommunication(comOptions) { Iif (this.coatyAgent === undefined) { throw new Error("TnConnector has been stopped"); } await this.coatyAgent.communicationManager.stop(); this.debug("TNC agent communication stopped"); const { coatyAgentIdentityName, coatyAgentIdentityId } = this._options; this._setOptionsWithDefaults({ ...this._options, ...comOptions, debug: this.options.debug, grpcProtoPath: this.options.grpcProtoPath, grpcServerPort: this.options.grpcServerPort, consensusDatabaseFolder: this.options.consensusDatabaseFolder, }); if (this.options.coatyAgentIdentityId !== coatyAgentIdentityId || this.options.coatyAgentIdentityName !== coatyAgentIdentityName) { this._shutdownCoatyAgent(); this._resolveCoatyAgent(true); } await this.coatyAgent.communicationManager.start({ binding: this._getCoatyComBinding() }); this.debug("TNC agent communication restarted with options %o", this.options); } _setOptionsWithDefaults(options) { const optionalDefaults = { coatyAgentIdentityName: "FlowPro Agent", coatyAgentIdentityId: core_1.Runtime.newUuid(), coatyBrokerUrl: "", coatyNamespace: "tnc", coatyUsername: "", coatyPassword: "", coatyTlsCert: "", coatyTlsKey: "", coatyVerifyServerCert: "true", coatyFailFastIfOffline: "true", grpcProtoPath: path.join(__dirname, "../proto/"), grpcServerPort: "50060", consensusDatabaseFolder: "", }; this._options = Object.assign(optionalDefaults, options); try { Iif (this._options.coatyTlsCert !== "" && !this._options.coatyTlsCert.startsWith("-----BEGIN CERTIFICATE-----")) { this._options.coatyTlsCert = (0, fs_1.readFileSync)(this._options.coatyTlsCert, { encoding: "utf8" }); } Iif (this._options.coatyTlsKey !== "" && !this._options.coatyTlsKey.startsWith("-----BEGIN RSA PRIVATE KEY-----")) { this._options.coatyTlsKey = (0, fs_1.readFileSync)(this._options.coatyTlsKey, { encoding: "utf8" }); } } catch (error) { this.debug("Couldn't read Coaty TLS certificate or private key file: %s", error.message); } } _resolveCoatyAgent(suppressAutoStart) { const comps = { controllers: { ObjectLifecycleController: core_1.ObjectLifecycleController, TnCommunicationController: tn_communication_controller_1.TnCommunicationController, TnLifecycleController: tn_lifecycle_controller_1.TnLifecycleController, TnConsensusController: tn_consensus_controller_1.TnConsensusController, }, }; const controllerConfig = { TnCommunicationController: {}, TnLifecycleController: {}, TnConsensusController: {}, }; const config = { common: { agentInfo: agent_info_1.agentInfo, agentIdentity: { name: this.options.coatyAgentIdentityName, objectId: this.options.coatyAgentIdentityId, }, extra: this.options, }, communication: { binding: this._getCoatyComBinding(), shouldAutoStart: !suppressAutoStart && !!this.options.coatyBrokerUrl, }, controllers: controllerConfig, databases: { raftStore: { adapter: "SqLiteNodeAdapter", connectionString: path.join(this.options.consensusDatabaseFolder, `flowpro-tnc-raft-${this.options.coatyAgentIdentityId}.db`), }, }, }; config.common.agentIdentity["role"] = TnConnector.IDENTITY_ROLE; const container = core_1.Container.resolve(comps, config); this.debug("TNC agent resolved with identity %o", container.identity); container.communicationManager.observeCommunicationState() .subscribe(state => { if (container.communicationManager === undefined) { this._isCoatyAgentOnline = false; return; } this._isCoatyAgentOnline = state === core_1.CommunicationState.Online; const comOptions = container.communicationManager.options.binding.options; this.debug("TNC agent communication state: %s on namespace '%s' at %s", this._isCoatyAgentOnline ? "online" : "offline", comOptions.namespace, comOptions.brokerUrl || "not yet specified broker URL"); }); container.communicationManager.observeOperatingState() .subscribe(opState => { this.debug("TNC agent operating state: %s", opState === core_1.OperatingState.Started ? "started" : "stopped"); }); this._coatyAgent = container; } _shutdownCoatyAgent() { const agent = this.coatyAgent; if (agent !== undefined) { this._coatyAgent = undefined; this._isCoatyAgentOnline = false; this.debug("Shutting down Coaty agent '%s' with identity %s", agent.identity.name, agent.identity.objectId); agent.shutdown(); } } _getCoatyComBinding() { const bindingOptions = { namespace: this.options.coatyNamespace, brokerUrl: this.options.coatyBrokerUrl, }; Iif (this.options.coatyUsername) { bindingOptions.username = this.options.coatyUsername; } Iif (this.options.coatyPassword) { bindingOptions.password = this.options.coatyPassword; } Iif (this.options.coatyTlsCert && this.options.coatyTlsKey) { bindingOptions.tlsOptions = { cert: this.options.coatyTlsCert, key: this.options.coatyTlsKey, }; if (this.options.coatyVerifyServerCert === "false") { bindingOptions.tlsOptions.rejectUnauthorized = false; } } Iif ((bindingOptions.brokerUrl.startsWith("wss") || bindingOptions.brokerUrl.startsWith("wxs") || bindingOptions.brokerUrl.startsWith("alis")) && this.options.coatyVerifyServerCert === "false") { bindingOptions.wsOptions = { rejectUnauthorized: false, }; } return core_1.MqttBinding.withOptions(bindingOptions); } _createGrpcPackage() { const protos = ["tnc_coaty.proto", "tnc_icc.proto", "tnc_life.proto", "tnc_consensus.proto"]; this._grpcPackage = new grpc_package_1.GrpcPackage(protos.map(p => path.join(this.options.grpcProtoPath, p))); this.debug("Loaded gRPC protobuf definitions from %s", protos.join(", ")); } _createGrpcServer() { const server = new grpc_js_1.Server(); this._addServiceImpl(server, "flowpro.tnc.icc.RoutingService", new routing_service_1.RoutingService(this.debug)); this._addServiceImpl(server, "flowpro.tnc.coaty.CommunicationService", new tn_communication_service_1.TnCommunicationService(() => this.coatyAgent.getController("TnCommunicationController"), () => this.options, () => this._isCoatyAgentOnline, this.debug, opts => this.restartCoatyCommunication(opts))); this._addServiceImpl(server, "flowpro.tnc.life.LifecycleService", new tn_lifecycle_service_1.TnLifecycleService(() => this.coatyAgent.getController("TnLifecycleController"), () => this.options, () => this._isCoatyAgentOnline, this.debug)); this._addServiceImpl(server, "flowpro.tnc.consensus.ConsensusService", new tn_consensus_service_1.TnConsensusService(() => this.coatyAgent.getController("TnConsensusController"), () => this.options, () => this._isCoatyAgentOnline, this.debug)); this._grpcServer = server; } _addServiceImpl(server, serviceName, impl) { this._serviceImpls.set(serviceName, impl); server.addService(this._grpcPackage.getServiceDefinition(serviceName), impl.handlers); } async _startGrpcServer() { return new Promise((resolve, reject) => { const server = this._grpcServer; Iif (!server) { reject(new Error("gRPC server not yet created")); return; } server.bindAsync("0.0.0.0:" + this.options.grpcServerPort, grpc_js_1.ServerCredentials.createInsecure(), (error, port) => { Iif (error) { reject(new Error(`grpc Server port ${this.options.grpcServerPort} could not be bound. Is port already in use or blocked by firewall? Original error: ${error.message}`)); return; } server.start(); this.debug("Started gRPC server on port %d", port); resolve(); }); }); } async _stopGrpcServer(gracefully) { return new Promise(resolve => { const server = this._grpcServer; if (server !== undefined) { this._grpcServer = undefined; if (gracefully) { server.tryShutdown(() => { this.debug("Stopped gRPC server"); resolve(); }); } else { server.forceShutdown(); resolve(); } } else { resolve(); } }); } } exports.TnConnector = TnConnector; TnConnector.IDENTITY_ROLE = "TNC Agent"; |