三個節(jié)點鸭栖,測試一下p2p廣播功能帝雇,點到點消息發(fā)送功能和節(jié)點發(fā)現(xiàn)功能
- 場景模擬node1 發(fā)送廣播,node2和node3 訂閱冤灾,
- node1 點對點 發(fā)送數(shù)據(jù)給node2和node3
- node2連接node1 玛臂, node3連接node2烤蜕,node1發(fā)現(xiàn)node3 節(jié)點
代碼分別如下
node1
/*
Copyright (C) BABEC. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"chainmaker.org/chainmaker/common/v2/crypto/asym"
cmTls "chainmaker.org/chainmaker/common/v2/crypto/tls"
cmx509 "chainmaker.org/chainmaker/common/v2/crypto/x509"
"chainmaker.org/chainmaker/common/v2/helper"
"chainmaker.org/chainmaker/net-liquid/core/host"
"chainmaker.org/chainmaker/net-liquid/core/peer"
"chainmaker.org/chainmaker/net-liquid/core/protocol"
"chainmaker.org/chainmaker/net-liquid/core/util"
"chainmaker.org/chainmaker/net-liquid/discovery/protocoldiscovery"
nethost "chainmaker.org/chainmaker/net-liquid/host"
"chainmaker.org/chainmaker/net-liquid/logger"
"chainmaker.org/chainmaker/net-liquid/pubsub"
"context"
"fmt"
ma "github.com/multiformats/go-multiaddr"
"strconv"
"time"
)
const msg = "Hello!My first LIQUID program demo."
const testProtocolID = "/test"
var (
addrsTcp = []ma.Multiaddr{
ma.StringCast("/ip4/127.0.0.1/tcp/7081"),
ma.StringCast("/ip4/127.0.0.1/tcp/7084"),
ma.StringCast("/ip4/127.0.0.1/tcp/7085"),
}
keyPEMs = [][]byte{
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIDc16StWEUNF0W7iXrT/k0Niy1ZWKbheI2wkjMqJ3WcaoAoGCCqGSM49\nAwEHoUQDQgAEmung19cBCguCaWeyf/nnARHRISTxtaxxf3Zqn+EQxxeYsXqa8oAg\ncalmJaGcmk9kTc0aJwpi7bKvJQdrRg1vFw==\n-----END EC PRIVATE KEY-----\n"),
[]byte("-----BEGIN EC PRIVATE KEY-----\n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kKKxutqGseJnjg4upWhvDo",
"Qmav9PUgzrdaLTgJ68XzX9gb4eKLzTonMUpAEsyJRFqV63",
"QmNQTmmZj8STcDhpKK3izd6q9DT42EuXu7oJyH7Tk1czga",
}
)
func CreateHostTCP(idx int, seeds map[peer.ID]ma.Multiaddr) (host.Host, error) {
certPool := cmx509.NewCertPool()
for i := range certPEMs {
certPool.AppendCertsFromPEM(certPEMs[i])
}
sk, err := asym.PrivateKeyFromPEM(keyPEMs[idx], nil)
if err != nil {
return nil, err
}
tlsCert, err := cmTls.X509KeyPair(certPEMs[idx], keyPEMs[idx])
if err != nil {
return nil, err
}
hostCfg := &nethost.HostConfig{
TlsCfg: &cmTls.Config{
Certificates: []cmTls.Certificate{tlsCert},
InsecureSkipVerify: true,
ClientAuth: cmTls.RequireAnyClientCert,
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*cmx509.Certificate) error {
tlsCertBytes := rawCerts[0]
cert, err := cmx509.ParseCertificate(tlsCertBytes)
if err != nil {
return err
}
_, err = cert.Verify(cmx509.VerifyOptions{Roots: certPool})
if err != nil {
return err
}
return nil
},
},
LoadPidFunc: func(certificates []*cmx509.Certificate) (peer.ID, error) {
pid, err := helper.GetLibp2pPeerIdFromCertDer(certificates[0].Raw)
if err != nil {
return "", err
}
return peer.ID(pid), err
},
SendStreamPoolInitSize: 10,
SendStreamPoolCap: 50,
PeerReceiveStreamMaxCount: 100,
ListenAddresses: []ma.Multiaddr{addrsTcp[idx]},
DirectPeers: seeds,
MsgCompress: false,
Insecurity: false,
PrivateKey: sk,
}
return hostCfg.NewHost(nethost.TcpNetwork, context.Background(), logger.NewLogPrinter("HOST"+strconv.Itoa(idx)))
}
func TestHostTCP() {
// create host1 pidList[1]: ma.Join(addr2TargetTcp, ma.StringCast("/p2p/"+pidList[1].ToString()))
host1, err := CreateHostTCP(0, map[peer.ID]ma.Multiaddr{})
// register notifee
notifyBundle := &host.NotifieeBundle{
PeerConnectedFunc: func(id peer.ID) {
fmt.Printf("節(jié)點已連接,節(jié)點ID:%s\n", id)
},
PeerDisconnectedFunc: func(id peer.ID) {
fmt.Printf("節(jié)點已斷開迹冤,節(jié)點ID:%s\n", id)
},
PeerProtocolSupportedFunc: func(protocolID protocol.ID, pid peer.ID) {
fmt.Printf("節(jié)點%s新支持協(xié)議%s\n", pid, protocolID)
},
PeerProtocolUnsupportedFunc: func(protocolID protocol.ID, pid peer.ID) {
fmt.Printf("節(jié)點%s取消支持協(xié)議%s\n", pid, protocolID)
},
}
host1.Notify(notifyBundle)
ps := pubsub.NewChainPubSub("c1635410037173", logger.NewLogPrinter("PubSub"))
err = ps.AttachHost(host1)
if err != nil {
// do something
panic(err)
}
// start hosts
err = host1.Start()
err = host1.RegisterMsgPayloadHandler(testProtocolID, func(senderPID peer.ID, msgPayload []byte) {
fmt.Println(string(msgPayload))
})
testTopic := "topic"
go func() {
i:=0
for {
i++
// host1 send msg to host2
time.Sleep(time.Second)
err = host1.SendMsg(testProtocolID, pidList[1], []byte(msg))
err = host1.SendMsg(testProtocolID, pidList[2], []byte(msg))
ps.Publish(testTopic,[]byte("廣播的消息"+strconv.Itoa(i)))
}
}()
// 節(jié)點發(fā)現(xiàn)服務
// 10. 開啟節(jié)點發(fā)現(xiàn)服務
log := logger.NewLogPrinter("TEST")
discoveryService, err := protocoldiscovery.NewProtocolBasedDiscovery(host1, protocoldiscovery.WithLogger(log))
if err != nil {
// do something
panic(err)
}
ctx := context.Background()
// 11. 宣布自己支持的服務
err = discoveryService.Announce(ctx, testProtocolID)
if err != nil {
// do something
panic(err)
}
// 12. 搜尋其他節(jié)點
findC, err := discoveryService.FindPeers(ctx, testProtocolID)
if err != nil {
// do something
panic(err)
}
go listenFindingC(ctx,0,host1,findC)
select {
}
}
func listenFindingC(ctx context.Context, idx int, h host.Host, c <-chan ma.Multiaddr) {
for {
select {
case <-ctx.Done():
return
case ai := <-c:
fmt.Printf("發(fā)現(xiàn)新節(jié)點讽营,地址:%s\n", ai.String())
addr, pid := util.GetNetAddrAndPidFromNormalMultiAddr(ai)
if pid == "" {
fmt.Errorf("[Discovery%d] peer id not contains in addr", idx)
continue
}
if h.ConnMgr().PeerCount() >= h.ConnMgr().MaxPeerCountAllowed() || h.ID() == pid || h.ConnMgr().IsConnected(pid) {
continue
}
fmt.Printf("[Discovery%d] find new peer.(pid: %s, addr: %s)", idx, pid, addr.String())
_, err := h.Dial(ai)
if err!=nil{
fmt.Errorf("連接出現(xiàn)錯誤")
continue
}
}
}
}
node 2
/*
Copyright (C) BABEC. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"chainmaker.org/chainmaker/common/v2/crypto/asym"
cmTls "chainmaker.org/chainmaker/common/v2/crypto/tls"
cmx509 "chainmaker.org/chainmaker/common/v2/crypto/x509"
"chainmaker.org/chainmaker/common/v2/helper"
"chainmaker.org/chainmaker/net-liquid/core/handler"
"chainmaker.org/chainmaker/net-liquid/core/host"
"chainmaker.org/chainmaker/net-liquid/core/peer"
"chainmaker.org/chainmaker/net-liquid/core/protocol"
"chainmaker.org/chainmaker/net-liquid/core/util"
"chainmaker.org/chainmaker/net-liquid/discovery/protocoldiscovery"
nethost "chainmaker.org/chainmaker/net-liquid/host"
"chainmaker.org/chainmaker/net-liquid/logger"
"chainmaker.org/chainmaker/net-liquid/pubsub"
"context"
"fmt"
ma "github.com/multiformats/go-multiaddr"
"strconv"
)
const msg = "Hello!My first LIQUID program demo."
const testProtocolID = "/test"
var (
addrsTcp = []ma.Multiaddr{
ma.StringCast("/ip4/127.0.0.1/tcp/7081"),
ma.StringCast("/ip4/127.0.0.1/tcp/7084"),
ma.StringCast("/ip4/127.0.0.1/tcp/7085"),
}
keyPEMs = [][]byte{
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIDc16StWEUNF0W7iXrT/k0Niy1ZWKbheI2wkjMqJ3WcaoAoGCCqGSM49\nAwEHoUQDQgAEmung19cBCguCaWeyf/nnARHRISTxtaxxf3Zqn+EQxxeYsXqa8oAg\ncalmJaGcmk9kTc0aJwpi7bKvJQdrRg1vFw==\n-----END EC PRIVATE KEY-----\n"),
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIGLbfX/EvXQtyO+GJvj5rFB0gyTqO3g4gO/gHX2A/BgNoAoGCCqGSM49\nAwEHoUQDQgAE0nVVBXfDQp+EaRbXXfXjn3QG+KYKqApi13+aeNMO7hvS4FlS3B5Z\nMRUCS2oBICwDgLf2q6ef1T5by1u9+IKK7w==\n-----END EC PRIVATE KEY-----\n"),
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIEUG3DFeXHVr5WjTRfpuSou30zEy2DMmzmyxuSd7F7K9oAoGCCqGSM49\nAwEHoUQDQgAEF+Tvi0fMzJOT9DLS+SFKPIo8kb3ouM54dJ9ibbohpR7FC8Iiid4g\nJ1QNguDjxEmwMV7Q1YiAuighKEmvtb4R+Q==\n-----END EC PRIVATE KEY-----"),
}
certPEMs = [][]byte{
[]byte("-----BEGIN CERTIFICATE-----\nMIICcjCCAhegAwIBAgIIFPWUuqixxuAwCgYIKoZIzj0EAwIwZDELMAkGA1UEBhMC\nQ04xEDAOBgNVBAgTB0JlaWppbmcxEDAOBgNVBAcTB0JlaWppbmcxETAPBgNVBAoT\nCGhjLW9yZzU2MQswCQYDVQQLEwJjYTERMA8GA1UEAxMIaGMtb3JnNTYwHhcNMjEx\nMDI4MDgyMzU3WhcNNDExMDIzMDgyMzU3WjBxMQswCQYDVQQGEwJDTjEQMA4GA1UE\nCBMHQmVpamluZzEQMA4GA1UEBxMHQmVpamluZzERMA8GA1UEChMIaGMtb3JnNTYx\nEjAQBgNVBAsTCWNvbnNlbnN1czEXMBUGA1UEAxMOaGMtb3JnNTYtbm9kZTEwWTAT\nBgcqhkjOPQIBBggqhkjOPQMBBwNCAASa6eDX1wEKC4JpZ7J/+ecBEdEhJPG1rHF/\ndmqf4RDHF5ixeprygCBxqWYloZyaT2RNzRonCmLtsq8lB2tGDW8Xo4GlMIGiMA4G\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XmGRM9I3Ghdvs+Ew\nN4dM580M\n-----END CERTIFICATE-----\n"),
}
pidList = []peer.ID{
"QmVGwL6VXMab7HubwkheZQpJkKKxutqGseJnjg4upWhvDo",
"Qmav9PUgzrdaLTgJ68XzX9gb4eKLzTonMUpAEsyJRFqV63",
"QmNQTmmZj8STcDhpKK3izd6q9DT42EuXu7oJyH7Tk1czga",
}
)
func CreateHostTCP(idx int, seeds map[peer.ID]ma.Multiaddr) (host.Host, error) {
certPool := cmx509.NewCertPool()
for i := range certPEMs {
certPool.AppendCertsFromPEM(certPEMs[i])
}
sk, err := asym.PrivateKeyFromPEM(keyPEMs[idx], nil)
if err != nil {
return nil, err
}
tlsCert, err := cmTls.X509KeyPair(certPEMs[idx], keyPEMs[idx])
if err != nil {
return nil, err
}
hostCfg := &nethost.HostConfig{
TlsCfg: &cmTls.Config{
Certificates: []cmTls.Certificate{tlsCert},
InsecureSkipVerify: true,
ClientAuth: cmTls.RequireAnyClientCert,
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*cmx509.Certificate) error {
tlsCertBytes := rawCerts[0]
cert, err := cmx509.ParseCertificate(tlsCertBytes)
if err != nil {
return err
}
_, err = cert.Verify(cmx509.VerifyOptions{Roots: certPool})
if err != nil {
return err
}
return nil
},
},
LoadPidFunc: func(certificates []*cmx509.Certificate) (peer.ID, error) {
pid, err := helper.GetLibp2pPeerIdFromCertDer(certificates[0].Raw)
if err != nil {
return "", err
}
return peer.ID(pid), err
},
SendStreamPoolInitSize: 10,
SendStreamPoolCap: 50,
PeerReceiveStreamMaxCount: 100,
ListenAddresses: []ma.Multiaddr{addrsTcp[idx]},
DirectPeers: seeds,
MsgCompress: false,
Insecurity: false,
PrivateKey: sk,
}
return hostCfg.NewHost(nethost.TcpNetwork, context.Background(), logger.NewLogPrinter("HOST"+strconv.Itoa(idx)))
}
func TestHostTCP() {
// create host2 pidList[0]: ma.Join(addrsTcp[0], ma.StringCast("/p2p/"+pidList[0].ToString()))
host1, _ := CreateHostTCP(1, map[peer.ID]ma.Multiaddr{pidList[0]: ma.Join(addrsTcp[0], ma.StringCast("/p2p/"+pidList[0].ToString()))})
notifyBundle := &host.NotifieeBundle{
PeerConnectedFunc: func(id peer.ID) {
fmt.Printf("節(jié)點已連接,節(jié)點ID:%s\n", id)
},
PeerDisconnectedFunc: func(id peer.ID) {
fmt.Printf("節(jié)點已斷開泡徙,節(jié)點ID:%s\n", id)
},
PeerProtocolSupportedFunc: func(protocolID protocol.ID, pid peer.ID) {
fmt.Printf("節(jié)點%s新支持協(xié)議%s\n", pid, protocolID)
},
PeerProtocolUnsupportedFunc: func(protocolID protocol.ID, pid peer.ID) {
fmt.Printf("節(jié)點%s取消支持協(xié)議%s\n", pid, protocolID)
},
}
host1.Notify(notifyBundle)
ps := pubsub.NewChainPubSub("c1635410037173", logger.NewLogPrinter("PubSub"))
err := ps.AttachHost(host1)
if err != nil {
// do something
panic(err)
}
testTopic := "topic"
var subHandler handler.SubMsgHandler = func(publisher peer.ID, topic string, msg []byte) {
fmt.Printf("收到節(jié)點%s向頻道%s發(fā)布的消息:%s\n", publisher, topic, string(msg))
}
ps.Subscribe(testTopic, subHandler)
// start hosts
_ = host1.Start()
// wait for connection established between host1 and host2
// register msg payload handler
_ = host1.RegisterMsgPayloadHandler(testProtocolID, func(senderPID peer.ID, msgPayload []byte) {
fmt.Println("收到Message"+string(msgPayload))
})
// 節(jié)點發(fā)現(xiàn)服務
// 10. 開啟節(jié)點發(fā)現(xiàn)服務
log := logger.NewLogPrinter("TEST")
discoveryService, err := protocoldiscovery.NewProtocolBasedDiscovery(host1, protocoldiscovery.WithLogger(log))
if err != nil {
// do something
panic(err)
}
ctx := context.Background()
// 11. 宣布自己支持的服務
err = discoveryService.Announce(ctx, testProtocolID)
if err != nil {
// do something
panic(err)
}
// 12. 搜尋其他節(jié)點
findC, err := discoveryService.FindPeers(ctx, testProtocolID)
if err != nil {
// do something
panic(err)
}
go listenFindingC(ctx,1,host1,findC)
select {
}
}
func listenFindingC(ctx context.Context, idx int, h host.Host, c <-chan ma.Multiaddr) {
for {
select {
case <-ctx.Done():
return
case ai := <-c:
fmt.Printf("發(fā)現(xiàn)新節(jié)點橱鹏,地址:%s\n", ai.String())
addr, pid := util.GetNetAddrAndPidFromNormalMultiAddr(ai)
if pid == "" {
fmt.Errorf("[Discovery%d] peer id not contains in addr", idx)
continue
}
if h.ConnMgr().PeerCount() >= h.ConnMgr().MaxPeerCountAllowed() || h.ID() == pid || h.ConnMgr().IsConnected(pid) {
continue
}
fmt.Printf("[Discovery%d] find new peer.(pid: %s, addr: %s)", idx, pid, addr.String())
_, _ = h.Dial(ai)
}
}
}
node3
/*
Copyright (C) BABEC. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"chainmaker.org/chainmaker/common/v2/crypto/asym"
cmTls "chainmaker.org/chainmaker/common/v2/crypto/tls"
cmx509 "chainmaker.org/chainmaker/common/v2/crypto/x509"
"chainmaker.org/chainmaker/common/v2/helper"
"chainmaker.org/chainmaker/net-liquid/core/handler"
"chainmaker.org/chainmaker/net-liquid/core/host"
"chainmaker.org/chainmaker/net-liquid/core/peer"
"chainmaker.org/chainmaker/net-liquid/core/protocol"
"chainmaker.org/chainmaker/net-liquid/core/util"
"chainmaker.org/chainmaker/net-liquid/discovery/protocoldiscovery"
nethost "chainmaker.org/chainmaker/net-liquid/host"
"chainmaker.org/chainmaker/net-liquid/logger"
"chainmaker.org/chainmaker/net-liquid/pubsub"
"context"
"fmt"
ma "github.com/multiformats/go-multiaddr"
"strconv"
)
const msg = "Hello!My first LIQUID program demo."
const testProtocolID = "/test"
var (
addrsTcp = []ma.Multiaddr{
ma.StringCast("/ip4/127.0.0.1/tcp/7081"),
ma.StringCast("/ip4/127.0.0.1/tcp/7084"),
ma.StringCast("/ip4/127.0.0.1/tcp/7085"),
}
keyPEMs = [][]byte{
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIDc16StWEUNF0W7iXrT/k0Niy1ZWKbheI2wkjMqJ3WcaoAoGCCqGSM49\nAwEHoUQDQgAEmung19cBCguCaWeyf/nnARHRISTxtaxxf3Zqn+EQxxeYsXqa8oAg\ncalmJaGcmk9kTc0aJwpi7bKvJQdrRg1vFw==\n-----END EC PRIVATE KEY-----\n"),
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIGLbfX/EvXQtyO+GJvj5rFB0gyTqO3g4gO/gHX2A/BgNoAoGCCqGSM49\nAwEHoUQDQgAE0nVVBXfDQp+EaRbXXfXjn3QG+KYKqApi13+aeNMO7hvS4FlS3B5Z\nMRUCS2oBICwDgLf2q6ef1T5by1u9+IKK7w==\n-----END EC PRIVATE KEY-----\n"),
[]byte("-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIEUG3DFeXHVr5WjTRfpuSou30zEy2DMmzmyxuSd7F7K9oAoGCCqGSM49\nAwEHoUQDQgAEF+Tvi0fMzJOT9DLS+SFKPIo8kb3ouM54dJ9ibbohpR7FC8Iiid4g\nJ1QNguDjxEmwMV7Q1YiAuighKEmvtb4R+Q==\n-----END EC PRIVATE KEY-----"),
}
certPEMs = [][]byte{
[]byte("-----BEGIN CERTIFICATE-----\nMIICcjCCAhegAwIBAgIIFPWUuqixxuAwCgYIKoZIzj0EAwIwZDELMAkGA1UEBhMC\nQ04xEDAOBgNVBAgTB0JlaWppbmcxEDAOBgNVBAcTB0JlaWppbmcxETAPBgNVBAoT\nCGhjLW9yZzU2MQswCQYDVQQLEwJjYTERMA8GA1UEAxMIaGMtb3JnNTYwHhcNMjEx\nMDI4MDgyMzU3WhcNNDExMDIzMDgyMzU3WjBxMQswCQYDVQQGEwJDTjEQMA4GA1UE\nCBMHQmVpamluZzEQMA4GA1UEBxMHQmVpamluZzERMA8GA1UEChMIaGMtb3JnNTYx\nEjAQBgNVBAsTCWNvbnNlbnN1czEXMBUGA1UEAxMOaGMtb3JnNTYtbm9kZTEwWTAT\nBgcqhkjOPQIBBggqhkjOPQMBBwNCAASa6eDX1wEKC4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CqGSM49BAMCA0kAMEYCIQCSA05P68pGYBCrFoTF\nsPrjx7XN7x1EdFzKb7jnqIc6SQIhAOJYgKkwQQwrXV+yqQJ9XmGRM9I3Ghdvs+Ew\nN4dM580M\n-----END CERTIFICATE-----\n"),
}
pidList = []peer.ID{
"QmVGwL6VXMab7HubwkheZQpJkKKxutqGseJnjg4upWhvDo",
"Qmav9PUgzrdaLTgJ68XzX9gb4eKLzTonMUpAEsyJRFqV63",
"QmNQTmmZj8STcDhpKK3izd6q9DT42EuXu7oJyH7Tk1czga",
}
)
func CreateHostTCP(idx int, seeds map[peer.ID]ma.Multiaddr) (host.Host, error) {
certPool := cmx509.NewCertPool()
for i := range certPEMs {
certPool.AppendCertsFromPEM(certPEMs[i])
}
sk, err := asym.PrivateKeyFromPEM(keyPEMs[idx], nil)
if err != nil {
return nil, err
}
tlsCert, err := cmTls.X509KeyPair(certPEMs[idx], keyPEMs[idx])
if err != nil {
return nil, err
}
hostCfg := &nethost.HostConfig{
TlsCfg: &cmTls.Config{
Certificates: []cmTls.Certificate{tlsCert},
InsecureSkipVerify: true,
ClientAuth: cmTls.RequireAnyClientCert,
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*cmx509.Certificate) error {
tlsCertBytes := rawCerts[0]
cert, err := cmx509.ParseCertificate(tlsCertBytes)
if err != nil {
return err
}
_, err = cert.Verify(cmx509.VerifyOptions{Roots: certPool})
if err != nil {
return err
}
return nil
},
},
LoadPidFunc: func(certificates []*cmx509.Certificate) (peer.ID, error) {
pid, err := helper.GetLibp2pPeerIdFromCertDer(certificates[0].Raw)
if err != nil {
return "", err
}
return peer.ID(pid), err
},
SendStreamPoolInitSize: 10,
SendStreamPoolCap: 50,
PeerReceiveStreamMaxCount: 100,
ListenAddresses: []ma.Multiaddr{addrsTcp[idx]},
DirectPeers: seeds,
MsgCompress: false,
Insecurity: false,
PrivateKey: sk,
}
return hostCfg.NewHost(nethost.TcpNetwork, context.Background(), logger.NewLogPrinter("HOST"+strconv.Itoa(idx)))
}
func TestHostTCP() {
// create host2 pidList[0]: ma.Join(addrsTcp[0], ma.StringCast("/p2p/"+pidList[0].ToString()))
host1, _ := CreateHostTCP(2, map[peer.ID]ma.Multiaddr{pidList[1]: ma.Join(addrsTcp[1], ma.StringCast("/p2p/"+pidList[1].ToString()))})
notifyBundle := &host.NotifieeBundle{
PeerConnectedFunc: func(id peer.ID) {
fmt.Printf("節(jié)點已連接,節(jié)點ID:%s\n", id)
},
PeerDisconnectedFunc: func(id peer.ID) {
fmt.Printf("節(jié)點已斷開堪藐,節(jié)點ID:%s\n", id)
},
PeerProtocolSupportedFunc: func(protocolID protocol.ID, pid peer.ID) {
fmt.Printf("節(jié)點%s新支持協(xié)議%s\n", pid, protocolID)
},
PeerProtocolUnsupportedFunc: func(protocolID protocol.ID, pid peer.ID) {
fmt.Printf("節(jié)點%s取消支持協(xié)議%s\n", pid, protocolID)
},
}
host1.Notify(notifyBundle)
ps := pubsub.NewChainPubSub("c1635410037173", logger.NewLogPrinter("PubSub"))
err := ps.AttachHost(host1)
if err != nil {
// do something
panic(err)
}
testTopic := "topic"
var subHandler handler.SubMsgHandler = func(publisher peer.ID, topic string, msg []byte) {
fmt.Printf("收到節(jié)點%s向頻道%s發(fā)布的消息:%s\n", publisher, topic, string(msg))
}
ps.Subscribe(testTopic, subHandler)
// start hosts
_ = host1.Start()
// wait for connection established between host1 and host2
// register msg payload handler
_ = host1.RegisterMsgPayloadHandler(testProtocolID, func(senderPID peer.ID, msgPayload []byte) {
fmt.Println("收到Message"+string(msgPayload))
})
// 節(jié)點發(fā)現(xiàn)服務
// 10. 開啟節(jié)點發(fā)現(xiàn)服務
log := logger.NewLogPrinter("TEST")
discoveryService, err := protocoldiscovery.NewProtocolBasedDiscovery(host1, protocoldiscovery.WithLogger(log))
if err != nil {
// do something
panic(err)
}
ctx := context.Background()
// 11. 宣布自己支持的服務
err = discoveryService.Announce(ctx, testProtocolID)
if err != nil {
// do something
panic(err)
}
// 12. 搜尋其他節(jié)點
findC, err := discoveryService.FindPeers(ctx, testProtocolID)
if err != nil {
// do something
panic(err)
}
go listenFindingC(ctx,2,host1,findC)
select {
}
}
func listenFindingC(ctx context.Context, idx int, h host.Host, c <-chan ma.Multiaddr) {
for {
select {
case <-ctx.Done():
return
case ai := <-c:
fmt.Printf("發(fā)現(xiàn)新節(jié)點莉兰,地址:%s\n", ai.String())
addr, pid := util.GetNetAddrAndPidFromNormalMultiAddr(ai)
if pid == "" {
fmt.Errorf("[Discovery%d] peer id not contains in addr", idx)
continue
}
if h.ConnMgr().PeerCount() >= h.ConnMgr().MaxPeerCountAllowed() || h.ID() == pid || h.ConnMgr().IsConnected(pid) {
continue
}
fmt.Printf("[Discovery%d] find new peer.(pid: %s, addr: %s)", idx, pid, addr.String())
_, _ = h.Dial(ai)
}
}
}