forked from apache/rocketmq-client-go
/
message.go
145 lines (125 loc) · 4.66 KB
/
message.go
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
137
138
139
140
141
142
143
144
145
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rocketmq
/*
#cgo LDFLAGS: -L/usr/local/lib/ -lrocketmq
#include <rocketmq/CMessage.h>
#include <rocketmq/CMessageExt.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
//Message used for send
type Message struct {
Topic string
Tags string
Keys string
Body string
DelayTimeLevel int
Property map[string]string
cmsg *C.struct_CMessage
}
func (msg *Message) String() string {
return fmt.Sprintf("[Topic: %s, Tags: %s, Keys: %s, Body: %s, DelayTimeLevel: %d, Property: %v]",
msg.Topic, msg.Tags, msg.Keys, msg.Body, msg.DelayTimeLevel, msg.Property)
}
func (msg *Message) GetProperty(key string) string {
ck := C.CString(key)
defer C.free(unsafe.Pointer(ck))
return C.GoString(C.GetOriginMessageProperty(msg.cmsg, ck))
}
func goMsgToC(gomsg *Message) *C.struct_CMessage {
cs := C.CString(gomsg.Topic)
var cmsg = C.CreateMessage(cs)
C.free(unsafe.Pointer(cs))
cs = C.CString(gomsg.Tags)
C.SetMessageTags(cmsg, cs)
C.free(unsafe.Pointer(cs))
cs = C.CString(gomsg.Keys)
C.SetMessageKeys(cmsg, cs)
C.free(unsafe.Pointer(cs))
cs = C.CString(gomsg.Body)
C.SetMessageBody(cmsg, cs)
C.free(unsafe.Pointer(cs))
C.SetDelayTimeLevel(cmsg, C.int(gomsg.DelayTimeLevel))
for k, v := range gomsg.Property {
key := C.CString(k)
value := C.CString(v)
C.SetMessageProperty(cmsg, key, value)
C.free(unsafe.Pointer(key))
C.free(unsafe.Pointer(value))
}
return cmsg
}
func cMsgToGo(cMsg *C.struct_CMessage) *Message {
gomsg := &Message{}
gomsg.Topic = C.GoString(C.GetOriginMessageTopic(cMsg))
gomsg.Tags = C.GoString(C.GetOriginMessageTags(cMsg))
gomsg.Keys = C.GoString(C.GetOriginMessageKeys(cMsg))
gomsg.Body = C.GoString(C.GetOriginMessageBody(cMsg))
gomsg.DelayTimeLevel = int(C.GetOriginDelayTimeLevel(cMsg))
gomsg.cmsg = cMsg
return gomsg
}
//MessageExt used for consume
type MessageExt struct {
Message
MessageID string
QueueId int
ReconsumeTimes int
StoreSize int
BornTimestamp int64
StoreTimestamp int64
QueueOffset int64
CommitLogOffset int64
PreparedTransactionOffset int64
// improve: is there is a method convert c++ map to go variable?
cmsgExt *C.struct_CMessageExt
}
func (msgExt *MessageExt) String() string {
return fmt.Sprintf("[MessageId: %s, %s, QueueId: %d, ReconsumeTimes: %d, StoreSize: %d, BornTimestamp: %d, "+
"StoreTimestamp: %d, QueueOffset: %d, CommitLogOffset: %d, PreparedTransactionOffset: %d]", msgExt.MessageID,
msgExt.Message.String(), msgExt.QueueId, msgExt.ReconsumeTimes, msgExt.StoreSize, msgExt.BornTimestamp,
msgExt.StoreTimestamp, msgExt.QueueOffset, msgExt.CommitLogOffset, msgExt.PreparedTransactionOffset)
}
//GetProperty get the message property by key from message ext
func (msgExt *MessageExt) GetProperty(key string) string {
ck := C.CString(key)
defer C.free(unsafe.Pointer(ck))
return C.GoString(C.GetMessageProperty(msgExt.cmsgExt, ck))
}
func cmsgExtToGo(cmsg *C.struct_CMessageExt) *MessageExt {
gomsg := &MessageExt{cmsgExt: cmsg}
gomsg.Topic = C.GoString(C.GetMessageTopic(cmsg))
gomsg.Tags = C.GoString(C.GetMessageTags(cmsg))
gomsg.Keys = C.GoString(C.GetMessageKeys(cmsg))
gomsg.Body = C.GoString(C.GetMessageBody(cmsg))
gomsg.MessageID = C.GoString(C.GetMessageId(cmsg))
gomsg.DelayTimeLevel = int(C.GetMessageDelayTimeLevel(cmsg))
gomsg.QueueId = int(C.GetMessageQueueId(cmsg))
gomsg.ReconsumeTimes = int(C.GetMessageReconsumeTimes(cmsg))
gomsg.StoreSize = int(C.GetMessageStoreSize(cmsg))
gomsg.BornTimestamp = int64(C.GetMessageBornTimestamp(cmsg))
gomsg.StoreTimestamp = int64(C.GetMessageStoreTimestamp(cmsg))
gomsg.QueueOffset = int64(C.GetMessageQueueOffset(cmsg))
gomsg.CommitLogOffset = int64(C.GetMessageCommitLogOffset(cmsg))
gomsg.PreparedTransactionOffset = int64(C.GetMessagePreparedTransactionOffset(cmsg))
return gomsg
}