forked from mmussett/ems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
400 lines (319 loc) · 9.18 KB
/
client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package ems
/*
#cgo darwin CFLAGS: -I.
#cgo darwin CFLAGS: -I/opt/tibco/ems/ems830/ems/8.3/include/tibems
#cgo darwin LDFLAGS: -L/opt/tibco/ems/ems830/ems/8.3/lib -ltibems64
#include <tibems.h>
tibemsDestination castToDestination(tibemsTemporaryQueue queue) {
return (tibemsDestination)queue;
}
tibems_bool castToBool(int value) {
return (tibems_bool)value;
}
tibems_long castToLong(int value) {
return (tibems_long)value;
}
tibems_int castToInt(int value) {
return (tibems_int)value;
}
*/
import "C"
import (
"errors"
"strings"
"sync"
"sync/atomic"
"unsafe"
"fmt"
)
type Client interface {
IsConnected() bool
Connect() error
Disconnect() error
Send(destination string, message string, deliveryDelay int, deliveryMode string, expiration int) error
SendReceive(destination string, message string, deliveryMode string, expiration int) (string, error)
}
type client struct {
conn C.tibemsConnection
cf C.tibemsConnectionFactory
errorContext C.tibemsErrorContext
status uint32
options ClientOptions
sync.RWMutex
}
func NewClient(o *ClientOptions) Client {
c := &client{}
c.options = *o
c.status = disconnected
return c
}
func (c *client) IsConnected() bool {
c.RLock()
defer c.RUnlock()
return c.status == connected
}
func (c *client) Connect() error {
c.RLock()
defer c.RUnlock()
status := C.tibemsErrorContext_Create(&c.errorContext)
if status != TIBEMS_OK {
return errors.New("failed to create error context")
}
c.cf = C.tibemsConnectionFactory_Create()
url := c.options.GetServerUrl()
status = C.tibemsConnectionFactory_SetServerURL(c.cf, C.CString(url.String()))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// create the connection
status = C.tibemsConnectionFactory_CreateConnection(c.cf, &c.conn, C.CString(c.options.username), C.CString(c.options.password))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// start the connection
status = C.tibemsConnection_Start(c.conn)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
c.setConnected(connected)
return nil
}
func (c *client) Disconnect() error {
c.RLock()
defer c.RUnlock()
if c.IsConnected() {
status := C.tibemsConnection_Stop(c.conn)
if status != TIBEMS_OK {
return errors.New("failed to stop connection")
}
// close the connection
status = C.tibemsConnection_Close(c.conn)
if status != TIBEMS_OK {
return errors.New("failed to close connection")
}
c.setConnected(disconnected)
}
return nil
}
func (c *client) SendReceive(destination string, message string, deliveryMode string, expiration int) (string, error) {
var dest C.tibemsDestination
var session C.tibemsSession
var requestor C.tibemsMsgRequestor
var reqMsg C.tibemsMsg
var repMsg C.tibemsMsg
var msg C.tibemsTextMsg
// create the destination
status := C.tibemsDestination_Create(&dest, TIBEMS_QUEUE, C.CString(destination))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// create the session
status = C.tibemsConnection_CreateSession(c.conn, &session, TIBEMS_FALSE, TIBEMS_AUTO_ACKNOWLEDGE)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// create the requestor
status = C.tibemsMsgRequestor_Create(session, &requestor, dest)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// create the request message
status = C.tibemsMsg_Create(&reqMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// create the message
status = C.tibemsTextMsg_Create(&msg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// set message delivery mode
var emsDeliveryMode = TIBEMS_NON_PERSISTENT
if strings.ToLower(deliveryMode) == "persistent" {
emsDeliveryMode = TIBEMS_PERSISTENT
} else if strings.ToLower(deliveryMode) == "non_persistent" {
emsDeliveryMode = TIBEMS_NON_PERSISTENT
} else if strings.ToLower(deliveryMode) == "reliable" {
emsDeliveryMode = TIBEMS_RELIABLE
}
status = C.tibemsMsg_SetDeliveryMode(msg, C.tibemsDeliveryMode(emsDeliveryMode))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// set message expiration
status = C.tibemsMsg_SetExpiration(msg, C.castToLong(C.int(expiration)))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// create the reply message
status = C.tibemsMsg_Create(&repMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// set the message text
status = C.tibemsTextMsg_SetText(msg, C.CString(message))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// send a request message; wait for a reply
status = C.tibemsMsgRequestor_Request(requestor, msg, &repMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// Get the string data from the reply text message
var buf *C.char
buf = (*C.char)(C.calloc(32768, 1))
defer C.free(unsafe.Pointer(buf))
status = C.tibemsTextMsg_GetText(repMsg, &buf)
replyMessageText := C.GoString(buf)
fmt.Println("Received JMS Reply Text Message = "+replyMessageText)
// destroy the request message
status = C.tibemsMsg_Destroy(reqMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// destroy the requestor
status = C.tibemsMsgRequestor_Close(requestor)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// destroy the session
status = C.tibemsSession_Close(session)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
// destroy the destination
status = C.tibemsDestination_Destroy(dest)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", errors.New(e)
}
return replyMessageText, nil
}
func (c *client) Send(destination string, message string, deliveryDelay int, deliveryMode string, expiration int) error {
var dest C.tibemsDestination
var session C.tibemsSession
var msgProducer C.tibemsMsgProducer
var txtMsg C.tibemsTextMsg
// create the destination
status := C.tibemsDestination_Create(&dest, TIBEMS_QUEUE, C.CString(destination))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// create the session
status = C.tibemsConnection_CreateSession(c.conn, &session, TIBEMS_FALSE, TIBEMS_AUTO_ACKNOWLEDGE)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// create the producer
status = C.tibemsSession_CreateProducer(session, &msgProducer, dest)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
status = C.tibemsMsgProducer_SetDeliveryDelay(msgProducer, C.castToLong(C.int(deliveryDelay)))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
var emsDeliveryMode = TIBEMS_NON_PERSISTENT
if strings.ToLower(deliveryMode) == "persistent" {
emsDeliveryMode = TIBEMS_PERSISTENT
} else if strings.ToLower(deliveryMode) == "non_persistent" {
emsDeliveryMode = TIBEMS_NON_PERSISTENT
} else if strings.ToLower(deliveryMode) == "reliable" {
emsDeliveryMode = TIBEMS_RELIABLE
}
status = C.tibemsMsgProducer_SetDeliveryMode(msgProducer, C.castToInt(C.int(emsDeliveryMode)))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
status = C.tibemsMsgProducer_SetTimeToLive(msgProducer, C.castToLong(C.int(expiration)))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// create the message
status = C.tibemsTextMsg_Create(&txtMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// set the message text
status = C.tibemsTextMsg_SetText(txtMsg, C.CString(message))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// publish the message
status = C.tibemsMsgProducer_Send(msgProducer, txtMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// destroy the message
status = C.tibemsMsg_Destroy(txtMsg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// destroy the producer
status = C.tibemsMsgProducer_Close(msgProducer)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// destroy the session
status = C.tibemsSession_Close(session)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
// destroy the destination
status = C.tibemsDestination_Destroy(dest)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return errors.New(e)
}
return nil
}
func (c *client) connectionStatus() uint32 {
c.RLock()
defer c.RUnlock()
status := atomic.LoadUint32(&c.status)
return status
}
func (c *client) setConnected(status uint32) {
c.RLock()
defer c.RUnlock()
atomic.StoreUint32(&c.status, status)
}
func (c *client) getErrorContext() (string, string) {
var errorString, stackTrace = "", ""
var buf *C.char
defer C.free(unsafe.Pointer(buf))
C.tibemsErrorContext_GetLastErrorString(c.errorContext, &buf)
errorString = C.GoString(buf)
C.tibemsErrorContext_GetLastErrorStackTrace(c.errorContext, &buf)
stackTrace = C.GoString(buf)
return errorString, stackTrace
}