<commit_before>// Copyright 2018 <NAME>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

import GP from 'GP';
import Config from '../config.js';

import { BItem, BItemType } from '../Items/BItem.js';

import { SpaceServerConnection } from './SpaceServer.js';
import { SpaceServerClientConnection } from './SpaceServerClient.js';
import { BasilServerConnection } from './BasilServer.js';
import { BasilClientConnection } from './BasilClient.js';
import { AliveCheckConnection } from './AliveCheckMsgs.js';
import { PestoClientConnection } from './PestoClient.js';

import { BuildBasilMessageOps } from './BasilMessageOps.js';

import { BTransportWW } from './BTransportWW.js';
import { BTransportWS } from './BTransportWS.js';
import { BTransportTest } from './BTransportTest.js';

import { CombineParameters } from '../Utilities.js';
import { BException } from '../BException.js';

export class Comm extends BItem {
    constructor() {
        GP.DebugLog('Comm: constructor');
        super('org.basil.b.comm', undefined, BItemType.COMM);
        this.layer = Config.layers ? Config.layers.comm : 'org.basil.b.layer.comm';

        this.transports = new Map();

        // Build the table of ops to names and via versa
        BuildBasilMessageOps();
    }

    Start() {
        this.SetReady();
    };

    // Make a connection to a service.
    // 'parms' is passed to the created transport/service
    ConnectTransport(parms) {
        let params = CombineParameters(undefined, parms, {
            'transportId': undefined,     // identifier for the created transport
            'transport': 'WS',            // the type of transport to connect (WW, WS, test)
            'transportURL': undefined,    // URL to connect transport to
            'waitTilTransportReadyMS': 5000  // MS before timeout waiting for transport ready
        });
        return new Promise(function(resolve, reject) {
            let xport = undefined;
            // If there is already a transport for this destination URL, return that
            if (this.transports.has(params.transportURL) {
                xport = this.transports.get(params.transportURL);
                GP.DebugLog('Comm.ConnectTransport: reusing transport ' + xport.id)
            }
            else {
                // Create a new transport to the URL
                try {
                    switch (params.transport) {
                        case 'WW':
                            xport = new BTransportWW(params);
                            break;
                        case 'WS':
                            xport = new BTransportWS(params);
                            break;
                        case 'Test':<commit_msg>Fix typo in connection processing (fixes a crash).<commit_after>// Copyright 2018 <NAME>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

import GP from 'GP';
import Config from '../config.js';

import { BItem, BItemType } from '../Items/BItem.js';

import { SpaceServerConnection } from './SpaceServer.js';
import { SpaceServerClientConnection } from './SpaceServerClient.js';
import { BasilServerConnection } from './BasilServer.js';
import { BasilClientConnection } from './BasilClient.js';
import { AliveCheckConnection } from './AliveCheckMsgs.js';
import { PestoClientConnection } from './PestoClient.js';

import { BuildBasilMessageOps } from './BasilMessageOps.js';

import { BTransportWW } from './BTransportWW.js';
import { BTransportWS } from './BTransportWS.js';
import { BTransportTest } from './BTransportTest.js';

import { CombineParameters } from '../Utilities.js';
import { BException } from '../BException.js';

export class Comm extends BItem {
    constructor() {
        GP.DebugLog('Comm: constructor');
        super('org.basil.b.comm', undefined, BItemType.COMM);
        this.layer = Config.layers ? Config.layers.comm : 'org.basil.b.layer.comm';

        this.transports = new Map();

        // Build the table of ops to names and via versa
        BuildBasilMessageOps();
    }

    Start() {
        this.SetReady();
    };

    // Make a connection to a service.
    // 'parms' is passed to the created transport/service
    ConnectTransport(parms) {
        let params = CombineParameters(undefined, parms, {
            'transportId': undefined,     // identifier for the created transport
            'transport': 'WS',            // the type of transport to connect (WW, WS, test)
            'transportURL': undefined,    // URL to connect transport to
            'waitTilTransportReadyMS': 5000  // MS before timeout waiting for transport ready
        });
        return new Promise(function(resolve, reject) {
            let xport = undefined;
            // If there is already a transport for this destination URL, return that
            if (this.transports.has(params.transportURL)) {
                xport = this.transports.get(params.transportURL);
                GP.DebugLog('Comm.ConnectTransport: reusing transport ' + xport.id)
            }
            else {
                // Create a new transport to the URL
                try {
                    switch (params.transport) {
                        case 'WW':
                            xport = new BTransportWW(params);
                            break;
                        case 'WS':
                            xport = new BTransportWS(params);
                            break;
                        case 'Test':
{'fraction_non_alphanumeric': 0.1043613707165109, 'fraction_numerical': 0.003738317757009346, 'mean_word_length': 2.2250125565042693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5361001', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1601, 'n_words': 572}