Closed ychencode closed 7 years ago
my code is just like following:
func gridServices() (string, mux.Handler) {
return "/ndn/grid/status", mux.HandlerFunc(func(w ndn.Sender, i *ndn.Interest) {
fmt.Printf("- Request Interest Prefix is %v \n", i.Name.String())
w.SendData(&ndn.Data{
Name: i.Name,
Content: []byte("The status is On"),
})
})
}
and in main function:
m.Handle(gridServices())
At the subscriber client. I just ust fetch like:
gridStatus := f.Fetch(face, &ndn.Interest{
Name: ndn.NewName("/ndn/grid/status/"),
}, mux.Assembler, dec, mux.Gzipper)
But it shows that no content returned.
/ndn/grid/status/
in the fetcher code should be /ndn/grid/status
. This is because the trailing /
means that there is an empty component in the end.
I modify it but result seems still not right. I do another experiment. The server client code is as following:
func getConnection() net.Conn {
conn, err := packet.Dial("tcp", ":6363")
checkError(err)
return conn
}
func getFace(conn net.Conn, interests chan *ndn.Interest) ndn.Face {
face := ndn.NewFace(conn, interests)
return face
}
func checkError(err error) {
if err != nil {
log.Fatalln(err)
}
}
func main() {
conn := getConnection()
recv := make(chan *ndn.Interest)
face := getFace(conn, recv)
defer face.Close()
pem, err := os.Open("key/default.pri")
checkError(err)
defer pem.Close()
key, _ := ndn.DecodePrivateKey(pem)
m := mux.New()
m.Use(mux.Encryptor("/producer/encrypt", key.(*ndn.RSAKey)))
m.HandleFunc("/producer/encrypt", func(w ndn.Sender, i *ndn.Interest) {})
fmt.Printf("- Publish /hello service with /hello prefix \n")
m.HandleFunc("/hello", func(sender ndn.Sender, i *ndn.Interest) {
sender.SendData(&ndn.Data{
Name: i.Name,
Content: []byte(time.Now().UTC().String()),
})
})
fmt.Printf("- Publish grid status service with /ndn/grid/status prefix \n")
m.Handle(serveGridStatus())
m.Run(face, recv, key)
}
func serveGridStatus() (string, mux.HandlerFunc) {
return "/ndn/grid/status", mux.HandlerFunc(func(w ndn.Sender, i *ndn.Interest) {
w.SendData(&ndn.Data{
Name: i.Name,
Content: []byte(time.Now().UTC().String()),
})
})
}
and the client code is as following:
func main() {
conn := getConnection()
face := getFace(conn, nil)
defer face.Close()
pem, err := os.Open("key/default.pri")
checkError(err)
defer pem.Close()
key, err := ndn.DecodePrivateKey(pem)
checkError(err)
f := mux.NewFetcher()
dec := mux.Decryptor(key.(*ndn.RSAKey))
spew.Dump(f.Fetch(face, &ndn.Interest{Name: ndn.NewName("/hello")}, dec))
spew.Dump(f.Fetch(face, &ndn.Interest{Name: ndn.NewName("/ndn/grid/status")}, dec))
}
I think is just registered tow service bind name /hello and /ndn/grid/status. I modify the tail "/". But when I run them. They both return ([]uint8) <nil>
. Why?
Thanks for attaching the code. When encryptor encrypts data for multiple consumers, it will generate multiple encrypted AES keys for each consumer, and push them to local cache. Without the cache, the fetcher sees no decryption key, and returns empty []byte, In your example, that will be:
m := mux.New()
m.Use(mux.Encryptor("/producer/encrypt", key.(*ndn.RSAKey)))
m.Use(mux.Cacher) // You will need this
You can use any ndn.Cache
implementation for this.
Thank you for your patience. It had already worked. I have a personal request. Could you give me the email address of you? I just want to development a HEMS system by go-ndn library. I guess I will meet some problems. May I exchange with you through email?
Hi there. I just create a new handlefunc like example. It prefix is /hi and /grid/status. It returns some status content. But at the subscriber. I use fetcher to fetch the prefix. It always return no content. I think my code is the same as the example. I guess if there are some errors in my thought about the usage of go-ndn?