Как пинговать сеть программно - PullRequest
0 голосов
/ 11 мая 2018

Я могу проверить связь с сетью, используя следующую команду:

composer network ping -c admin@university

Но как мне сделать то же самое программно с Nodejs?

Ответы [ 2 ]

0 голосов
/ 12 мая 2018

От: https://github.com/mataide/serverless-hyperledger

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-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.
 */

// This is a model to Network Actions

'use strict';

const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
const AdminConnection = require('composer-admin').AdminConnection;
const BusinessNetworkCardStore = require('composer-common').BusinessNetworkCardStore;
const IdCard = require('composer-common').IdCard;


/** Class for the Network*/
class Network {

  /**
   * Need to have the mapping from bizNetwork name to the URLs to connect to.
   * bizNetwork nawme will be able to be used by Composer to get the suitable model files.
   *
   */
  constructor(credentials) {
    this.bizNetworkConnection = new BusinessNetworkConnection();
    this.adminConnection = new AdminConnection();
    this.credentials = credentials
  }


  async ping() {

    const idCardData = new IdCard(this.credentials['metadata'], this.credentials['connection']);

    const idCardName = BusinessNetworkCardStore.getDefaultCardName(idCardData);
    try{
      const imported = await this.adminConnection.importCard(idCardName, idCardData);
      if (imported) {
        this.businessNetworkDefinition = await this.bizNetworkConnection.connect(idCardName);
        if (!this.businessNetworkDefinition) {
          console.log("Error in network connection");
          throw "Error in network connection";
        }
        let result = await this.businessNetworkDefinition.ping();
        return result
      } else {
        console.log('null');
        throw "Error in importing card";
      }
    }catch(error){
      console.log(error);
      throw error;
    }
  }


}
module.exports = Network;
0 голосов
/ 11 мая 2018

Вы можете использовать child_process для выполнения команды ping компоновщика в Nodejs

Пример кода Nodejs, как показано ниже

// http://nodejs.org/api.html#_child_processes
var sys = require('sys')
var exec = require('child_process').exec;
var child;
// executes `pwd`
child = exec("pwd", function (error, stdout, stderr) {
  sys.print('stdout: ' + stdout);
  sys.print('stderr: ' + stderr);
  if (error !== null) {
    console.log('exec error: ' + error);
  }
});
// or more concisely
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("composer network ping -c admin@university", puts);
...