| | |
| | | package service |
| | | |
| | | import ( |
| | | "basic.com/pubsub/protomsg.git" |
| | | "basic.com/valib/deliver.git" |
| | | "github.com/gin-gonic/gin/internal/json" |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/gogo/protobuf/proto" |
| | | "github.com/satori/go.uuid" |
| | | "io/ioutil" |
| | | "time" |
| | | ) |
| | | |
| | | type ImageSource struct { |
| | | Id string `json:"id"` |
| | | Data []byte `json:"data"` |
| | | } |
| | | const ( |
| | | Url_Service_PUSH = "tcp:///tmp///webserver-2.ipc" |
| | | Url_Service_PULL = "tcp:///tmp///webserver-1.ipc" |
| | | Url_Service_PUSH = "tcp:///tmp///webserver_2.ipc" |
| | | Url_Service_PULL = "tcp:///tmp///webserver_1.ipc" |
| | | ) |
| | | var imgChan chan ImageSource |
| | | func PushImgMsg(is ImageSource){ |
| | | imgChan <- is |
| | | var imgPushChan chan protomsg.Recvmsg |
| | | |
| | | func TestPushImgMsg() { |
| | | InitService() |
| | | imgData := readImgFile() |
| | | fmt.Println("imgData.len:",len(imgData)) |
| | | for { |
| | | PushImgMsg(protomsg.Recvmsg{ |
| | | Id:uuid.NewV4().String(), |
| | | Addr:"", |
| | | Picdata:imgData, |
| | | }) |
| | | fmt.Println("pushed img") |
| | | time.Sleep(5*time.Second) |
| | | } |
| | | } |
| | | |
| | | func readImgFile() []byte{ |
| | | filePath := "/home/user/workspace/timg.jpg" |
| | | bytes, err := ioutil.ReadFile(filePath) |
| | | if err !=nil { |
| | | fmt.Println("Read img err:",err) |
| | | } |
| | | return bytes |
| | | } |
| | | |
| | | func PushImgMsg(is protomsg.Recvmsg){ |
| | | imgPushChan <- is |
| | | } |
| | | |
| | | var resultMap map[string]protomsg.SdkMessage |
| | | |
| | | |
| | | func InitService(){ |
| | | imgChan = make(chan ImageSource) |
| | | client := deliver.NewClient(deliver.PushPull, Url_Service_PUSH) |
| | | for { |
| | | select { |
| | | case is := <- imgChan: |
| | | b, _ := json.Marshal(is) |
| | | client.Send(b) |
| | | fmt.Println("service init!") |
| | | imgPushChan = make(chan protomsg.Recvmsg) |
| | | client_push := deliver.NewClient(deliver.PushPull, Url_Service_PUSH) |
| | | client_pull := deliver.NewClient(deliver.PushPull, Url_Service_PULL) |
| | | |
| | | go func() { |
| | | for { |
| | | select { |
| | | case is := <- imgPushChan: |
| | | fmt.Println("imgPushChan in") |
| | | b, _ := json.Marshal(is) |
| | | client_push.Send(b) |
| | | default: |
| | | //fmt.Println("no img in") |
| | | } |
| | | } |
| | | }() |
| | | |
| | | go func() { |
| | | //接收人脸提取结果 |
| | | for { |
| | | resultBytes, err := client_pull.Recv() |
| | | if err !=nil{ |
| | | fmt.Println("pull err:",err) |
| | | continue |
| | | } |
| | | rMsg := protomsg.SdkMessage{} |
| | | if err := proto.Unmarshal(resultBytes, &rMsg);err !=nil{ |
| | | fmt.Println("recv MSG:",rMsg) |
| | | resultMap[rMsg.Cid] = rMsg |
| | | } |
| | | |
| | | } |
| | | } |
| | | }() |
| | | |
| | | } |