liuxiaolong
2019-06-27 31634badea285266bc6bbfd234521dd573fa0e51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
        }
 
    }
}