go.dedis.ch/onet/v3@v3.2.11-0.20210930124529-e36530bca7ef/messages.go (about)

     1  package onet
     2  
     3  import (
     4  	"github.com/google/uuid"
     5  	"go.dedis.ch/onet/v3/network"
     6  )
     7  
     8  // ProtocolMsgID is to be embedded in every message that is made for a
     9  // ID of ProtocolMsg message as registered in network
    10  var ProtocolMsgID = network.RegisterMessage(ProtocolMsg{})
    11  
    12  // RequestTreeMsgID of RequestTree message as registered in network
    13  var RequestTreeMsgID = network.RegisterMessage(RequestTree{})
    14  
    15  // ResponseTreeMsgID of TreeMarshal message as registered in network
    16  var ResponseTreeMsgID = network.RegisterMessage(ResponseTree{})
    17  
    18  // SendTreeMsgID of TreeMarshal message as registered in network
    19  // Deprecated: use ResponseTreeMsgID
    20  var SendTreeMsgID = TreeMarshalTypeID
    21  
    22  // RequestRosterMsgID of RequestRoster message as registered in network
    23  // Deprecated: only the tree is sent, not anymore the roster
    24  var RequestRosterMsgID = network.RegisterMessage(RequestRoster{})
    25  
    26  // SendRosterMsgID of Roster message as registered in network
    27  // Deprecated: only the tree is sent, not anymore the roster
    28  var SendRosterMsgID = RosterTypeID
    29  
    30  // ConfigMsgID of the generic config message
    31  var ConfigMsgID = network.RegisterMessage(ConfigMsg{})
    32  
    33  // ProtocolMsg is to be embedded in every message that is made for a
    34  // ProtocolInstance
    35  type ProtocolMsg struct {
    36  	// Token uniquely identify the protocol instance this msg is made for
    37  	From *Token
    38  	// The TreeNodeId Where the message goes to
    39  	To *Token
    40  	// NOTE: this is taken from network.NetworkMessage
    41  	ServerIdentity *network.ServerIdentity
    42  	// MsgType of the underlying data
    43  	MsgType network.MessageTypeID
    44  	// The interface to the actual Data
    45  	Msg network.Message
    46  	// The actual data as binary blob
    47  	MsgSlice []byte
    48  	// The size of the data
    49  	Size network.Size
    50  	// Config is the config passed to the protocol constructor.
    51  	Config *GenericConfig
    52  }
    53  
    54  // ConfigMsg is sent by the overlay containing a generic slice of bytes to
    55  // give to service in the `NewProtocol` method.
    56  type ConfigMsg struct {
    57  	Config GenericConfig
    58  	Dest   TokenID
    59  }
    60  
    61  // RoundID uniquely identifies a round of a protocol run
    62  type RoundID uuid.UUID
    63  
    64  // String returns the canonical representation of the rounds ID (wrapper around // uuid.UUID.String())
    65  func (rId RoundID) String() string {
    66  	return uuid.UUID(rId).String()
    67  }
    68  
    69  // Equal returns true if and only if rID2 equals this RoundID.
    70  func (rId RoundID) Equal(rID2 RoundID) bool {
    71  	return rId == rID2
    72  }
    73  
    74  // IsNil returns true iff the RoundID is Nil
    75  func (rId RoundID) IsNil() bool {
    76  	return rId.Equal(RoundID(uuid.Nil))
    77  }
    78  
    79  // TokenID uniquely identifies the start and end-point of a message by an ID
    80  // (see Token struct)
    81  type TokenID uuid.UUID
    82  
    83  // String returns the canonical representation of the TokenID (wrapper around // uuid.UUID.String())
    84  func (t TokenID) String() string {
    85  	return uuid.UUID(t).String()
    86  }
    87  
    88  // Equal returns true if and only if t2 equals this TokenID.
    89  func (t TokenID) Equal(t2 TokenID) bool {
    90  	return t == t2
    91  }
    92  
    93  // IsNil returns true iff the TokenID is Nil
    94  func (t TokenID) IsNil() bool {
    95  	return t.Equal(TokenID(uuid.Nil))
    96  }
    97  
    98  // A Token contains all identifiers needed to uniquely identify one protocol
    99  // instance. It gets passed when a new protocol instance is created and get used
   100  // by every protocol instance when they want to send a message. That way, the
   101  // host knows how to create the ProtocolMsg message around the protocol's message
   102  // with the right fields set.
   103  type Token struct {
   104  	RosterID RosterID
   105  	TreeID   TreeID
   106  	// TO BE REMOVED
   107  	ProtoID   ProtocolID
   108  	ServiceID ServiceID
   109  	RoundID   RoundID
   110  	// TreeNodeID is defined by the
   111  	TreeNodeID TreeNodeID
   112  }
   113  
   114  // ID returns the TokenID which can be used to identify by token in map
   115  func (t *Token) ID() TokenID {
   116  	// TODO: This used to have a caching mechanism, but it caused data races.
   117  	// See issue #239. When tuning performance, if this shows up as a hot path,
   118  	// we need to add caching back in (safely, this time).
   119  	url := network.NamespaceURL + "token/" + t.RosterID.String() +
   120  		t.RoundID.String() + t.ServiceID.String() + t.ProtoID.String() + t.TreeID.String() +
   121  		t.TreeNodeID.String()
   122  	return TokenID(uuid.NewSHA1(uuid.NameSpaceURL, []byte(url)))
   123  }
   124  
   125  // Clone returns a new token out of this one
   126  func (t *Token) Clone() *Token {
   127  	t2 := *t
   128  	return &t2
   129  }
   130  
   131  // ChangeTreeNodeID return a new Token containing a reference to the given
   132  // TreeNode
   133  func (t *Token) ChangeTreeNodeID(newid TreeNodeID) *Token {
   134  	tOther := *t
   135  	tOther.TreeNodeID = newid
   136  	return &tOther
   137  }
   138  
   139  // TreeNodeInfo holds the sender and the destination of the message.
   140  type TreeNodeInfo struct {
   141  	To   *Token
   142  	From *Token
   143  }
   144  
   145  // OverlayMsg contains all routing-information about the tree and the
   146  // roster.
   147  type OverlayMsg struct {
   148  	TreeNodeInfo *TreeNodeInfo
   149  
   150  	// Deprecated: roster is not sent/requested anymore, only the tree
   151  	RequestRoster *RequestRoster
   152  	// Deprecated: roster is not sent/requested anymore, only the tree
   153  	Roster *Roster
   154  
   155  	RequestTree  *RequestTree
   156  	ResponseTree *ResponseTree
   157  	// Deprecated: use ResponseTree to send the tree and the roster
   158  	TreeMarshal *TreeMarshal
   159  
   160  	Config *GenericConfig
   161  }
   162  
   163  // RequestRoster is used to ask the parent for a given Roster
   164  type RequestRoster struct {
   165  	RosterID RosterID
   166  }
   167  
   168  // RequestTree is used to ask the parent for a given Tree
   169  type RequestTree struct {
   170  	// The treeID of the tree we want
   171  	TreeID TreeID
   172  	// Version of the request tree
   173  	Version uint32
   174  }
   175  
   176  // ResponseTree contains the information to build a tree
   177  type ResponseTree struct {
   178  	TreeMarshal *TreeMarshal
   179  	Roster      *Roster
   180  }
   181  
   182  // RosterUnknown is used in case the entity list is unknown
   183  type RosterUnknown struct {
   184  }