// Returns a new EventHub instance for the specified name private EventHub GetEventHub(HFClient client, string name) { EventHub ehub = null; Node e = eventHubs.GetOrNull(name); if (e != null) { ehub = client.NewEventHub(e.Name, e.Url, e.Properties); } return(ehub); }
// Reconstructs an existing channel private async Task <Channel> ReconstructChannelAsync(HFClient client, string channelName, JToken jsonChannel, CancellationToken token = default(CancellationToken)) { Channel channel; channel = client.NewChannel(channelName); // orderers is an array of orderer name strings JArray ordererNames = jsonChannel["orderers"] as JArray; //out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString())); if (ordererNames != null) { foreach (JToken jsonVal in ordererNames) { string ordererName = jsonVal.Value <string>(); Orderer orderer = GetOrderer(client, ordererName); if (orderer == null) { throw new NetworkConfigurationException($"Error constructing channel {channelName}. Orderer {ordererName} not defined in configuration"); } channel.AddOrderer(orderer); } } // peers is an object containing a nested object for each peer JToken jsonPeers = jsonChannel["peers"]; bool foundPeer = false; //out("Peers: " + (peers == null ? "null" : peers.toString())); if (jsonPeers != null) { foreach (JProperty prop in jsonPeers.Children <JProperty>()) { string peerName = prop.Name; if (logger.IsTraceEnabled()) { logger.Trace($"NetworkConfig.reconstructChannel: Processing peer {peerName}"); } JToken jsonPeer = prop.Value; if (jsonPeer == null) { throw new NetworkConfigurationException($"Error constructing channel {channelName}. Invalid peer entry: {peerName}"); } Peer peer = GetPeer(client, peerName); if (peer == null) { throw new NetworkConfigurationException($"Error constructing channel {channelName}. Peer {peerName} not defined in configuration"); } // Set the various roles PeerOptions peerOptions = PeerOptions.CreatePeerOptions(); foreach (PeerRole peerRole in Enum.GetValues(typeof(PeerRole))) { SetPeerRole(peerOptions, jsonPeer, peerRole); } foundPeer = true; // Add the event hub associated with this peer EventHub eventHub = GetEventHub(client, peerName); if (eventHub != null) { await channel.AddEventHubAsync(eventHub, token).ConfigureAwait(false); if (!peerOptions.HasPeerRoles()) { // means no roles were found but there is an event hub so define all roles but eventing. peerOptions.SetPeerRoles(new List <PeerRole> { PeerRole.ENDORSING_PEER, PeerRole.CHAINCODE_QUERY, PeerRole.LEDGER_QUERY }); } } await channel.AddPeerAsync(peer, peerOptions, token).ConfigureAwait(false); } } if (!foundPeer) { // peers is a required field throw new NetworkConfigurationException($"Error constructing channel {channelName}. At least one peer must be specified"); } return(channel); }