package iroh
import (
"context"
"iter"
"time"
"github.com/tmc/go-iroh/dns"
"github.com/tmc/go-iroh/key"
"github.com/tmc/go-iroh/netaddr"
)
const StaticProvenance = "static_lookup"
type StaticLookup struct {
endpoints map [key .EndpointID ]staticInfo
provenance string
lastUpdated uint64
}
type staticInfo struct {
data dns .EndpointData
}
func NewStaticLookup (infos ...dns .EndpointInfo ) *StaticLookup {
return NewStaticLookupWithProvenance (StaticProvenance , infos ...)
}
func NewStaticLookupWithProvenance (provenance string , infos ...dns .EndpointInfo ) *StaticLookup {
s := &StaticLookup {
endpoints : make (map [key .EndpointID ]staticInfo , len (infos )),
provenance : provenance ,
lastUpdated : uint64 (time .Now ().UnixMicro ()),
}
for _ , info := range infos {
s .endpoints [info .ID ] = staticInfo {data : cloneEndpointData (info .Data )}
}
return s
}
func StaticLookupFromAddrs (addrs ...netaddr .EndpointAddr ) *StaticLookup {
infos := make ([]dns .EndpointInfo , 0 , len (addrs ))
for _ , addr := range addrs {
infos = append (infos , dns .EndpointInfoFromAddr (addr ))
}
return NewStaticLookup (infos ...)
}
func (s *StaticLookup ) Resolve (ctx context .Context , id key .EndpointID ) iter .Seq2 [Item , error ] {
if s == nil {
return nil
}
info , ok := s .endpoints [id ]
if !ok {
return nil
}
item := NewItem (dns .EndpointInfo {ID : id , Data : cloneEndpointData (info .data )}, s .provenance , &s .lastUpdated )
return func (yield func (Item , error ) bool ) {
if ctx .Err () == nil {
yield (item , nil )
}
}
}
func cloneEndpointData(data dns .EndpointData ) dns .EndpointData {
out := dns .NewEndpointData (data .Addrs ()...)
if userData := data .UserData (); userData != nil {
u := *userData
out .SetUserData (&u )
}
return out
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .