github.com/amazechain/amc@v0.1.3/common/message/message.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package message
    18  
    19  import (
    20  	"github.com/libp2p/go-libp2p/core/peer"
    21  )
    22  
    23  const (
    24  	MsgConnect = MessageType(iota + 1)
    25  	MsgPingReq
    26  	MsgPingResp
    27  	MsgSystem
    28  	MsgAppHandshake
    29  	MsgApplication
    30  	MsgDownloader
    31  	MsgNewBlock
    32  	MsgDisconnect
    33  	MsgTransaction
    34  	MsgTypeFirstInvalid
    35  )
    36  
    37  type MessageType uint8
    38  
    39  func (mt MessageType) IsValid() bool {
    40  	return mt >= MsgConnect && mt < MsgTypeFirstInvalid
    41  }
    42  
    43  // String implements the stringer interface.
    44  func (mt MessageType) String() string {
    45  	switch mt {
    46  	case MsgConnect:
    47  		return "Connect"
    48  	case MsgPingReq:
    49  		return "PingRequest"
    50  	case MsgPingResp:
    51  		return "PingResponse"
    52  	case MsgAppHandshake:
    53  		return "AppHandshake"
    54  	case MsgApplication:
    55  		return "MsgApplication"
    56  	case MsgDownloader:
    57  		return "MsgDownloader"
    58  	case MsgTransaction:
    59  		return "MsgTransaction"
    60  	default:
    61  		return "unknown"
    62  	}
    63  }
    64  
    65  type IMessage interface {
    66  	Type() MessageType
    67  	Peer() peer.ID
    68  	Broadcast() bool
    69  	Encode() ([]byte, error)
    70  	Decode(MessageType, []byte) error
    71  }