Example #1
0
func TestNodeChannel(t *testing.T) {
	defer log.AfterTest(t)
	local := sda.NewLocalTest()
	_, _, tree := local.GenTree(2, false, true, true)
	defer local.CloseAll()

	p, err := local.CreateProtocol(tree, "ProtocolChannels")
	if err != nil {
		t.Fatal("Couldn't create new node:", err)
	}
	c := make(chan struct {
		*sda.TreeNode
		NodeTestMsg
	}, 1)
	tni := p.(*ProtocolChannels).TreeNodeInstance
	err = tni.RegisterChannel(c)
	if err != nil {
		t.Fatal("Couldn't register channel:", err)
	}
	err = tni.DispatchChannel([]*sda.ProtocolMsg{&sda.ProtocolMsg{
		Msg:     NodeTestMsg{3},
		MsgType: network.RegisterMessageType(NodeTestMsg{}),
		From: &sda.Token{
			TreeID:     tree.ID,
			TreeNodeID: tree.Root.ID,
		}},
	})
	if err != nil {
		t.Fatal("Couldn't dispatch to channel:", err)
	}
	msg := <-c
	if msg.I != 3 {
		t.Fatal("Message should contain '3'")
	}
}
Example #2
0
// This makes h2 the leader, so it creates a tree and entity list
// and start a protocol. H1 should receive that message and request the entitity
// list and the treelist and then instantiate the protocol.
func TestProtocolAutomaticInstantiation(t *testing.T) {
	defer log.AfterTest(t)

	log.TestOutput(testing.Verbose(), 4)
	// setup
	chanH1 := make(chan bool)
	chanH2 := make(chan bool)
	chans := []chan bool{chanH1, chanH2}
	id := 0
	// custom creation function so we know the step due to the channels
	fn := func(n *sda.TreeNodeInstance) (sda.ProtocolInstance, error) {
		ps := SimpleProtocol{
			TreeNodeInstance: n,
			Chan:             chans[id],
		}
		ps.RegisterHandler(ps.ReceiveMessage)
		id++
		return &ps, nil
	}

	network.RegisterMessageType(SimpleMessage{})
	sda.ProtocolRegisterName(simpleProto, fn)
	h1, h2 := SetupTwoHosts(t, true)
	defer h1.Close()
	defer h2.Close()
	h1.StartProcessMessages()
	// create small Tree
	el := sda.NewRoster([]*network.ServerIdentity{h1.ServerIdentity, h2.ServerIdentity})
	h1.AddRoster(el)
	tree := el.GenerateBinaryTree()
	h1.AddTree(tree)
	// start the protocol
	go func() {
		_, err := h1.StartProtocol(simpleProto, tree)
		if err != nil {
			t.Fatal(fmt.Sprintf("Could not start protocol %v", err))
		}
	}()

	// we are supposed to receive something from host1 from Start()
	select {
	case _ = <-chanH1:
		break
	case <-time.After(200 * time.Millisecond):
		t.Fatal("Could not receive from channel of host 1")
	}
	// Then we are supposed to receive from h2 after he got the tree and the
	// entity list from h1
	select {
	case _ = <-chanH2:
		break
	case <-time.After(2 * time.Second):
		t.Fatal("Could not receive from channel of host 1")
	}
}
Example #3
0
// Save takes everything in the SimulationConfig structure and saves it to
// dir + SimulationFileName
func (sc *SimulationConfig) Save(dir string) error {
	network.RegisterMessageType(&SimulationConfigFile{})
	scf := &SimulationConfigFile{
		TreeMarshal: sc.Tree.MakeTreeMarshal(),
		Roster:      sc.Roster,
		PrivateKeys: sc.PrivateKeys,
		Config:      sc.Config,
	}
	buf, err := network.MarshalRegisteredType(scf)
	if err != nil {
		log.Fatal(err)
	}
	err = ioutil.WriteFile(dir+"/"+SimulationFileName, buf, 0660)
	if err != nil {
		log.Fatal(err)
	}

	return nil
}
Example #4
0
func init() {
	network.RegisterMessageType(Tree{})
	network.RegisterMessageType(tbmStruct{})
}
Example #5
0
	// All children from this tree. The top-node only has one child, which is
	// the root
	Children []*TreeMarshal
}

