github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/messagevalue.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pcommon
     7  
     8  import "time"
     9  
    10  // MessageValue is basic implementation of Message. It is used since p2p v0.3
    11  type MessageValue struct {
    12  	subProtocol SubProtocol
    13  	// Length is length of payload
    14  	length uint32
    15  	// timestamp is unix time (precision of nanosecond)
    16  	timestamp int64
    17  	// ID is 16 bytes unique identifier
    18  	id MsgID
    19  	// OriginalID is message id of request which trigger this message. it will be all zero, if message is request or notice.
    20  	originalID MsgID
    21  
    22  	// marshaled by google protocol buffer v3. object is determined by Subprotocol
    23  	payload []byte
    24  }
    25  
    26  // NewLiteMessageValue create MessageValue object which payload is empty
    27  func NewLiteMessageValue(protocol SubProtocol, msgID, originalID MsgID, timestamp int64,) *MessageValue {
    28  	return &MessageValue{id: msgID, originalID: originalID, timestamp: timestamp, subProtocol: protocol}
    29  }
    30  
    31  // NewMessageValue create a new object
    32  func NewMessageValue(protocol SubProtocol, msgID, originalID MsgID, timestamp int64, payload []byte) *MessageValue {
    33  	msg := NewLiteMessageValue(protocol, msgID, originalID, timestamp)
    34  	msg.SetPayload(payload)
    35  	return msg
    36  }
    37  
    38  func NewSimpleMsgVal(protocol SubProtocol, msgID MsgID) *MessageValue {
    39  	return NewLiteMessageValue(protocol, msgID, EmptyID, time.Now().UnixNano())
    40  }
    41  
    42  func NewSimpleRespMsgVal(protocol SubProtocol, msgID MsgID, originalID MsgID) *MessageValue {
    43  	return NewLiteMessageValue(protocol, msgID, originalID, time.Now().UnixNano())
    44  }
    45  
    46  func (m *MessageValue) Subprotocol() SubProtocol {
    47  	return m.subProtocol
    48  }
    49  
    50  func (m *MessageValue) Length() uint32 {
    51  	return m.length
    52  
    53  }
    54  
    55  func (m *MessageValue) Timestamp() int64 {
    56  	return m.timestamp
    57  }
    58  
    59  func (m *MessageValue) ID() MsgID {
    60  	return m.id
    61  }
    62  
    63  func (m *MessageValue) OriginalID() MsgID {
    64  	return m.originalID
    65  }
    66  
    67  func (m *MessageValue) Payload() []byte {
    68  	return m.payload
    69  }
    70  
    71  func (m *MessageValue) SetPayload(payload []byte) {
    72  	m.payload = payload
    73  	m.length = uint32(len(payload))
    74  }
    75  
    76  var _ Message = (*MessageValue)(nil)
    77