package service
|
|
import (
|
"basic.com/pubsub/protomsg.git"
|
"basic.com/valib/deliver.git"
|
"fmt"
|
"github.com/gogo/protobuf/proto"
|
"github.com/pierrec/lz4"
|
"github.com/satori/go.uuid"
|
"image"
|
"io/ioutil"
|
"os"
|
"time"
|
)
|
|
const (
|
Url_Service_PUSH = "ipc:///tmp///virtual-faceextract-sdk-pull_2.ipc"
|
Url_Service_PULL = "ipc:///tmp///virtual-faceextract-sdk-pull_1.ipc"
|
)
|
var imgPushChan chan protomsg.Recvmsg
|
var client_push deliver.Deliver
|
var client_pull deliver.Deliver
|
|
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
|
}
|
|
}
|
}
|