func (tm *TreeMarshal) String() string {
	s := fmt.Sprintf("%v", tm.ServerIdentityID)
	s += "\n"
	for i := range tm.Children {
		s += tm.Children[i].String()
	}
	return s
}

// TreeMarshalTypeID of TreeMarshal message as registered in network
var TreeMarshalTypeID = network.RegisterMessageType(TreeMarshal{})

// TreeMarshalCopyTree takes a TreeNode and returns a corresponding
// TreeMarshal
func TreeMarshalCopyTree(tr *TreeNode) *TreeMarshal {
	tm := &TreeMarshal{
		TreeNodeID:       tr.ID,
		ServerIdentityID: tr.ServerIdentity.ID,
	}
	for i := range tr.Children {
		tm.Children = append(tm.Children,
			TreeMarshalCopyTree(tr.Children[i]))
	}
	return tm
}
Example #6
0
func init() {
	network.RegisterMessageType(Announce{})
	network.RegisterMessageType(Reply{})
	sda.ProtocolRegisterName("ExampleChannels", NewExampleChannels)
}
Example #7
0
func init() {
	dummyMsgType = network.RegisterMessageType(DummyMsg{})
}
Example #8
0
		}(i)
	}
	wg.Wait()
}

// BackForthProtocolForth & Back are messages that go down and up the tree.
// => BackForthProtocol protocol / message
type SimpleMessageForth struct {
	Val int
}

type SimpleMessageBack struct {
	Val int
}

var simpleMessageForthType = network.RegisterMessageType(SimpleMessageForth{})
var simpleMessageBackType = network.RegisterMessageType(SimpleMessageBack{})

