Ejemplo n.º 1
0
/**
 * Add an endPoint to a node and open an acceptor.  If a compatible
 * overlay does not exist, add the default for the endPoint.
 */
func (n *Node) AddEndPoint(e xt.EndPointI) (ndx int, err error) {
	if e == nil {
		return -1, NilEndPoint
	}
	ndx = -1
	foundOverlay := false
	count := len(n.overlays)
	if count > 0 {
		for j := 0; j < count; j++ {
			overlay := (n.overlays)[j]
			if overlay.IsElement(e) {
				foundOverlay = true
				break
			}
		}
	}
	if !foundOverlay {
		// create a suitable overlay
		var newO xo.OverlayI
		newO, err = xo.DefaultOverlay(e)
		if err != nil {
			return
		}
		// add it to our collection
		n.overlays = append(n.overlays, newO)
	}
	if ndx == -1 {
		n.endPoints = append(n.endPoints, e)
		ndx = len(n.endPoints) - 1
	}
	return
}
Ejemplo n.º 2
0
func (s *XLSuite) TestPeerSerialization(c *C) {
	if VERBOSITY > 0 {
		fmt.Println("TEST_PEER_SERIALIZATION")
	}
	rng := xr.MakeSimpleRNG()

	// this is just a lazy way of building a peer
	name := rng.NextFileName(4)
	nid, err := makeNodeID(rng)
	c.Assert(err, Equals, nil)

	lfs := "tmp/" + hex.EncodeToString(nid.Value())
	node, err := NewNew(name, nid, lfs)
	c.Assert(err, Equals, nil)

	// harvest its keys
	ck := &node.ckPriv.PublicKey
	ckPEM, err := xc.RSAPubKeyToPEM(ck)
	c.Assert(err, Equals, nil)
	sk := &node.skPriv.PublicKey
	skPEM, err := xc.RSAPubKeyToPEM(sk)
	c.Assert(err, Equals, nil)

	// the other bits necessary
	port := 1024 + rng.Intn(1024)
	addr := fmt.Sprintf("1.2.3.4:%d", port)
	ep, err := xt.NewTcpEndPoint(addr)
	c.Assert(err, Equals, nil)
	ctor, err := xt.NewTcpConnector(ep)
	c.Assert(err, Equals, nil)
	overlay, err := xo.DefaultOverlay(ep)
	c.Assert(err, Equals, nil)
	oSlice := []xo.OverlayI{overlay}
	ctorSlice := []xt.ConnectorI{ctor}
	peer, err := NewPeer(name, nid, ck, sk, oSlice, ctorSlice)
	c.Assert(err, Equals, nil)
	c.Assert(peer, Not(Equals), nil)

	// build the expected serialization

	// BaseNode
	var bns []string
	s.addAString(&bns, "peer {")
	s.addAString(&bns, fmt.Sprintf("    name: %s", name))
	s.addAString(&bns, fmt.Sprintf("    nodeID: %s", nid.String()))
	s.addAString(&bns, fmt.Sprintf("    commsPubKey: %s", ckPEM))
	s.addAString(&bns, fmt.Sprintf("    sigPubKey: %s", skPEM))
	s.addAString(&bns, fmt.Sprintf("    overlays {"))
	for i := 0; i < len(oSlice); i++ {
		s.addAString(&bns, fmt.Sprintf("        %s", oSlice[i].String()))
	}
	s.addAString(&bns, fmt.Sprintf("    }"))

	// Specific to Peer
	s.addAString(&bns, fmt.Sprintf("    connectors {"))
	for i := 0; i < len(ctorSlice); i++ {
		s.addAString(&bns, fmt.Sprintf("        %s", ctorSlice[i].String()))
	}
	s.addAString(&bns, fmt.Sprintf("    }")) // closes connectors
	s.addAString(&bns, fmt.Sprintf("}"))     // closes peer
	myVersion := strings.Join(bns, "\n")

	serialized := peer.String()
	c.Assert(serialized, Equals, myVersion)

	backAgain, rest, err := ParsePeer(serialized)
	c.Assert(err, Equals, nil)
	c.Assert(len(rest), Equals, 0)
	reserialized := backAgain.String()
	c.Assert(reserialized, Equals, serialized)

}