github.com/igggame/nebulas-go@v2.1.0+incompatible/net/types.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package net
    20  
    21  import (
    22  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/gogo/protobuf/proto"
    26  	"github.com/nebulasio/go-nebulas/crypto/hash"
    27  	"github.com/nebulasio/go-nebulas/util/byteutils"
    28  )
    29  
    30  // Message Priority.
    31  const (
    32  	MessagePriorityHigh = iota
    33  	MessagePriorityNormal
    34  	MessagePriorityLow
    35  )
    36  
    37  // Sync Message Type
    38  const (
    39  	ChunkHeadersRequest  = "sync"      // ChainSync
    40  	ChunkHeadersResponse = "chunks"    // ChainChunks
    41  	ChunkDataRequest     = "getchunk"  // ChainGetChunk
    42  	ChunkDataResponse    = "chunkdata" // ChainChunkData
    43  )
    44  
    45  // Sync Errors
    46  var (
    47  	ErrPeersIsNotEnough = errors.New("peers is not enough")
    48  )
    49  
    50  // MessageType a string for message type.
    51  type MessageType string
    52  
    53  // Message interface for message.
    54  type Message interface {
    55  	MessageType() string
    56  	MessageFrom() string
    57  	Data() []byte
    58  	Hash() string
    59  }
    60  
    61  // Serializable model
    62  type Serializable interface {
    63  	ToProto() (proto.Message, error)
    64  	FromProto(proto.Message) error
    65  }
    66  
    67  // PeersSlice is a slice which contains peers
    68  type PeersSlice []interface{}
    69  
    70  // PeerFilterAlgorithm is the algorithm used to filter peers
    71  type PeerFilterAlgorithm interface {
    72  	Filter(PeersSlice) PeersSlice
    73  }
    74  
    75  // Service net Service interface
    76  type Service interface {
    77  	Start() error
    78  	Stop()
    79  
    80  	Node() *Node
    81  
    82  	Register(...*Subscriber)
    83  	Deregister(...*Subscriber)
    84  
    85  	Broadcast(string, Serializable, int)
    86  	Relay(string, Serializable, int)
    87  	SendMsg(string, []byte, string, int) error
    88  
    89  	SendMessageToPeers(messageName string, data []byte, priority int, filter PeerFilterAlgorithm) []string
    90  	SendMessageToPeer(messageName string, data []byte, priority int, peerID string) error
    91  
    92  	ClosePeer(peerID string, reason error)
    93  
    94  	BroadcastNetworkID([]byte)
    95  }
    96  
    97  // MessageWeight float64
    98  type MessageWeight float64
    99  
   100  // const
   101  const (
   102  	MessageWeightZero = MessageWeight(0)
   103  	MessageWeightNewTx
   104  	MessageWeightNewBlock = MessageWeight(0.5)
   105  	MessageWeightRouteTable
   106  	MessageWeightChainChunks
   107  	MessageWeightChainChunkData
   108  )
   109  
   110  // Subscriber subscriber.
   111  type Subscriber struct {
   112  	// id usually the owner/creator, used for troubleshooting .
   113  	id interface{}
   114  
   115  	// msgChan chan for subscribed message.
   116  	msgChan chan Message
   117  
   118  	// msgType message type to subscribe
   119  	msgType string
   120  
   121  	// msgWeight weight of msgType
   122  	msgWeight MessageWeight
   123  
   124  	// doFilter dup message
   125  	doFilter bool
   126  }
   127  
   128  // func NewSubscriber(id interface{}, msgChan chan Message, doFilter bool, msgTypes ...string) *Subscriber {
   129  // 	return &Subscriber{id, msgChan, msgTypes, doFilter}
   130  // }
   131  
   132  // NewSubscriber return new Subscriber instance.
   133  func NewSubscriber(id interface{}, msgChan chan Message, doFilter bool, msgType string, weight MessageWeight) *Subscriber {
   134  	return &Subscriber{id, msgChan, msgType, weight, doFilter}
   135  }
   136  
   137  // ID return id.
   138  func (s *Subscriber) ID() interface{} {
   139  	return s.id
   140  }
   141  
   142  // MessageType return msgTypes.
   143  func (s *Subscriber) MessageType() string {
   144  	return s.msgType
   145  }
   146  
   147  // MessageChan return msgChan.
   148  func (s *Subscriber) MessageChan() chan Message {
   149  	return s.msgChan
   150  }
   151  
   152  // MessageWeight return weight of msgType
   153  func (s *Subscriber) MessageWeight() MessageWeight {
   154  	return s.msgWeight
   155  }
   156  
   157  // DoFilter return doFilter
   158  func (s *Subscriber) DoFilter() bool {
   159  	return s.doFilter
   160  }
   161  
   162  // BaseMessage base message
   163  type BaseMessage struct {
   164  	t    string
   165  	from string
   166  	data []byte
   167  }
   168  
   169  // NewBaseMessage new base message
   170  func NewBaseMessage(t string, from string, data []byte) Message {
   171  	return &BaseMessage{t: t, from: from, data: data}
   172  }
   173  
   174  // MessageType get message type
   175  func (msg *BaseMessage) MessageType() string {
   176  	return msg.t
   177  }
   178  
   179  // MessageFrom get message who send
   180  func (msg *BaseMessage) MessageFrom() string {
   181  	return msg.from
   182  }
   183  
   184  // Data get the message data
   185  func (msg *BaseMessage) Data() []byte {
   186  	return msg.data
   187  }
   188  
   189  // Hash return the message hash
   190  func (msg *BaseMessage) Hash() string {
   191  	return byteutils.Hex(hash.Sha3256(msg.data))
   192  }
   193  
   194  // String get the message to string
   195  func (msg *BaseMessage) String() string {
   196  	return fmt.Sprintf("BaseMessage {type:%s; data:%s; from:%s}",
   197  		msg.t,
   198  		msg.data,
   199  		msg.from,
   200  	)
   201  }