github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/gossip/discovery/discovery.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package discovery 18 19 import ( 20 "github.com/hyperledger/fabric/gossip/common" 21 proto "github.com/hyperledger/fabric/protos/gossip" 22 ) 23 24 // CryptoService is an interface that the discovery expects to be implemented and passed on creation 25 type CryptoService interface { 26 // ValidateAliveMsg validates that an Alive message is authentic 27 ValidateAliveMsg(message *proto.SignedGossipMessage) bool 28 29 // SignMessage signs a message 30 SignMessage(m *proto.GossipMessage, internalEndpoint string) *proto.Envelope 31 } 32 33 // CommService is an interface that the discovery expects to be implemented and passed on creation 34 type CommService interface { 35 // Gossip gossips a message 36 Gossip(msg *proto.SignedGossipMessage) 37 38 // SendToPeer sends to a given peer a message. 39 // The nonce can be anything since the communication module handles the nonce itself 40 SendToPeer(peer *NetworkMember, msg *proto.SignedGossipMessage) 41 42 // Ping probes a remote peer and returns if it's responsive or not 43 Ping(peer *NetworkMember) bool 44 45 // Accept returns a read-only channel for membership messages sent from remote peers 46 Accept() <-chan *proto.SignedGossipMessage 47 48 // PresumedDead returns a read-only channel for peers that are presumed to be dead 49 PresumedDead() <-chan common.PKIidType 50 51 // CloseConn orders to close the connection with a certain peer 52 CloseConn(peer *NetworkMember) 53 } 54 55 // NetworkMember is a peer's representation 56 type NetworkMember struct { 57 Endpoint string 58 Metadata []byte 59 PKIid common.PKIidType 60 InternalEndpoint string 61 } 62 63 // PreferredEndpoint computes the endpoint to connect to, 64 // while preferring internal endpoint over the standard 65 // endpoint 66 func (nm NetworkMember) PreferredEndpoint() string { 67 if nm.InternalEndpoint != "" { 68 return nm.InternalEndpoint 69 } 70 return nm.Endpoint 71 } 72 73 // Discovery is the interface that represents a discovery module 74 type Discovery interface { 75 76 // Exists returns whether a peer with given 77 // PKI-ID is known 78 Exists(PKIID common.PKIidType) bool 79 80 // Self returns this instance's membership information 81 Self() NetworkMember 82 83 // UpdateMetadata updates this instance's metadata 84 UpdateMetadata([]byte) 85 86 // UpdateEndpoint updates this instance's endpoint 87 UpdateEndpoint(string) 88 89 // Stops this instance 90 Stop() 91 92 // GetMembership returns the alive members in the view 93 GetMembership() []NetworkMember 94 95 // InitiateSync makes the instance ask a given number of peers 96 // for their membership information 97 InitiateSync(peerNum int) 98 99 // Connect makes this instance to connect to a remote instance 100 Connect(NetworkMember) 101 }