Reputation: 7550
I am able to ping network using the following command:
composer network ping -c admin@university
But how do I do the same programmatically with Nodejs?
Upvotes: 0
Views: 169
Reputation: 7550
From: 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;
Upvotes: 0
Reputation: 1458
you can use child_process to execute the ping command of the composer in Nodejs
Sample Nodejs code as below
// 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);
Upvotes: 1