-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathatmi.go
1760 lines (1443 loc) · 44.8 KB
/
atmi.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief XATMI main package
* NOTES for finalizers! Note that if we pass from finalized object (typed ubf,
* expression tree or ATMI Context) pointer to C, the and the function call
* for the object is last in the object's go scope, the go might being to GC
* the go object, while the C function have received pointer in args. Thus
* C side in the middle of processing might get destructed object. e.g.
* c_str := C.BPrintStrC(&u.Buf.Ctx.c_ctx, (*C.UBFH)(unsafe.Pointer(u.Buf.C_ptr)))
* after enter in C.BPrintStrC(), the GC might kill the u object. Thus to avoid
* this, we create a defered "no-op" call in the enter of go func. With "u.Buf.nop()"
* at the end of the functions.
*
* @file atmi.go
*/
/* -----------------------------------------------------------------------------
* Enduro/X Middleware Platform for Distributed Transaction Processing
* Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
* Copyright (C) 2017-2019, Mavimax, Ltd. All Rights Reserved.
* This software is released under one of the following licenses:
* LGPL or Mavimax's license for commercial use.
* See LICENSE file for full text.
*
* C (as designed by Dennis Ritchie and later authors) language code is licensed
* under Enduro/X Modified GNU Affero General Public License, version 3.
* See LICENSE_C file for full text.
* -----------------------------------------------------------------------------
* LGPL license:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License, version 3 as published
* by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 3
* for more details.
*
* You should have received a copy of the Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -----------------------------------------------------------------------------
* A commercial use license is available from Mavimax, Ltd
* -----------------------------------------------------------------------------
*/
package atmi
/*
#cgo pkg-config: atmisrvinteg
#include <ndebug.h>
#include <string.h>
#include <stdlib.h>
#include <oatmi.h>
#include <onerror.h>
// Wrapper for TPNIT
static int go_tpinit(TPCONTEXT_T *p_ctxt) {
return Otpinit(p_ctxt, NULL);
}
//ATMI library error code
static int go_tperrno(TPCONTEXT_T *p_ctxt) {
return Otperrno(p_ctxt);
}
//Standard library error code
static int go_Nerror(TPCONTEXT_T *p_ctxt) {
return ONerror(p_ctxt);
}
static void free_string(char* s) { free(s); }
static char * malloc_string(int size) { return malloc(size); }
static void go_param_to_tpqctl(
TPQCTL *qc,
long *ctl_flags,
long *ctl_deq_time,
long *ctl_priority,
long *ctl_diagnostic,
char *ctl_diagmsg,
char *ctl_msgid,
char *ctl_corrid,
char *ctl_replyqueue,
char *ctl_failurequeue,
char *ctl_cltid,
long *ctl_urcode,
long *ctl_appkey,
long *ctl_delivery_qos,
long *ctl_reply_qos,
long *ctl_exp_time)
{
qc->flags = *ctl_flags;
qc->deq_time = *ctl_deq_time;
qc->priority = *ctl_priority;
qc->diagnostic = *ctl_diagnostic;
strcpy(qc->diagmsg, ctl_diagmsg);
memcpy(qc->msgid, ctl_msgid, TMMSGIDLEN);
memcpy(qc->corrid, ctl_corrid, TMCORRIDLEN);
strcpy(qc->replyqueue, ctl_replyqueue);
strcpy(qc->failurequeue, ctl_failurequeue);
strcpy(qc->cltid.clientdata, ctl_cltid);
qc->urcode = *ctl_urcode;
qc->appkey = *ctl_appkey;
qc->delivery_qos = *ctl_delivery_qos;
qc->reply_qos = *ctl_reply_qos;
qc->exp_time = *ctl_exp_time;
}
static void go_tpqctl_to_param(
TPQCTL *qc,
long *ctl_flags,
long *ctl_deq_time,
long *ctl_priority,
long *ctl_diagnostic,
char *ctl_diagmsg,
char *ctl_msgid,
char *ctl_corrid,
char *ctl_replyqueue,
char *ctl_failurequeue,
char *ctl_cltid,
long *ctl_urcode,
long *ctl_appkey,
long *ctl_delivery_qos,
long *ctl_reply_qos,
long *ctl_exp_time)
{
*ctl_flags = qc->flags;
*ctl_deq_time = qc->deq_time;
*ctl_priority = qc->priority;
*ctl_diagnostic = qc->diagnostic;
strcpy(ctl_diagmsg, qc->diagmsg);
memcpy(ctl_msgid, qc->msgid, TMMSGIDLEN);
memcpy(ctl_corrid, qc->corrid, TMCORRIDLEN);
strcpy(ctl_replyqueue, qc->replyqueue);
strcpy(ctl_failurequeue, qc->failurequeue);
strcpy(ctl_cltid, qc->cltid.clientdata);
qc->urcode = *ctl_urcode;
qc->appkey = *ctl_appkey;
qc->delivery_qos = *ctl_delivery_qos;
qc->reply_qos = *ctl_reply_qos;
qc->exp_time = *ctl_exp_time;
}
static int go_tpenqueue (TPCONTEXT_T *p_ctx, char *qspace, char *qname, char *data, long len, long flags,
long *ctl_flags,
long *ctl_deq_time,
long *ctl_priority,
long *ctl_diagnostic,
char *ctl_diagmsg,
char *ctl_msgid,
char *ctl_corrid,
char *ctl_replyqueue,
char *ctl_failurequeue,
char *ctl_cltid,
long *ctl_urcode,
long *ctl_appkey,
long *ctl_delivery_qos,
long *ctl_reply_qos,
long *ctl_exp_time
)
{
int ret;
TPQCTL qc;
memset(&qc, 0, sizeof(qc));
go_param_to_tpqctl(&qc,
ctl_flags,
ctl_deq_time,
ctl_priority,
ctl_diagnostic,
ctl_diagmsg,
ctl_msgid,
ctl_corrid,
ctl_replyqueue,
ctl_failurequeue,
ctl_cltid,
ctl_urcode,
ctl_appkey,
ctl_delivery_qos,
ctl_reply_qos,
ctl_exp_time);
ret = Otpenqueue (p_ctx, qspace, qname, &qc, data, len, flags);
go_tpqctl_to_param(&qc,
ctl_flags,
ctl_deq_time,
ctl_priority,
ctl_diagnostic,
ctl_diagmsg,
ctl_msgid,
ctl_corrid,
ctl_replyqueue,
ctl_failurequeue,
ctl_cltid,
ctl_urcode,
ctl_appkey,
ctl_delivery_qos,
ctl_reply_qos,
ctl_exp_time);
return ret;
}
static int go_tpdequeue (TPCONTEXT_T *p_ctx, char *qspace, char *qname, char **data, long *len, long flags,
long *ctl_flags,
long *ctl_deq_time,
long *ctl_priority,
long *ctl_diagnostic,
char *ctl_diagmsg,
char *ctl_msgid,
char *ctl_corrid,
char *ctl_replyqueue,
char *ctl_failurequeue,
char *ctl_cltid,
long *ctl_urcode,
long *ctl_appkey,
long *ctl_delivery_qos,
long *ctl_reply_qos,
long *ctl_exp_time
)
{
int ret;
TPQCTL qc;
memset(&qc, 0, sizeof(qc));
go_param_to_tpqctl(&qc,
ctl_flags,
ctl_deq_time,
ctl_priority,
ctl_diagnostic,
ctl_diagmsg,
ctl_msgid,
ctl_corrid,
ctl_replyqueue,
ctl_failurequeue,
ctl_cltid,
ctl_urcode,
ctl_appkey,
ctl_delivery_qos,
ctl_reply_qos,
ctl_exp_time);
ret = Otpdequeue (p_ctx, qspace, qname, &qc, data, len, flags);
go_tpqctl_to_param(&qc,
ctl_flags,
ctl_deq_time,
ctl_priority,
ctl_diagnostic,
ctl_diagmsg,
ctl_msgid,
ctl_corrid,
ctl_replyqueue,
ctl_failurequeue,
ctl_cltid,
ctl_urcode,
ctl_appkey,
ctl_delivery_qos,
ctl_reply_qos,
ctl_exp_time);
return ret;
}
//We need a tpfree version with NULL context
//So if we run in NULL context, then we must kill the new context appeared
//after the function call... (if any...)
//NOTE that tpfree will allocate auto-context if none currently present...
void go_tpfree(char *ptr)
{
// Allocate new context + set it...
TPCONTEXT_T c = tpnewctxt(0, 1);
tpfree(ptr);
tpfreectxt(c);
}
//Read the return code from current ATMI context
//@param p_ctx ATMI context
//@param c_err C error is set
//@return tpurcode (or 0 and error loaded)
static long go_tpurcode(TPCONTEXT_T *p_ctx, int *c_err)
{
long ret=0;
if (EXSUCCEED!=ndrx_tpsetctxt(*p_ctx, 0, CTXT_PRIV_ATMI|CTXT_PRIV_NSTD))
{
userlog("Failed to set ATMI context");
*c_err=EXTRUE;
goto out;
}
else
{
*c_err=EXFALSE;
}
ret=tpurcode;
//Move process back to NULL context
ndrx_tpsetctxt(TPNULLCONTEXT, 0L, CTXT_PRIV_ATMI|CTXT_PRIV_NSTD);
out:
return ret;
}
*/
import "C"
import "unsafe"
import "fmt"
import "runtime"
import "encoding/base64"
/*
* SUCCEED/FAIL flags
*/
const (
SUCCEED = 0
FAIL = -1
)
/*
* List of ATMI Error codes (atmi.h/xatmi.h)
*/
const (
TPMINVAL = 0
TPEABORT = 1
TPEBADDESC = 2
TPEBLOCK = 3
TPEINVAL = 4
TPELIMIT = 5
TPENOENT = 6
TPEOS = 7
TPEPERM = 8
TPEPROTO = 9
TPESVCERR = 10
TPESVCFAIL = 11
TPESYSTEM = 12
TPETIME = 13
TPETRAN = 14
TPGOTSIG = 15
TPERMERR = 16
TPEITYPE = 17
TPEOTYPE = 18
TPERELEASE = 19
TPEHAZARD = 20
TPEHEURISTIC = 21
TPEEVENT = 22
TPEMATCH = 23
TPEDIAGNOSTIC = 24
TPEMIB = 25
TPINITFAIL = 30
TPMAXVAL = 31
)
/*
* flag bits for C language xatmi routines
*/
const (
TPNOBLOCK = 0x00000001
TPSIGRSTRT = 0x00000002
TPNOREPLY = 0x00000004
TPNOTRAN = 0x00000008
TPTRAN = 0x00000010
TPNOTIME = 0x00000020
TPGETANY = 0x00000080
TPNOCHANGE = 0x00000100
TPCONV = 0x00000400
TPSENDONLY = 0x00000800
TPRECVONLY = 0x00001000
TPTRANSUSPEND = 0x00040000 /* Suspend current transaction */
TPSOFTTIMEOUT = 0x00080000 /* Software time-out, translated to XATMI timeout for caller */
TPSOFTNOENT = 0x00100000 /* No service entry */
TPNOAUTBUF = 0x00200000 /* Don't restore autbuf in srv context */
TPREGEXMATCH = 0x00800000 /* Use regular expressoins for match */
TPNOCACHELOOK = 0x01000000 /* Do not lookup cache */
TPNOCACHEADD = 0x02000000 /* Do not save data to cache */
TPNOCACHEDDATA = 0x04000000 /* Do not use cached data */
TPNOABORT = 0x08000000 /* Do not abort global transaction if svc call failed */
)
/*
* values for rval in tpreturn
*/
const (
TPFAIL = 0x0001
TPSUCCESS = 0x0002
)
/*
* events returned during conversational communication
*/
const (
TPEV_DISCONIMM = 0x0001
TPEV_SVCERR = 0x0002
TPEV_SVCFAIL = 0x0004
TPEV_SVCSUCC = 0x0008
TPEV_SENDONLY = 0x0020
)
/*
* Transaction handling constants
*/
const (
TPTXCOMMITDLOG = 0x00000004 /**< Commit decision logged */
TPTXNOOPTIM = 0x00000100 /**< No known host optimization */
)
/*
* Max message size (int bytes)
const (
ATMI_MSG_MAX_SIZE = 65536
)
Replaced with atmi.ATMIMsgSizeMax()!
*/
/*
* TPQCTL.flags flags
*/
const (
TPNOFLAGS = 0x00000
TPQCORRID = 0x00001 /* set/get correlation id */
TPQFAILUREQ = 0x00002 /* set/get failure queue */
TPQBEFOREMSGID = 0x00004 /* RFU, enqueue before message id */
TPQGETBYMSGIDOLD = 0x00008 /* RFU, deprecated */
TPQMSGID = 0x00010 /* get msgid of enq/deq message */
TPQPRIORITY = 0x00020 /* set/get message priority */
TPQTOP = 0x00040 /* RFU, enqueue at queue top */
TPQWAIT = 0x00080 /* RFU, wait for dequeuing */
TPQREPLYQ = 0x00100 /* set/get reply queue */
TPQTIME_ABS = 0x00200 /* RFU, set absolute time */
TPQTIME_REL = 0x00400 /* RFU, set absolute time */
TPQGETBYCORRIDOLD = 0x00800 /* deprecated */
TPQPEEK = 0x01000 /* peek */
TPQDELIVERYQOS = 0x02000 /* RFU, delivery quality of service */
TPQREPLYQOS = 0x04000 /* RFU, reply message quality of service */
TPQEXPTIME_ABS = 0x08000 /* RFU, absolute expiration time */
TPQEXPTIME_REL = 0x10000 /* RFU, relative expiration time */
TPQEXPTIME_NONE = 0x20000 /* RFU, never expire */
TPQGETBYMSGID = 0x40008 /* dequeue by msgid */
TPQGETBYCORRID = 0x80800 /* dequeue by corrid */
TPQASYNC = 0x100000 /* Async complete */
)
/*
* Values for TQPCTL.diagnostic
*/
const (
QMEINVAL = -1
QMEBADRMID = -2
QMENOTOPEN = -3
QMETRAN = -4
QMEBADMSGID = -5
QMESYSTEM = -6
QMEOS = -7
QMEABORTED = -8
QMENOTA = -8 /* QMEABORTED */
QMEPROTO = -9
QMEBADQUEUE = -10
QMENOMSG = -11
QMEINUSE = -12
QMENOSPACE = -13
QMERELEASE = -14
QMEINVHANDLE = -15
QMESHARE = -16
)
/*
* Q constants
*/
const (
TMMSGIDLEN = 32
TMCORRIDLEN = 32
TMQNAMELEN = 15
NDRX_MAX_ID_SIZE = 96
)
/*
* Log levels for TPLOG (corresponding to ndebug.h)
*/
const (
LOG_ALWAYS = 1
LOG_ERROR = 2
LOG_WARN = 3
LOG_INFO = 4
LOG_DEBUG = 5
LOG_DUMP = 6
)
/*
* Logging facilites
*/
const (
LOG_FACILITY_NDRX = 0x00001 /* settings for ATMI logging */
LOG_FACILITY_UBF = 0x00002 /* settings for UBF logging */
LOG_FACILITY_TP = 0x00004 /* settings for TP logging */
LOG_FACILITY_TP_THREAD = 0x00008 /* settings for TP, thread based logging */
LOG_FACILITY_TP_REQUEST = 0x00010 /* Request logging, thread based */
)
/*
* Enduro/X standard library error codes
*/
const (
NEINVALINI = 1 /* Invalid INI file */
NEMALLOC = 2 /* Malloc failed */
NEUNIX = 3 /* Unix error occurred */
NEINVAL = 4 /* Invalid value passed to function */
NESYSTEM = 5 /* System failure */
NEMANDATORY = 6 /* Mandatory field is missing */
NEFORMAT = 7 /* Format error */
NETOUT = 8 /* Time-out condition */
NENOCONN = 9 /* No connection */
NELIMIT = 10 /* Limit reached */
)
/**
* Enduro/X extensions
*/
const (
TPEX_NOCHANGE = 0x00000004 /**< Reject tpimport with error if */
TPEX_STRING = 0x00000008 /**< Export buffer in base64 format */
)
/**
* Multi contexting defines
*/
const (
TPNULLCONTEXT = 0 /**< NULL Context */
)
/*
* Transaction ID type
*/
type TPTRANID struct {
c_tptranid C.TPTRANID
}
/*
* ATMI Context object
*/
type ATMICtx struct {
gcoff int //dummy counter tricking the gc to suspend while using object in c
c_ctx C.TPCONTEXT_T
}
/*
* Server context data (used for server's main thread
* switching taks to worker thread)
*/
type TPSRVCTXDATA struct {
c_ptr *C.char
}
/*
* Event controll struct
*/
type TPEVCTL struct {
flags int64
name1 string
name2 string
}
/*
* Queue control structure
*/
type TPQCTL struct {
Flags int64 /* indicates which of the values are set */
Deq_time int64 /* absolute/relative time for dequeuing */
Priority int64 /* enqueue priority */
Diagnostic int64 /* indicates reason for failure */
Diagmsg string /* diagnostic message */
Msgid [TMMSGIDLEN]byte /* id of message before which to queue */
Corrid [TMCORRIDLEN]byte /* correlation id used to identify message */
Replyqueue string /* queue name for reply message */
Failurequeue string /* queue name for failure message */
Cltid string /* client identifier for originating client */
Urcode int64 /* application user-return code */
Appkey int64 /* application authentication client key */
Delivery_qos int64 /* delivery quality of service */
Reply_qos int64 /* reply message quality of service */
Exp_time int64 /* expiration time */
}
///////////////////////////////////////////////////////////////////////////////////
// ATMI Buffers section
///////////////////////////////////////////////////////////////////////////////////
//ATMI buffer
type ATMIBuf struct {
gcoff int
C_ptr *C.char
//We will need some API for length & buffer setting
//Probably we need a wrapper for lenght function
C_len C.long
//have finalizer installed
HaveFinalizer bool
//Have some context, just a reference to, for ATMI buffer operations
Ctx *ATMICtx
}
//Base interface for typed buffer
type TypedBuffer interface {
GetBuf() *ATMIBuf
}
//Have inteface to base ATMI buffer
func (u *ATMIBuf) GetBuf() *ATMIBuf {
return u
}
//Do nothing, to trick the GC
func (u *ATMIBuf) nop() int {
u.gcoff++
return u.gcoff
}
//Max message size
//@return buffer size configured by Enduro/X, min 64K
func ATMIMsgSizeMax() int64 {
return int64(C.ndrx_msgsizemax())
}
///////////////////////////////////////////////////////////////////////////////////
// Error Handlers, ATMI level
///////////////////////////////////////////////////////////////////////////////////
//ATMI Error type
type atmiError struct {
code int
message string
}
//ATMI error interface
type ATMIError interface {
Error() string
Code() int
Message() string
}
//Do nothing, to trick the GC
func (ac *ATMICtx) nop() int {
ac.gcoff++
return ac.gcoff
}
//Generate ATMI error, read the codes
func (ac *ATMICtx) NewATMIError() ATMIError {
var err atmiError
err.code = int(C.go_tperrno(&ac.c_ctx))
err.message = C.GoString(C.Otpstrerror(&ac.c_ctx, C.go_tperrno(&ac.c_ctx)))
return err
}
//Build a custom error
//@param err Error buffer to build
//@param code Error code to setup
//@param msg Error message
func NewCustomATMIError(code int, msg string) ATMIError {
var err atmiError
err.code = code
err.message = msg
return err
}
//Standard error interface
func (e atmiError) Error() string {
return fmt.Sprintf("%d: %s", e.code, e.message)
}
//code getter
func (e atmiError) Code() int {
return e.code
}
//message getter
func (e atmiError) Message() string {
return e.message
}
///////////////////////////////////////////////////////////////////////////////////
// Error Handlers, NSTD - Enduro/X Standard library
///////////////////////////////////////////////////////////////////////////////////
//NSTD Error type
type nstdError struct {
code int
message string
}
//NSTD error interface
type NSTDError interface {
Error() string
Code() int
Message() string
}
//Generate NSTD error, read the codes
func (ac *ATMICtx) NewNstdError() NSTDError {
var err nstdError
err.code = int(C.go_Nerror(&ac.c_ctx))
err.message = C.GoString(C.ONstrerror(&ac.c_ctx, C.go_Nerror(&ac.c_ctx)))
return err
}
//Build a custom error. Can be used at Go level sources
//To simulate standard error
//@param err Error buffer to build
//@param code Error code to setup
//@param msg Error message
func NewCustomNstdError(code int, msg string) NSTDError {
var err nstdError
err.code = code
err.message = msg
return err
}
//Standard error interface
func (e nstdError) Error() string {
return fmt.Sprintf("%d: %s", e.code, e.message)
}
//Error code getter
func (e nstdError) Code() int {
return e.code
}
//Error message getter
func (e nstdError) Message() string {
return e.message
}
///////////////////////////////////////////////////////////////////////////////////
// API Section
// TODO: Think about persistent association with thread. So that
// in XA case it would be simpler to manipulate with DB + XATMI...
///////////////////////////////////////////////////////////////////////////////////
//Allocate new ATMI context. This is the context with most of the XATMI operations
//are made. Single go routine can have multiple contexts at the same time.
//The function does not open queues or init XATMI sub-system unless the dependant
//operation is called. For example you may allocat the context and use it for logging
//that will not make overhead for system queues.
//@return ATMI Error, Pointer to ATMI Context object
func NewATMICtx() (*ATMICtx, ATMIError) {
var ret ATMICtx
ret.c_ctx = C.tpnewctxt(0, 0)
if nil == ret.c_ctx {
return nil, NewCustomATMIError(TPESYSTEM, "Failed to allocate "+
"new context - see ULOG for details")
}
runtime.SetFinalizer(&ret, freeATMICtx)
return &ret, nil
}
//Free up the ATMI Context
//Internally this will call the TpTerm too to termiante any XATMI client
//session in progress.
func (ac *ATMICtx) FreeATMICtx() {
ac.TpTerm() //This extra, but let it be
C.Otpfreectxt(&ac.c_ctx, ac.c_ctx)
}
//Associate current OS thread with context
//This might be needed for global transaction processing
//Which uses underlaying OS threads for transaction association
func (ac *ATMICtx) AssocThreadWithCtx() ATMIError {
if ret := C.tpsetctxt(ac.c_ctx, 0); SUCCEED != ret {
return ac.NewATMIError()
}
return nil
}
//Disassocate current os thread from context
//This might be needed for global transaction processing
//Which uses underlaying OS threads for transaction association
func (ac *ATMICtx) DisassocThreadFromCtx() ATMIError {
if ret := C.tpgetctxt(&ac.c_ctx, 0); SUCCEED != ret {
return ac.NewATMIError()
}
return nil
}
//Kill the ATMI context (internal version for finalizer)
func freeATMICtx(ac *ATMICtx) {
if nil != ac.c_ctx {
//ac.TpTerm() //This extra, but let it be - not needed, free will do.
C.Otpfreectxt(&ac.c_ctx, ac.c_ctx)
}
}
//Make context object from C pointer. Function can be used in case
//If doing any direct XATMI operations and you have a C context handler.
//Which can be promoted to Go level ATMI Context.
//@param c_ctx Context ATMI object
//@return ATMI Context Object
func MakeATMICtx(c_ctx C.TPCONTEXT_T) *ATMICtx {
var ret ATMICtx
ret.c_ctx = c_ctx
return &ret
}
//TODO, maybe we need to use error deligates, so that user can override the error handling object?
//Allocate buffer
//Accepts the standard ATMI values
//We should add error handling here
//@param b_type Buffer type
//@param b_subtype Buffer sub-type
//@param size Buffer size request
//@return ATMI Buffer, atmiError
func (ac *ATMICtx) TpAlloc(b_type string, b_subtype string, size int64) (*ATMIBuf, ATMIError) {
var buf ATMIBuf
var err ATMIError
c_type := C.CString(b_type)
c_subtype := C.CString(b_subtype)
size_l := C.long(size)
buf.C_ptr = C.Otpalloc(&ac.c_ctx, c_type, c_subtype, size_l)
//Check the error
if nil == buf.C_ptr {
err = ac.NewATMIError()
}
C.free(unsafe.Pointer(c_type))
C.free(unsafe.Pointer(c_subtype))
runtime.SetFinalizer(&buf, tpfree)
buf.HaveFinalizer = true
ac.nop() //keep context until the end of the func, and only then allow gc
return &buf, err
}
//Change the context of the buffers (needed for error handling)
func (buf *ATMIBuf) TpSetCtxt(ac *ATMICtx) {
buf.Ctx = ac
}
//Reallocate the buffer
//@param buf ATMI buffer
//@return ATMI Error
func (buf *ATMIBuf) TpRealloc(size int64) ATMIError {
var err ATMIError
buf.C_ptr = C.Otprealloc(&buf.Ctx.c_ctx, buf.C_ptr, C.long(size))
if nil == buf.C_ptr {
err = buf.Ctx.NewATMIError()
}
buf.nop()
return err
}
//Initialize client
//@return ATMI Error
func (ac *ATMICtx) TpInit() ATMIError {
var err ATMIError
if SUCCEED != C.go_tpinit(&ac.c_ctx) {
err = ac.NewATMIError()
}
ac.nop() //keep context until the end of the func, and only then allow gc
return err
}
// Do the service call, assume using the same buffer
// for return value.
// This works for self describing buffers. Otherwise we need a buffer size in
// ATMIBuf.
// @param svc service name
// @param buf ATMI buffer
// @param flags Flags to be used
// @return atmiError
func (ac *ATMICtx) TpCall(svc string, tb TypedBuffer, flags int64) (int, ATMIError) {
var err ATMIError
c_svc := C.CString(svc)
buf := tb.GetBuf()
ret := C.Otpcall(&ac.c_ctx, c_svc, buf.C_ptr, buf.C_len, &buf.C_ptr, &buf.C_len, C.long(flags))
if SUCCEED != ret {
err = ac.NewATMIError()
}
//Check the types
switch tb.(type) {
case *TypedVIEW:
if v, ok := tb.(*TypedVIEW); ok {
itype := ""
_, errA := ac.TpTypes(buf, &itype, &v.view)
if nil != err && nil != errA {
err = errA
}
}
break
}
C.free(unsafe.Pointer(c_svc))
ac.nop() //keep context until the end of the func, and only then allow gc
return int(ret), err
}
//TP Async call
//@param svc Service Name to call
//@param buf ATMI buffer
//@param flags Flags to be used for call (see flags section)
//@return Call Descriptor (cd), ATMI Error
func (ac *ATMICtx) TpACall(svc string, tb TypedBuffer, flags int64) (int, ATMIError) {
var err ATMIError
c_svc := C.CString(svc)
buf := tb.GetBuf()
ret := C.Otpacall(&ac.c_ctx, c_svc, buf.C_ptr, buf.C_len, C.long(flags))
if FAIL == ret {
err = ac.NewATMIError()
}
C.free(unsafe.Pointer(c_svc))
buf.nop() //keep context until the end of the func, and only then allow gc
ac.nop() //keep context until the end of the func, and only then allow gc
return int(ret), err
}
//Get async call reply
//@param cd call
//@param buf ATMI buffer
//@param flags call flags
func (ac *ATMICtx) TpGetRply(cd *int, tb TypedBuffer, flags int64) (int, ATMIError) {
var err ATMIError
var c_cd C.int
buf := tb.GetBuf()
ret := C.Otpgetrply(&ac.c_ctx, &c_cd, &buf.C_ptr, &buf.C_len, C.long(flags))
*cd = int(c_cd)
if SUCCEED != ret {
err = ac.NewATMIError()
}
ac.nop() //keep context until the end of the func, and only then allow gc
buf.nop() //keep context until the end of the func, and only then allow gc
return int(ret), err
}
//Cancel async call
//@param cd Call descriptor
//@return ATMI error
func (ac *ATMICtx) TpCancel(cd int) ATMIError {
var err ATMIError
ret := C.Otpcancel(&ac.c_ctx, C.int(cd))
if SUCCEED != ret {
err = ac.NewATMIError()
}
ac.nop() //keep context until the end of the func, and only then allow gc
return err
}
//Connect to service in conversational mode
//@param svc Service name
//@param data ATMI buffers