From 9cb20b2be0918cd12b9377d190c0ee49cbadf04c Mon Sep 17 00:00:00 2001 From: liuxiaolong <736321739@qq.com> Date: 星期四, 27 六月 2019 11:48:47 +0800 Subject: [PATCH] push once --- service/FaceSdkService.go | 142 ++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 122 insertions(+), 20 deletions(-) diff --git a/service/FaceSdkService.go b/service/FaceSdkService.go index d1bd50f..a15485d 100644 --- a/service/FaceSdkService.go +++ b/service/FaceSdkService.go @@ -1,34 +1,136 @@ package service import ( + "basic.com/pubsub/protomsg.git" "basic.com/valib/deliver.git" - "github.com/gin-gonic/gin/internal/json" + "fmt" + "github.com/gogo/protobuf/proto" + "github.com/pierrec/lz4" + "github.com/satori/go.uuid" + "image" + "io/ioutil" + "os" + "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 = "ipc:///tmp///virtual-faceextract-sdk-pull_2.ipc" + Url_Service_PULL = "ipc:///tmp///virtual-faceextract-sdk-pull_1.ipc" ) -var imgChan chan ImageSource -func PushImgMsg(is ImageSource){ - imgChan <- is -} +var imgPushChan chan protomsg.Recvmsg +var client_push deliver.Deliver +var client_pull deliver.Deliver -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) +func TestPushImgMsg() { + InitService() + proImg := readImgFile() + + if b, err := proto.Marshal(&proImg);err !=nil{ + fmt.Println("protoImage marshal err") + return + } else { + bc := make([]byte,len(b)) + ht := make([]int, 64<<10) + n,err := lz4.CompressBlock(b,bc,ht) + if err !=nil { + fmt.Println(err) } - + if n >= len(b){ + fmt.Println("image is not compressible") + } + bc = bc[:n] + //for { + PushImgMsg(protomsg.Recvmsg{ + Id:uuid.NewV4().String(), + Addr:"", + Picdata:bc, + }) + fmt.Println("pushed img") + //time.Sleep(10*time.Second) + //} } +} +func readImgFile() protomsg.Image{ + var i protomsg.Image + timeUnix := time.Now().Unix() + formatTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05") + filePath := "/home/user/workspace/timg.jpg" + + file, err := os.Open(filePath) + defer file.Close() + if err !=nil{ + fmt.Println("image not exist") + return i + } else { + img, _, err := image.Decode(file) + bytes, err := ioutil.ReadFile(filePath) + if err !=nil { + return i + } + b := img.Bounds() + width := b.Max.X + height := b.Max.Y + i = protomsg.Image{ + Width:int32(width), + Height:int32(height), + Timestamp:formatTimeStr, + Data:bytes, + } + return i + } +} + +func PushImgMsg(is protomsg.Recvmsg){ + imgPushChan <- is +} + +var resultMap map[string]protomsg.SdkMessage + + +func InitService(){ + 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) + defer func() { + client_push.Close() + client_pull.Close() + }() + go thSend() + + go thRecv() +} + +func thSend(){ + for { + select { + case is := <- imgPushChan: + fmt.Println("imgPushChan in") + b, _ := proto.Marshal(&is) + err := client_push.Send(b) + if err !=nil { + fmt.Println("img Send err:",err) + } + default: + //fmt.Println("no img in") + } + } +} + +func thRecv(){ + 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 + } + + } } \ No newline at end of file -- Gitblit v1.8.0