type BackForthProtocol struct {
	*sda.TreeNodeInstance
	Val       int
	counter   int
	forthChan chan struct {
		*sda.TreeNode
		SimpleMessageForth
	}
	backChan chan struct {
		*sda.TreeNode
		SimpleMessageBack
	}
	handler func(val int)
Example #9
0
func init() {
	network.RegisterMessageType(&StatusRet{})
}
Example #10
0
// NewServiceFunc is the type of a function that is used to instantiate a given Service
// A service is initialized with a Host (to send messages to someone), the
// overlay (to register a Tree + Roster + start new node), and a path where
// it can finds / write everything it needs
type NewServiceFunc func(c *Context, path string) Service

// GenericConfig is a config that can withhold any type of specific configs for
// protocols. It is passed down to the service NewProtocol function.
type GenericConfig struct {
	Type uuid.UUID
	//Data network.ProtocolMessage
}

// GenericConfigID is the ID used by the network library for sending / receiving
// GenericCOnfig
var GenericConfigID = network.RegisterMessageType(GenericConfig{})

// A serviceFactory is used to register a NewServiceFunc
type serviceFactory struct {
	constructors map[ServiceID]NewServiceFunc
	// translations between name of a Service and its ServiceID. Used to register a
	// Service using a name.
	translations map[string]ServiceID
	// Inverse mapping of ServiceId => string
	inverseTr map[ServiceID]string
}

// ServiceFactory is the global service factory to instantiate Services
var ServiceFactory = serviceFactory{
	constructors: make(map[ServiceID]NewServiceFunc),
	translations: make(map[string]ServiceID),
Example #11
0
package sda

import (
	"sync"

	"github.com/dedis/cothority/network"
	"github.com/satori/go.uuid"
)

// SDADataMessageID is to be embedded in every message that is made for a
// ID of SDAData message as registered in network
var SDADataMessageID = network.RegisterMessageType(ProtocolMsg{})

// RequestTreeMessageID of RequestTree message as registered in network
var RequestTreeMessageID = network.RegisterMessageType(RequestTree{})

// RequestRosterMessageID of RequestRoster message as registered in network
var RequestRosterMessageID = network.RegisterMessageType(RequestRoster{})

// SendTreeMessageID of TreeMarshal message as registered in network
var SendTreeMessageID = TreeMarshalTypeID

// SendRosterMessageID of Roster message as registered in network
var SendRosterMessageID = RosterTypeID

// ProtocolMsg is to be embedded in every message that is made for a
// ProtocolInstance
type ProtocolMsg struct {
	// Token uniquely identify the protocol instance this msg is made for
	From *Token
	// The TreeNodeId Where the message goes to
Example #12
0
func init() {
	network.RegisterMessageType(PropagateMsg{})
}
Example #13
0
func init() {
	network.RegisterMessageType(PrepareClose{})
	network.RegisterMessageType(Close{})
	sda.ProtocolRegisterName("CloseAll", NewCloseAll)
}
Example #14
0
// LoadSimulationConfig gets all configuration from dir + SimulationFileName and instantiates the
// corresponding host 'ha'.
func LoadSimulationConfig(dir, ha string) ([]*SimulationConfig, error) {
	network.RegisterMessageType(SimulationConfigFile{})
	bin, err := ioutil.ReadFile(dir + "/" + SimulationFileName)
	if err != nil {
		return nil, err
	}
	_, msg, err := network.UnmarshalRegisteredType(bin,
		network.DefaultConstructors(network.Suite))
	if err != nil {
		return nil, err
	}
	scf := msg.(SimulationConfigFile)
	sc := &SimulationConfig{
		Roster:      scf.Roster,
		PrivateKeys: scf.PrivateKeys,
		Config:      scf.Config,
	}
	sc.Tree, err = scf.TreeMarshal.MakeTree(sc.Roster)
	if err != nil {
		return nil, err
	}

	var ret []*SimulationConfig
	if ha != "" {
		if !strings.Contains(ha, ":") {
			// to correctly match hosts a column is needed, else
			// 10.255.0.1 would also match 10.255.0.10 and others
			ha += ":"
		}
		for _, e := range sc.Roster.List {
			for _, a := range e.Addresses {
				log.Lvl4("Searching for", ha, "in", a)
				// If we are started in Deterlab- or other
				// big-server-needs-multiple-hosts, we might
				// want to initialise all hosts in one instance
				// of 'cothority' so as to minimize memory
				// footprint
				if strings.Contains(a, ha) {
					log.Lvl3("Found host", a, "to match", ha)
					host := NewHost(e, scf.PrivateKeys[a])
					scNew := *sc
					scNew.Host = host
					scNew.Overlay = host.overlay
					ret = append(ret, &scNew)
				}

			}
		}
		if len(ret) == 0 {
			return nil, errors.New("Didn't find address: " + ha)
		}
	} else {
		ret = append(ret, sc)
	}
	if strings.Contains(sc.Roster.List[0].Addresses[0], "localhost") {
		// Now strip all superfluous numbers of localhost
		for i := range sc.Roster.List {
			_, port, _ := net.SplitHostPort(sc.Roster.List[i].Addresses[0])
			sc.Roster.List[i].Addresses[0] = "localhost:" + port
		}
	}
	return ret, nil
}
Example #15
0
func init() {
	// register network messages and protocol
	network.RegisterMessageType(Message{})
	network.RegisterMessageType(SignatureReply{})
	sda.ProtocolRegisterName("NaiveTree", NewProtocol)
}
Example #16
0
	}
	return hosts[0], hosts[1]
}

// Test instantiation of ProtocolInstances

// Test access of actual peer that received the message
// - corner-case: accessing parent/children with multiple instances of the same peer
// in the graph - ProtocolID + GraphID + InstanceID is not enough
// XXX ???

// Test complete parsing of new incoming packet
// - Test if it is SDAMessage
// - reject if unknown ProtocolID
// - setting up of graph and Hostlist
// - instantiating ProtocolInstance

// SimpleMessage is just used to transfer one integer
type SimpleMessage struct {
	I int
}

var SimpleMessageType = network.RegisterMessageType(SimpleMessage{})

func testMessageSimple(t *testing.T, msg network.Packet) SimpleMessage {
	if msg.MsgType != SimpleMessageType {
		t.Fatal("Received non SimpleMessage type")
	}
	return msg.Msg.(SimpleMessage)
}