go.dedis.ch/onet/v4@v4.0.0-pre1/messages.go (about)

     1  package onet
     2  
     3  import (
     4  	"go.dedis.ch/onet/v4/network"
     5  	"gopkg.in/satori/go.uuid.v1"
     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  }
    51  
    52  // ConfigMsg is sent by the overlay containing a generic slice of bytes to
    53  // give to service in the `NewProtocol` method.
    54  type ConfigMsg struct {
    55  	Config GenericConfig
    56  	Dest   TokenID
    57  }
    58  
    59  // RoundID uniquely identifies a round of a protocol run
    60  type RoundID uuid.UUID
    61  
    62  // String returns the canonical representation of the rounds ID (wrapper around // uuid.UUID.String())
    63  func (rId RoundID) String() string {
    64  	return uuid.UUID(rId).String()
    65  }
    66  
    67  // Equal returns true if and only if rID2 equals this RoundID.
    68  func (rId RoundID) Equal(rID2 RoundID) bool {
    69  	return uuid.Equal(uuid.UUID(rId), uuid.UUID(rID2))
    70  }
    71  
    72  // IsNil returns true iff the RoundID is Nil
    73  func (rId RoundID) IsNil() bool {
    74  	return rId.Equal(RoundID(uuid.Nil))
    75  }
    76  
    77  // TokenID uniquely identifies the start and end-point of a message by an ID
    78  // (see Token struct)
    79  type TokenID uuid.UUID
    80  
    81  // String returns the canonical representation of the TokenID (wrapper around // uuid.UUID.String())
    82  func (t TokenID) String() string {
    83  	return uuid.UUID(t).String()
    84  }
    85  
    86  // Equal returns true if and only if t2 equals this TokenID.
    87  func (t TokenID) Equal(t2 TokenID) bool {
    88  	return uuid.Equal(uuid.UUID(t), uuid.UUID(t2))
    89  }
    90  
    91  // IsNil returns true iff the TokenID is Nil
    92  func (t TokenID) IsNil() bool {
    93  	return t.Equal(TokenID(uuid.Nil))
    94  }
    95  
    96  // A Token contains all identifiers needed to uniquely identify one protocol
    97  // instance. It gets passed when a new protocol instance is created and get used
    98  // by every protocol instance when they want to send a message. That way, the
    99  // host knows how to create the ProtocolMsg message around the protocol's message
   100  // with the right fields set.
   101  type Token struct {
   102  	RosterID RosterID
   103  	TreeID   TreeID
   104  	// TO BE REMOVED
   105  	ProtoID   ProtocolID
   106  	ServiceID ServiceID
   107  	RoundID   RoundID
   108  	// TreeNodeID is defined by the
   109  	TreeNodeID TreeNodeID
   110  }
   111  
   112  // ID returns the TokenID which can be used to identify by token in map
   113  func (t *Token) ID() TokenID {
   114  	// TODO: This used to have a caching mechanism, but it caused data races.
   115  	// See issue #239. When tuning performance, if this shows up as a hot path,
   116  	// we need to add caching back in (safely, this time).
   117  	url := network.NamespaceURL + "token/" + t.RosterID.String() +
   118  		t.RoundID.String() + t.ServiceID.String() + t.ProtoID.String() + t.TreeID.String() +
   119  		t.TreeNodeID.String()
   120  	return TokenID(uuid.NewV5(uuid.NamespaceURL, url))
   121  }
   122  
   123  // Clone returns a new token out of this one
   124  func (t *Token) Clone() *Token {
   125  	t2 := *t
   126  	return &t2
   127  }
   128  
   129  // ChangeTreeNodeID return a new Token containing a reference to the given
   130  // TreeNode
   131  func (t *Token) ChangeTreeNodeID(newid TreeNodeID) *Token {
   132  	tOther := *t
   133  	tOther.TreeNodeID = newid
   134  	return &tOther
   135  }
   136  
   137  // TreeNodeInfo holds the sender and the destination of the message.
   138  type TreeNodeInfo struct {
   139  	To   *Token
   140  	From *Token
   141  }
   142  
   143  // OverlayMsg contains all routing-information about the tree and the
   144  // roster.
   145  type OverlayMsg struct {
   146  	TreeNodeInfo *TreeNodeInfo
   147  
   148  	// Deprecated: roster is not sent/requested anymore, only the tree
   149  	RequestRoster *RequestRoster
   150  	// Deprecated: roster is not sent/requested anymore, only the tree
   151  	Roster *Roster
   152  
   153  	RequestTree  *RequestTree
   154  	ResponseTree *ResponseTree
   155  	// Deprecated: use ResponseTree to send the tree and the roster
   156  	TreeMarshal *TreeMarshal
   157  }
   158  
   159  // RequestRoster is used to ask the parent for a given Roster
   160  type RequestRoster struct {
   161  	RosterID RosterID
   162  }
   163  
   164  // RequestTree is used to ask the parent for a given Tree
   165  type RequestTree struct {
   166  	// The treeID of the tree we want
   167  	TreeID TreeID
   168  	// Version of the request tree
   169  	Version uint32
   170  }
   171  
   172  // ResponseTree contains the information to build a tree
   173  type ResponseTree struct {
   174  	TreeMarshal *TreeMarshal
   175  	Roster      *Roster
   176  }
   177  
   178  // RosterUnknown is used in case the entity list is unknown
   179  type RosterUnknown struct {
   180  }