2022-10-18 14:41:01 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"git.hpds.cc/Component/network/log"
|
|
|
|
"git.hpds.cc/pavement/hpds_node"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ImageDataKey = 0x10
|
|
|
|
|
|
|
|
var (
|
|
|
|
counter uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
sf := hpds_node.NewStreamFunction(
|
|
|
|
"echo-sf",
|
2022-10-23 13:33:16 +08:00
|
|
|
hpds_node.WithMqAddr("localhost:27187"),
|
2022-10-18 14:41:01 +08:00
|
|
|
hpds_node.WithObserveDataTags(ImageDataKey),
|
|
|
|
hpds_node.WithCredential("token:z1"),
|
|
|
|
)
|
|
|
|
defer sf.Close()
|
|
|
|
|
|
|
|
sf.SetHandler(Handler)
|
|
|
|
|
|
|
|
err := sf.Connect()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Connect to MQ failure: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler process the data in the stream
|
|
|
|
func Handler(img []byte) (byte, []byte) {
|
|
|
|
// Initialize WasmEdge's VM
|
|
|
|
//vmConf, vm := initVM()
|
|
|
|
//bg := bindgen.Instantiate(vm)
|
|
|
|
//defer bg.Release()
|
|
|
|
//defer vm.Release()
|
|
|
|
//defer vmConf.Release()
|
|
|
|
//
|
|
|
|
//// recognize the image
|
|
|
|
//res, err := bg.Execute("infer", img)
|
|
|
|
//if err == nil {
|
|
|
|
// fmt.Println("GO: Run bindgen -- infer:", string(res[0].([]byte)))
|
|
|
|
//} else {
|
|
|
|
// fmt.Println("GO: Run bindgen -- infer FAILED")
|
|
|
|
//}
|
2022-10-23 13:33:16 +08:00
|
|
|
id := atomic.AddUint64(&counter, 1)
|
2022-10-18 14:41:01 +08:00
|
|
|
str := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
|
|
|
|
tick := time.Now().Format("20060102150405") + str[10:]
|
2022-10-23 13:33:16 +08:00
|
|
|
go WriteFile(fmt.Sprintf("./%d_%s.jpeg", id, tick), img)
|
2022-10-18 14:41:01 +08:00
|
|
|
|
|
|
|
// print logs
|
|
|
|
hash := genSha1(img)
|
2022-10-23 13:33:16 +08:00
|
|
|
log.Printf("received image-%d hash %v, img_size=%d \n", id, hash, len(img))
|
2022-10-18 14:41:01 +08:00
|
|
|
|
|
|
|
return 0x11, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genSha1 generate the hash value of the image
|
|
|
|
func genSha1(buf []byte) string {
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write(buf)
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteFile(fn string, data []byte) {
|
|
|
|
err := os.WriteFile(fn, data, 777)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("write file error =%v \n", err)
|
|
|
|
}
|
|
|
|
}
|