All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperledger.fabric.sdk.PeerClient Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
/*
 *  Copyright 2016 DTCC, Fujitsu Australia Software Technology - All Rights Reserved.
 *
 *  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.
 */

package org.hyperledger.fabric.sdk;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.sdk.exception.PeerException;
import org.hyperledger.protos.Fabric;
import org.hyperledger.protos.Fabric.Response;
import org.hyperledger.protos.PeerGrpc;
import org.hyperledger.protos.PeerGrpc.PeerBlockingStub;

import java.util.concurrent.TimeUnit;

/**
 * Sample client code that makes gRPC calls to the server.
 */
public class PeerClient {
	private static final Log logger = LogFactory.getLog(PeerClient.class);

	private final ManagedChannel channel;
	private final PeerBlockingStub blockingStub;

	/**
	 * Construct client for accessing Peer server using the existing channel.
	 */
	public PeerClient(ManagedChannelBuilder channelBuilder) {
		channel = channelBuilder.build();
		blockingStub = PeerGrpc.newBlockingStub(channel);
	}

	public void shutdown() throws InterruptedException {
		channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
	}

	public Response processTransaction(Fabric.Transaction transaction) throws PeerException {
		try {
			return blockingStub.processTransaction(transaction);
		} catch (StatusRuntimeException e) {
			logger.warn(String.format("RPC failed: %s", e.getStatus()));
			throw new PeerException("Sending transaction to peer failed", e);
		}
	}

	@Override
	public void finalize() {
		try {
			shutdown();
		} catch (InterruptedException e) {
			logger.debug("Failed to shutdown the PeerClient");
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy