Files
indra/pkg/indra/client.go
2023-01-28 11:17:42 +00:00

101 lines
2.0 KiB
Go

package indra
import (
"sync"
"time"
"github.com/cybriq/qu"
"go.uber.org/atomic"
"github.com/indra-labs/indra"
"github.com/indra-labs/indra/pkg/crypto/key/prv"
"github.com/indra-labs/indra/pkg/crypto/key/pub"
"github.com/indra-labs/indra/pkg/crypto/key/signer"
"github.com/indra-labs/indra/pkg/crypto/nonce"
"github.com/indra-labs/indra/pkg/node"
"github.com/indra-labs/indra/pkg/onion/layers/confirm"
log2 "github.com/indra-labs/indra/pkg/proc/log"
"github.com/indra-labs/indra/pkg/traffic"
"github.com/indra-labs/indra/pkg/types"
)
var (
log = log2.GetLogger(indra.PathBase)
check = log.E.Chk
)
type Engine struct {
*node.Node
node.Nodes
sync.Mutex
Load byte
*confirm.Confirms
PendingResponses
*signer.KeySet
ShuttingDown atomic.Bool
qu.C
}
func NewClient(tpt types.Transport, hdrPrv *prv.Key, no *node.Node,
nodes node.Nodes) (c *Engine, e error) {
no.Transport = tpt
no.IdentityPrv = hdrPrv
no.IdentityPub = pub.Derive(hdrPrv)
var ks *signer.KeySet
if _, ks, e = signer.New(); check(e) {
return
}
// Add our first return session.
no.AddSession(traffic.NewSession(nonce.NewID(), no.Peer, 0, nil, nil, 5))
c = &Engine{
Confirms: confirm.NewConfirms(),
Node: no,
Nodes: nodes,
KeySet: ks,
C: qu.T(),
}
return
}
// Start a single thread of the Engine.
func (en *Engine) Start() {
for {
if en.handler() {
break
}
}
}
func (en *Engine) RegisterConfirmation(hook confirm.Hook,
cnf nonce.ID) {
if hook == nil {
return
}
en.Confirms.Add(&confirm.Callback{
ID: cnf,
Time: time.Now(),
Hook: hook,
})
}
// Cleanup closes and flushes any resources the client opened that require sync
// in order to reopen correctly.
func (en *Engine) Cleanup() {
// Do cleanup stuff before shutdown.
}
// Shutdown triggers the shutdown of the client and the Cleanup before
// finishing.
func (en *Engine) Shutdown() {
if en.ShuttingDown.Load() {
return
}
log.T.C(func() string {
return "shutting down client " + en.Node.AddrPort.String()
})
en.ShuttingDown.Store(true)
en.C.Q()
}