protocol

package
v0.0.0-...-20cb4a4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2019 License: AGPL-3.0 Imports: 4 Imported by: 0

README

Navigation: DEDIS :: Cothority Template :: Protocol Template

Protocol Template

In the this directory, you will find the implementation of a toy protocol where nodes work together to count how many instances of the protocol there are. It demonstrates how to send messages and how to handle incoming messages.

To implement a new protocol, you must register the kinds of messages it passes, and also the protocol itself:

func init() {
	network.RegisterMessage(Announce{})
	network.RegisterMessage(Reply{})
	onet.GlobalProtocolRegister(Name, NewProtocol)
}

The messages are defined in the file struct.go. For each message, you need to define the message itself, and the message as it will arrive to you from the cothority server.

After registering, define a struct that implements the onet.ProtocolInstance interface:

type TemplateProtocol struct {
  *onet.TreeNodeInstance
  ...
}

// Check that *TemplateProtocol implements onet.ProtocolInstance
var _ onet.ProtocolInstance = (*TemplateProtocol)(nil)

Next, define the function that generates a new protocol instance. Using the newly created onet.ProtocolInstance, you can call RegisterHandler in order to register handlers for each of the message types. Later, the server will choose the correct handler to call based on which message type arrives.

Any state that is needed by the protocol (for example, the ChildCount channel) should be initialized here.

Finally, define the Start function that will initiate the protocol instance:

func (p *ProtocolExampleHandlers) Start() error {...}

Documentation

Overview

Package protocol contains an example demonstrating how to write a protocol and a simulation.

The protocol has two messages:

  • Announce which is sent from the root down the tree
  • Reply which is sent back up to the root

If you want to add other messages, be sure to follow the way Announce and StructAnnounce are set up.

A simple protocol uses four files: - struct.go defines the messages sent around - protocol.go defines the actions for each message - protocol_test.go tests the protocol in a local test - simulation.go tests the protocol on distant platforms like deterlab

Index

Constants

View Source
const Name = "Template"

Name can be used from other packages to refer to this protocol.

Variables

This section is empty.

Functions

func NewProtocol

func NewProtocol(n *onet.TreeNodeInstance) (onet.ProtocolInstance, error)

NewProtocol initialises the structure for use in one round

Types

type Announce

type Announce struct {
	Message string
}

Announce is used to pass a message to all children.

type Reply

type Reply struct {
	ChildrenCount int
}

Reply returns the count of all children.

type StructAnnounce

type StructAnnounce struct {
	*onet.TreeNode
	Announce
}

StructAnnounce just contains Announce and the data necessary to identify and process the message in the sda framework.

type StructReply

type StructReply struct {
	*onet.TreeNode
	Reply
}

StructReply just contains Reply and the data necessary to identify and process the message in the sda framework.

type TemplateProtocol

type TemplateProtocol struct {
	*onet.TreeNodeInstance
	ChildCount chan int
}

TemplateProtocol holds the state of a given protocol.

For this example, it defines a channel that will receive the number of children. Only the root-node will write to the channel.

func (*TemplateProtocol) HandleAnnounce

func (p *TemplateProtocol) HandleAnnounce(msg StructAnnounce) error

HandleAnnounce is the first message and is used to send an ID that is stored in all nodes.

func (*TemplateProtocol) HandleReply

func (p *TemplateProtocol) HandleReply(reply []StructReply) error

HandleReply is the message going up the tree and holding a counter to verify the number of nodes.

func (*TemplateProtocol) Start

func (p *TemplateProtocol) Start() error

Start sends the Announce-message to all children

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址