-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
546 lines (445 loc) · 12.7 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package ems
/*
#cgo darwin CFLAGS: -I.
#cgo darwin CFLAGS: -I/opt/tibco/ems/ems841/ems/8.4/include/tibems
#cgo darwin LDFLAGS: -L/opt/tibco/ems/ems841/ems/8.4/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"
"fmt"
"strings"
"sync"
"sync/atomic"
"unsafe"
)
type IClient interface {
IsConnected() bool
Connect() error
Disconnect() error
Send(destination string, destinationType string, message string, deliveryDelay int, deliveryMode string, expiration int) error
SendReceive(destination string, destinationType string, message string, deliveryMode string, expiration int) (string, error)
Receive(destination string, destinationType string, timeout int) (string, bool, error)
}
type Client struct {
conn C.tibemsConnection
cf C.tibemsConnectionFactory
errorContext C.tibemsErrorContext
status uint32
options ClientOptions
sync.RWMutex
}
func NewClient(o *ClientOptions) IClient {
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, destinationType 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
var destType C.tibemsDestinationType = TIBEMS_QUEUE
switch strings.ToUpper(destinationType) {
case "QUEUE":
destType = TIBEMS_QUEUE
break
case "TOPIC":
destType = TIBEMS_TOPIC
}
// create the destination
status := C.tibemsDestination_Create(&dest, destType, 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) Receive(destination string, destinationType string, timeout int) (string, bool, error) {
var dest C.tibemsDestination
var session C.tibemsSession
var msgConsumer C.tibemsMsgConsumer
var msg C.tibemsMsg
var msgType C.tibemsMsgType
var msgTypeName string
var destType C.tibemsDestinationType = TIBEMS_QUEUE
switch strings.ToUpper(destinationType) {
case "QUEUE":
destType = TIBEMS_QUEUE
break
case "TOPIC":
destType = TIBEMS_TOPIC
}
// create the destination
status := C.tibemsDestination_Create(&dest, destType, C.CString(destination))
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, 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 "", false, errors.New(e)
}
// create the consumer
status = C.tibemsSession_CreateConsumer(session, &msgConsumer, dest, nil, TIBEMS_FALSE)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, errors.New(e)
}
// start the connection
status = C.tibemsConnection_Start(c.conn)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, errors.New(e)
}
status = C.tibemsMsgConsumer_ReceiveTimeout(msgConsumer, &msg, C.castToLong(C.int(timeout)))
if status != TIBEMS_OK {
if status == TIBEMS_TIMEOUT {
return "", true, nil
} else {
e, s := c.getErrorContext()
fmt.Println(s)
return "", false, errors.New(e)
}
}
// Check message type
status = C.tibemsMsg_GetBodyType(msg, &msgType)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, errors.New(e)
}
switch msgType {
case TIBEMS_MESSAGE:
msgTypeName = "MESSAGE"
break
case TIBEMS_TEXT_MESSAGE:
msgTypeName = "TEXT"
break
case TIBEMS_BYTES_MESSAGE:
msgTypeName = "BYTES"
break
case TIBEMS_OBJECT_MESSAGE:
msgTypeName = "OBJECT"
break
case TIBEMS_STREAM_MESSAGE:
msgTypeName = "STREAM"
break
case TIBEMS_MAP_MESSAGE:
msgTypeName = "MAP"
break
default:
msgTypeName = "UNKNOWN"
break
}
if msgType != TIBEMS_TEXT_MESSAGE {
return "", false, errors.New("Unable to process message type " + msgTypeName)
}
// 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(msg, &buf)
messageText := C.GoString(buf)
//fmt.Println("Received JMS Text Message = "+ messageText)
// destroy the message
status = C.tibemsMsg_Destroy(msg)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, errors.New(e)
}
// destroy the session
status = C.tibemsSession_Close(session)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, errors.New(e)
}
// destroy the destination
status = C.tibemsDestination_Destroy(dest)
if status != TIBEMS_OK {
e, _ := c.getErrorContext()
return "", false, errors.New(e)
}
return messageText, false, nil
}
func (c *Client) Send(destination string, destinationType 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
var destType C.tibemsDestinationType = TIBEMS_QUEUE
switch strings.ToUpper(destinationType) {
case "QUEUE":
destType = TIBEMS_QUEUE
break
case "TOPIC":
destType = TIBEMS_TOPIC
}
// create the destination
status := C.tibemsDestination_Create(&dest, destType, 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 buf1, buf2 *C.char
defer C.free(unsafe.Pointer(buf1))
defer C.free(unsafe.Pointer(buf2))
C.tibemsErrorContext_GetLastErrorString(c.errorContext, &buf1)
errorString = C.GoString(buf1)
C.tibemsErrorContext_GetLastErrorStackTrace(c.errorContext, &buf2)
stackTrace = C.GoString(buf2)
return errorString, stackTrace
}