Example #1
0
func (ns *NodeState) do_AMB_REGISTER(sok io.ReadWriteCloser, req textprot.TextReq) (err error) {
	if len(req.Args) == 0 {
		err = errors.New("core2core: server got truncated AMB_REGISTER")
		return
	}
	bts, err := hex.DecodeString(req.Args[0])
	if err != nil {
		return err
	}

	var kee [32]byte
	copy(kee[:], bts)
	fmt.Printf("%x\n", kee[:])

	// unlock once the kiricom state is in place
	ns.Lock()
	_, ok := ns.ambState[kee]
	if ok {
		ns.Unlock()
		err = (&textprot.TextReq{
			Verb: "NOPE",
		}).WriteTo(sok)
		return
	}

	dieInDisgrace := func() {
		ns.Lock()
		delete(ns.ambState, kee)
		ns.Unlock()
	}

	defer kilog.Debug("core2core: ambassador register for %x terminated", kee[:])

	// signal OKAY
	err = (&textprot.TextReq{
		Verb: "OKAY",
	}).WriteTo(sok)
	if err != nil {
		kilog.Debug("core2core: WHAT %v", err.Error())
		dieInDisgrace()
		return
	}

	// construct the client kiricom state
	kcstat := kiricom.NewClientCtx(8192, sok)
	ns.ambState[kee] = kcstat
	ns.Unlock()

	defer dieInDisgrace()
	defer kcstat.Close()
	kcstat.WaitDeath()
	return
}
Example #2
0
// ConnAmbassador connects through this node as an ambassador
func (clnt *Client) ConnAmbassador(srvpub natrium.EdDSAPublic) (*kiricom.ClientCtx, error) {
	req := textprot.TextReq{
		Verb: "AMB_CONNECT",
		Args: []string{hex.EncodeToString(srvpub)},
	}
	_, err := clnt.execCmd(req)
	if err != nil {
		return nil, err
	}

	clntctx := kiricom.NewClientCtx(8192, clnt.sok)
	return clntctx, nil
}