github.com/turingchain2020/turingchain@v1.1.21/p2p/base.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package p2p 实现多种类型p2p兼容管理
     6  package p2p
     7  
     8  import (
     9  	"github.com/turingchain2020/turingchain/client"
    10  	"github.com/turingchain2020/turingchain/common/pubsub"
    11  	"github.com/turingchain2020/turingchain/queue"
    12  	"github.com/turingchain2020/turingchain/types"
    13  	lru "github.com/hashicorp/golang-lru"
    14  )
    15  
    16  // IP2P p2p interface
    17  type IP2P interface {
    18  	StartP2P()
    19  	CloseP2P()
    20  }
    21  
    22  // Manager p2p manager
    23  type Manager struct {
    24  	SysAPI   client.QueueProtocolAPI
    25  	Client   queue.Client
    26  	ChainCfg *types.TuringchainConfig
    27  	PubSub   *pubsub.PubSub
    28  	//subChan         chan interface{}
    29  	broadcastFilter *lru.Cache
    30  	p2ps            []IP2P
    31  	p2pCfg          *types.P2P
    32  }
    33  
    34  // CreateP2P p2p creator
    35  type CreateP2P func(mgr *Manager, subCfg []byte) IP2P
    36  
    37  var (
    38  	p2pRegTypes = make(map[string]CreateP2P)
    39  )
    40  
    41  // RegisterP2PCreate register p2p create func
    42  func RegisterP2PCreate(p2pType string, create CreateP2P) {
    43  
    44  	if create == nil {
    45  		panic("RegisterP2P, nil CreateP2P, p2pType = " + p2pType)
    46  	}
    47  	if _, ok := p2pRegTypes[p2pType]; ok {
    48  		panic("RegisterP2P, duplicate, p2pType = " + p2pType)
    49  	}
    50  
    51  	p2pRegTypes[p2pType] = create
    52  }
    53  
    54  // LoadP2PCreate load p2p create func
    55  func LoadP2PCreate(p2pType string) CreateP2P {
    56  
    57  	if create, ok := p2pRegTypes[p2pType]; ok {
    58  		return create
    59  	}
    60  	panic("LoadP2P, not exist, p2pType = " + p2pType)
    61  }
    62  
    63  // NewP2PMgr new p2p manager
    64  func NewP2PMgr(cfg *types.TuringchainConfig) *Manager {
    65  	mgr := &Manager{
    66  		PubSub: pubsub.NewPubSub(1024),
    67  	}
    68  	var err error
    69  	mgr.broadcastFilter, err = lru.New(10240)
    70  	if err != nil {
    71  		panic("NewP2PBasePanic, err=" + err.Error())
    72  	}
    73  	mgr.ChainCfg = cfg
    74  	mgr.p2pCfg = cfg.GetModuleConfig().P2P
    75  	// set default p2p config
    76  	if len(mgr.p2pCfg.Types) == 0 {
    77  		panic("NewP2PBasePanic, Missing config p2p.types")
    78  	}
    79  
    80  	return mgr
    81  }
    82  
    83  // Wait wait p2p
    84  func (mgr *Manager) Wait() {}
    85  
    86  // Close close p2p
    87  func (mgr *Manager) Close() {
    88  
    89  	for _, p2p := range mgr.p2ps {
    90  		p2p.CloseP2P()
    91  		p2p = nil
    92  	}
    93  	mgr.p2ps = nil
    94  	//mgr.PubSub.Unsub(mgr.subChan)
    95  	if mgr.Client != nil {
    96  		mgr.Client.Close()
    97  	}
    98  }
    99  
   100  // SetQueueClient set the queue
   101  func (mgr *Manager) SetQueueClient(cli queue.Client) {
   102  	var err error
   103  	mgr.SysAPI, err = client.New(cli, nil)
   104  	if err != nil {
   105  		panic("SetQueueClient client.New err:" + err.Error())
   106  	}
   107  	mgr.Client = cli
   108  
   109  	p2pCfg := mgr.ChainCfg.GetModuleConfig().P2P
   110  	subCfg := mgr.ChainCfg.GetSubConfig().P2P
   111  
   112  	for _, p2pTy := range p2pCfg.Types {
   113  
   114  		newP2P := LoadP2PCreate(p2pTy)(mgr, subCfg[p2pTy])
   115  		newP2P.StartP2P()
   116  		mgr.p2ps = append(mgr.p2ps, newP2P)
   117  	}
   118  
   119  	go mgr.handleSysEvent()
   120  	//go mgr.handleP2PSub()
   121  }