Example #1
0
func (s *simpleService) ProcessClientRequest(e *network.ServerIdentity, r *sda.ClientRequest) {
	msgT, pm, err := network.UnmarshalRegisteredType(r.Data, network.DefaultConstructors(network.Suite))
	log.ErrFatal(err)
	if msgT != simpleRequestType {
		return
	}
	req := pm.(simpleRequest)
	tree := req.ServerIdentities.GenerateBinaryTree()
	tni := s.ctx.NewTreeNodeInstance(tree, tree.Root, "BackForth")
	proto, err := newBackForthProtocolRoot(tni, req.Val, func(n int) {
		if err := s.ctx.SendRaw(e, &simpleResponse{
			Val: n,
		}); err != nil {
			log.Error(err)
		}
	})
	if err != nil {
		log.Error(err)
		return
	}
	if err := s.ctx.RegisterProtocolInstance(proto); err != nil {
		log.Error(err)
	}
	go proto.Start()
}
Example #2
0
// NewTreeFromMarshal takes a slice of bytes and an Roster to re-create
// the original tree
func NewTreeFromMarshal(buf []byte, el *Roster) (*Tree, error) {
	tp, pm, err := network.UnmarshalRegisteredType(buf,
		network.DefaultConstructors(network.Suite))
	if err != nil {
		return nil, err
	}
	if tp != TreeMarshalTypeID {
		return nil, errors.New("Didn't receive TreeMarshal-struct")
	}
	t, err := pm.(TreeMarshal).MakeTree(el)
	t.computeSubtreeAggregate(network.Suite, t.Root)
	return t, err
}
Example #3
0
// BinaryUnmarshaler takes a TreeMarshal and stores it in the tree
func (t *Tree) BinaryUnmarshaler(b []byte) error {
	_, m, err := network.UnmarshalRegisteredType(b, network.DefaultConstructors(network.Suite))
	tbm, ok := m.(tbmStruct)
	if !ok {
		return errors.New("Didn't find TBMstruct")
	}
	tree, err := NewTreeFromMarshal(tbm.T, tbm.EL)
	if err != nil {
		return err
	}
	t.Roster = tbm.EL
	t.ID = tree.ID
	t.Root = tree.Root
	return nil
}
Example #4
0
func (ds *DummyService) ProcessServiceMessage(e *network.ServerIdentity, s *sda.InterServiceMessage) {
	id, m, err := network.UnmarshalRegisteredType(s.Data, network.DefaultConstructors(network.Suite))
	if err != nil {
		ds.link <- false
		return
	}
	if id != dummyMsgType {
		ds.link <- false
		return
	}
	dms := m.(DummyMsg)
	if dms.A != 10 {
		ds.link <- false
		return
	}
	ds.link <- true
}
Example #5
0
func (ds *DummyService) ProcessClientRequest(e *network.ServerIdentity, r *sda.ClientRequest) {
	msgT, _, err := network.UnmarshalRegisteredType(r.Data, network.DefaultConstructors(network.Suite))
	if err != nil || msgT != dummyMsgType {
		ds.link <- false
		return
	}
	if ds.firstTni == nil {
		ds.firstTni = ds.c.NewTreeNodeInstance(ds.fakeTree, ds.fakeTree.Root, "DummyService")
	}

	dp := NewDummyProtocol(ds.firstTni, ds.Config, ds.link)

	if err := ds.c.RegisterProtocolInstance(dp); err != nil {
		ds.link <- false
		return
	}
	dp.Start()
}
Example #6
0
// dispatchMsgToProtocol will dispatch this sda.Data to the right instance
func (n *TreeNodeInstance) dispatchMsgToProtocol(sdaMsg *ProtocolMsg) error {
	// Decode the inner message here. In older versions, it was decoded before,
	// but first there is no use to do it before, and then every protocols had
	// to manually registers their messages. Since it is done automatically by
	// the Node, decoding should also be done by the node.
	var err error
	t, msg, err := network.UnmarshalRegisteredType(sdaMsg.MsgSlice, network.DefaultConstructors(n.Suite()))
	if err != nil {
		log.Error(n.ServerIdentity().First(), "Error while unmarshalling inner message of SDAData", sdaMsg.MsgType, ":", err)
	}
	// Put the msg into SDAData
	sdaMsg.MsgType = t
	sdaMsg.Msg = msg

	// if message comes from parent, dispatch directly
	// if messages come from children we must aggregate them
	// if we still need to wait for additional messages, we return
	msgType, msgs, done := n.aggregate(sdaMsg)
	if !done {
		log.Lvl3(n.Name(), "Not done aggregating children msgs")
		return nil
	}
	log.Lvlf5("TNI dispatching -Message is: %+v", sdaMsg.Msg)

	switch {
	case n.channels[msgType] != nil:
		log.Lvl4(n.Info(), "Dispatching to channel")
		err = n.DispatchChannel(msgs)
	case n.handlers[msgType] != nil:
		log.Lvl4("Dispatching to handler", n.ServerIdentity().Addresses)
		err = n.dispatchHandler(msgs)
	default:
		return errors.New("This message-type is not handled by this protocol")
	}
	return err
}
Example #7
0
// GetReply takes a clientRequest and passes it to the corresponding
// handler-function.
func (p *ServiceProcessor) GetReply(e *network.ServerIdentity, d []byte) network.Body {
	mt, m, err := network.UnmarshalRegisteredType(d,
		network.DefaultConstructors(network.Suite))
	fu, ok := p.functions[mt]
	if !ok {
		return &StatusRet{"Didn't register message-handler: " + mt.String()}
	}

	if err != nil {
		return &StatusRet{err.Error()}
	}

	//to0 := reflect.TypeOf(fu).In(0)
	to1 := reflect.TypeOf(fu).In(1)
	f := reflect.ValueOf(fu)

	log.Lvl4("Dispatching to", e.Addresses)
	arg0 := reflect.New(reflect.TypeOf(network.ServerIdentity{}))
	arg0.Elem().Set(reflect.ValueOf(e).Elem())
	arg1 := reflect.New(to1.Elem())
	arg1.Elem().Set(reflect.ValueOf(m))

	ret := f.Call([]reflect.Value{arg0, arg1})

	errI := ret[1].Interface()

	if errI != nil {
		return &StatusRet{errI.(error).Error()}
	}

	reply := ret[0].Interface()
	if reply == nil {
		reply = StatusOK
	}
	return reply
}
Example #8
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
}