Ejemplo n.º 1
1
func (p *Peer) Accept(conn net.Conn) (_err error) {
	defer conn.Close()

	p.logFields(SERVE, log.Fields{
		"remoteAddr": conn.RemoteAddr(),
	}).Debug("accepted connection")
	defer func() {
		if _err != nil {
			p.logErr(SERVE, _err).Error()
		}
	}()

	var failResp string
	if p.readAcquire() {
		defer p.wg.Done()
	} else {
		failResp = "sync not available, currently mutating"
	}

	remoteConfig, err := p.handleConfig(conn, SERVE, failResp)
	if err != nil {
		return errgo.Mask(err)
	}

	if failResp == "" {
		return p.interactWithClient(conn, remoteConfig, cf.NewBitstring(0))
	}
	return nil
}
Ejemplo n.º 2
0
func (t *prefixTree) newChildNode(parent *prefixNode, childIndex int) *prefixNode {
	n := &prefixNode{prefixTree: t, Leaf: true}
	var key *cf.Bitstring
	if parent != nil {
		parentKey := parent.Key()
		key = cf.NewBitstring(parentKey.BitLen() + t.BitQuantum)
		key.SetBytes(parentKey.Bytes())
		for j := 0; j < parent.BitQuantum; j++ {
			if (1<<uint(j))&childIndex == 0 {
				key.Clear(parentKey.BitLen() + j)
			} else {
				key.Set(parentKey.BitLen() + j)
			}
		}
	} else {
		key = cf.NewBitstring(0)
	}
	n.NodeKey = mustEncodeBitstring(key)
	svalues := make([]*cf.Zp, t.NumSamples())
	for i := 0; i < len(svalues); i++ {
		svalues[i] = cf.Zi(cf.P_SKS, 1)
	}
	n.NodeSValues = mustEncodeZZarray(svalues)
	return n
}
Ejemplo n.º 3
0
func (n *prefixNode) Children() ([]recon.PrefixNode, error) {
	if n.IsLeaf() {
		return nil, nil
	}
	key := n.Key()
	numChildren := 1 << uint(n.BitQuantum)
	var result []recon.PrefixNode
	for i := 0; i < numChildren; i++ {
		childKey := cf.NewBitstring(key.BitLen() + n.BitQuantum)
		childKey.SetBytes(key.Bytes())
		for j := 0; j < n.BitQuantum; j++ {
			if (1<<uint(j))&i == 0 {
				childKey.Clear(key.BitLen() + j)
			} else {
				childKey.Set(key.BitLen() + j)
			}
		}
		child, err := n.Node(childKey)
		if err != nil {
			return nil, fmt.Errorf("children failed on child#%v, key=%v: %v", i, childKey, err)
		}
		result = append(result, child)
	}
	return result, nil
}
Ejemplo n.º 4
0
func (n *prefixNode) Parent() (recon.PrefixNode, bool, error) {
	key := n.Key()
	if key.BitLen() == 0 {
		return nil, false, nil
	}
	parentKey := cf.NewBitstring(key.BitLen() - n.BitQuantum)
	parentKey.SetBytes(key.Bytes())
	parent, err := n.Node(parentKey)
	if err != nil {
		return nil, false, fmt.Errorf("failed to get parent: %v", err)
	}
	return parent, true, nil
}
Ejemplo n.º 5
0
func (t *prefixTree) Node(bs *cf.Bitstring) (node recon.PrefixNode, err error) {
	nbq := t.BitQuantum
	key := bs
	nodeKey := mustEncodeBitstring(key)
	for {
		node, err = t.getNode(nodeKey)
		if err != recon.ErrNodeNotFound || key.BitLen() == 0 {
			break
		}
		key = cf.NewBitstring(key.BitLen() - nbq)
		key.SetBytes(bs.Bytes())
		nodeKey = mustEncodeBitstring(key)
	}
	return node, err
}
Ejemplo n.º 6
0
func (n *MemPrefixNode) Key() *cf.Bitstring {
	var keys []int
	for cur := n; cur != nil && cur.parent != nil; cur = cur.parent {
		keys = append([]int{cur.key}, keys...)
	}
	bs := cf.NewBitstring(len(keys) * n.BitQuantum)
	for i := len(keys) - 1; i >= 0; i-- {
		for j := 0; j < n.BitQuantum; j++ {
			if ((keys[i] >> uint(j)) & 0x01) == 1 {
				bs.Set(i*n.BitQuantum + j)
			} else {
				bs.Clear(i*n.BitQuantum + j)
			}
		}
	}
	return bs
}
Ejemplo n.º 7
0
func ReadBitstring(r io.Reader) (*cf.Bitstring, error) {
	nbits, err := ReadLen(r)
	if err != nil {
		return nil, err
	}
	bs := cf.NewBitstring(nbits)
	nbytes, err := ReadLen(r)
	if err != nil {
		return nil, err
	}
	if nbits == 0 {
		return bs, nil
	}
	buf := make([]byte, nbytes)
	_, err = io.ReadFull(r, buf)
	bs.SetBytes(buf)
	return bs, err
}
Ejemplo n.º 8
0
func (t *prefixTree) Root() (recon.PrefixNode, error) {
	return t.Node(cf.NewBitstring(0))
}