github.com/aergoio/aergo@v1.3.1/polaris/common/message.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package common
     7  
     8  import (
     9  	"github.com/aergoio/aergo/p2p/p2pcommon"
    10  	"time"
    11  )
    12  
    13  // PolarisMessage is data struct for transferring between polaris server and client.
    14  // as of 2019.04.23, this is copy of MessageValue.
    15  type PolarisMessage struct {
    16  	subProtocol p2pcommon.SubProtocol
    17  	// Length is lenght of payload
    18  	length uint32
    19  	// timestamp is unix time (precision of second)
    20  	timestamp int64
    21  	// ID is 16 bytes unique identifier
    22  	id p2pcommon.MsgID
    23  	// OriginalID is message id of request which trigger this message. it will be all zero, if message is request or notice.
    24  	originalID p2pcommon.MsgID
    25  
    26  	// marshaled by google protocol buffer v3. object is determined by Subprotocol
    27  	payload []byte
    28  }
    29  
    30  
    31  // NewPolarisMessage create a new object
    32  func NewPolarisMessage(msgID p2pcommon.MsgID, protocol p2pcommon.SubProtocol, payload []byte) *PolarisMessage {
    33  	return &PolarisMessage{id: msgID, timestamp:time.Now().UnixNano(), subProtocol:protocol,payload:payload,length:uint32(len(payload))}
    34  }
    35  func NewPolarisRespMessage(msgID , orgReqID p2pcommon.MsgID, protocol p2pcommon.SubProtocol, payload []byte) *PolarisMessage {
    36  	return &PolarisMessage{id: msgID, originalID:orgReqID, timestamp:time.Now().UnixNano(), subProtocol:protocol,payload:payload,length:uint32(len(payload))}
    37  }
    38  
    39  func (m *PolarisMessage) Subprotocol() p2pcommon.SubProtocol {
    40  	return m.subProtocol
    41  }
    42  
    43  func (m *PolarisMessage) Length() uint32 {
    44  	return m.length
    45  
    46  }
    47  
    48  func (m *PolarisMessage) Timestamp() int64 {
    49  	return m.timestamp
    50  }
    51  
    52  func (m *PolarisMessage) ID() p2pcommon.MsgID {
    53  	return m.id
    54  }
    55  
    56  func (m *PolarisMessage) OriginalID() p2pcommon.MsgID {
    57  	return m.originalID
    58  }
    59  
    60  func (m *PolarisMessage) Payload() []byte {
    61  	return m.payload
    62  }
    63  
    64  var _ p2pcommon.Message = (*PolarisMessage)(nil)