github.com/klaytn/klaytn@v1.12.1/node/cn/channel_manager.go (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package cn 18 19 import ( 20 "fmt" 21 22 "github.com/klaytn/klaytn/networks/p2p" 23 ) 24 25 const ( 26 BlockChannel uint = iota 27 TxChannel 28 ConsensusChannel 29 MiscChannel 30 MaxChannel 31 ) 32 33 type ChannelManager struct { 34 msgChannels [][]chan p2p.Msg 35 msgCodes map[uint64]uint 36 } 37 38 // NewChannelManager returns a new ChannelManager. 39 // The ChannelManager manages the channel for the msg code. 40 func NewChannelManager(channelSize int) *ChannelManager { 41 channelMgr := &ChannelManager{ 42 msgChannels: make([][]chan p2p.Msg, 0, channelSize), 43 msgCodes: make(map[uint64]uint), 44 } 45 46 for i := 0; i < channelSize; i++ { 47 channelMgr.msgChannels = append(channelMgr.msgChannels, make([]chan p2p.Msg, MaxChannel, MaxChannel)) 48 } 49 50 // register channelType to MsgCode 51 channelMgr.RegisterMsgCode(BlockChannel, NewBlockHashesMsg) 52 channelMgr.RegisterMsgCode(BlockChannel, BlockHeaderFetchRequestMsg) 53 channelMgr.RegisterMsgCode(BlockChannel, BlockHeaderFetchResponseMsg) 54 channelMgr.RegisterMsgCode(BlockChannel, BlockBodiesFetchRequestMsg) 55 channelMgr.RegisterMsgCode(BlockChannel, BlockBodiesFetchResponseMsg) 56 channelMgr.RegisterMsgCode(BlockChannel, BlockHeadersRequestMsg) 57 channelMgr.RegisterMsgCode(BlockChannel, BlockHeadersMsg) 58 channelMgr.RegisterMsgCode(BlockChannel, BlockBodiesRequestMsg) 59 channelMgr.RegisterMsgCode(BlockChannel, BlockBodiesMsg) 60 channelMgr.RegisterMsgCode(BlockChannel, NewBlockMsg) 61 62 channelMgr.RegisterMsgCode(TxChannel, TxMsg) 63 64 channelMgr.RegisterMsgCode(MiscChannel, ReceiptsRequestMsg) 65 channelMgr.RegisterMsgCode(MiscChannel, ReceiptsMsg) 66 channelMgr.RegisterMsgCode(MiscChannel, StatusMsg) 67 channelMgr.RegisterMsgCode(MiscChannel, NodeDataRequestMsg) 68 channelMgr.RegisterMsgCode(MiscChannel, NodeDataMsg) 69 channelMgr.RegisterMsgCode(MiscChannel, StakingInfoRequestMsg) 70 channelMgr.RegisterMsgCode(MiscChannel, StakingInfoMsg) 71 72 return channelMgr 73 } 74 75 // RegisterChannelWithIndex registers the channel corresponding to network and channel ID. 76 func (cm *ChannelManager) RegisterChannelWithIndex(idx int, channelId uint, channel chan p2p.Msg) { 77 cm.msgChannels[idx][channelId] = channel 78 } 79 80 // RegisterMsgCode registers the channel id corresponding to msgCode. 81 func (cm *ChannelManager) RegisterMsgCode(channelId uint, msgCode uint64) { 82 cm.msgCodes[msgCode] = channelId 83 } 84 85 // GetChannelWithMsgCode returns the channel corresponding to msgCode. 86 func (cm *ChannelManager) GetChannelWithMsgCode(idx int, msgCode uint64) (chan p2p.Msg, error) { 87 if channelID, ok := cm.msgCodes[msgCode]; ok { 88 return cm.msgChannels[idx][channelID], nil 89 } else { 90 return nil, fmt.Errorf("there is no channel for idx:%v, msgCode:%v", idx, msgCode) 91 } 92 }