gopkg.in/dedis/onet.v2@v2.0.0-20181115163211-c8f3724038a7/messages.go (about)

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