dc_ttapi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /***************************************************************************
  2. *
  3. * 通过TTAPI发送
  4. *
  5. * Author : Li Bo Feng
  6. *
  7. * Update History
  8. * DATE OWNER DESCRIPTION
  9. * ----------- ------------ -----------
  10. * 2014-12-29 Li Bo Feng Generated
  11. *
  12. ***************************************************************************/
  13. #include "dc.h"
  14. #include "dc_code.h"
  15. #include "dc_args.h"
  16. #include "ttapi.h"
  17. /**
  18. * TTAPI连接配置及缓存信息
  19. */
  20. static struct TTConnection
  21. {
  22. int is_configured;
  23. int initialized;
  24. tt_config_t config;
  25. } ttconn[2];
  26. // ttconn[IDX_DOMESTIC]
  27. // ttconn[IDX_ABROAD]
  28. static tt_values_list_t vcontext[3]; //fixed variable for current system to set virtical context of TT
  29. static tt_values_list_t vcontext_dynamic[9]; //variable set to set other virtical context for every record line in log
  30. void _clear_ttapi(int serverid)
  31. {
  32. if (ttconn[serverid].initialized)
  33. {
  34. TT_shutdown(&(ttconn[serverid].config));
  35. ttconn[serverid].initialized = 0;
  36. }
  37. }
  38. int _init_ttapi(int serverid)
  39. {
  40. ABLOG_Entry(Public, _init_ttapi);
  41. memset(&(ttconn[serverid]), 0x00, sizeof(struct TTConnection));
  42. int iRtn;
  43. if (dc_args.ttapi[serverid].server_name != NULL
  44. && strlen(dc_args.ttapi[serverid].server_name) > 0
  45. && dc_args.ttapi[serverid].server_addr != NULL
  46. && strlen(dc_args.ttapi[serverid].server_addr) > 0)
  47. {
  48. (ttconn[serverid].config).server = dc_args.ttapi[serverid].server_addr;
  49. //set vcontext value with stSys
  50. vcontext[0].name = "ServerName";
  51. vcontext[0].value = dc_args.server_name;
  52. vcontext[0].size = strlen(vcontext[0].value);
  53. vcontext[1].name = "ComponentName";
  54. vcontext[1].value = dc_args.instance_name;
  55. vcontext[1].size = strlen(vcontext[1].value);
  56. vcontext[2].name = "ApplicationName";
  57. vcontext[2].value = dc_args.app_name;
  58. vcontext[2].size = strlen(vcontext[2].value);
  59. vcontext[0].next = &vcontext[1];
  60. vcontext[1].next = &vcontext[2];
  61. vcontext[2].next = &vcontext_dynamic[0];
  62. //TTAPI check function
  63. iRtn = TT_check_version();
  64. if (iRtn != 0)
  65. {
  66. ABLOG_Printf(Error, (ABLOG,"[E] TT_check_version failed, [%d]",iRtn));
  67. ABLOG_Return_Code(DC_RET_TTAPI_VERSION_ERROR);
  68. }
  69. //TT_send initialization
  70. iRtn = TT_init(&(ttconn[serverid].config));
  71. if (iRtn != 0)
  72. {
  73. ABLOG_Printf(Error, (ABLOG,"[E] TT_init failed: [%d]",iRtn));
  74. ABLOG_Return_Code(DC_RET_TTAPI_INITIALIZE_ERROR);
  75. }
  76. ABLOG_Printf(Metrics,
  77. (ABLOG,"[I] ttapi initialization [%s] OK!", (ttconn[serverid].config).server));
  78. ttconn[serverid].initialized = 1;
  79. ttconn[serverid].is_configured = 1;
  80. }
  81. else
  82. {
  83. ttconn[serverid].is_configured = 0;
  84. }
  85. ABLOG_Return_Code(DC_RET_OK);
  86. }
  87. int _ttSend(tt_config_t *config, tt_event_t *event)
  88. {
  89. ABLOG_Entry(Public, _ttSend);
  90. ABLOG_Printf(Detail,
  91. (ABLOG,"[D] TT_Track event, VID: [%s], HID: [%s]", event->vertical_id.link_id,event->horizontal_id.link_id));
  92. int iRtn_tt_track = TT_track(config, event);
  93. if (iRtn_tt_track != 0)
  94. {
  95. ABLOG_Printf(Error,
  96. (ABLOG,"[E] TT_send event track error, returned:[%d,0x%08X]",iRtn_tt_track,iRtn_tt_track));
  97. ABLOG_Return_Code(DC_RET_TTAPI_SEND_ERROR);
  98. }
  99. ABLOG_Return_Code(DC_RET_OK);
  100. }
  101. /*-------
  102. * funTT1
  103. * Description : send event
  104. * parameter : tt_config_t *config, struct LogRecord *stLog, tt_event_t *event
  105. *
  106. * OUT : 0-DC_RET_OK;1-DC_RET_ABNORMAL
  107. *------ */
  108. int funTT1(tt_config_t *config, struct LogRecord *stLog, tt_event_t *event)
  109. {
  110. ABLOG_Entry(Public, funTT1);
  111. char hlink[50 + 1];
  112. char vlink[50 + 1];
  113. int iRtn = 0;
  114. memset(hlink, 0x00, sizeof(hlink));
  115. memset(vlink, 0x00, sizeof(vlink));
  116. //memset (vcontext2,0x00,sizeof(vcontext2) ); /* clear vcontext[4],vcontext[5],vcontext[6] */
  117. //fprintf(stdout, "Start to create a STARTED_INBOUND event.\n");
  118. event->type = CYTA_STARTED_INBOUND_EVENT;
  119. //HID01=LinkSysName + FrontID
  120. sprintf(hlink, stLog->prepared.sLinkSys);
  121. strcat(hlink, stLog->received.sFrntID);
  122. //VID= SysName + FrontID
  123. sprintf(vlink, dc_args.sys_name);
  124. strcat(vlink, stLog->received.sFrntID);
  125. event->vertical_id.link_id = vlink;
  126. event->vertical_id.link_id_size = strlen(vlink);
  127. event->horizontal_id.link_id = hlink;
  128. event->horizontal_id.link_id_size = strlen(hlink);
  129. iRtn = _ttSend(config, event);
  130. ABLOG_Return_Code(iRtn);
  131. }
  132. /*-------
  133. * funTT2
  134. *
  135. * OUT : 0-DC_RET_OK;1-DC_RET_ABNORMAL
  136. *------- */
  137. int funTT2(tt_config_t *config, struct LogRecord *stLog, tt_event_t *event)
  138. {
  139. ABLOG_Entry(Public, funTT2);
  140. char hlink[50 + 1];
  141. char vlink[50 + 1];
  142. int iRtn = 0;
  143. memset(hlink, 0x00, sizeof(hlink));
  144. memset(vlink, 0x00, sizeof(vlink));
  145. //fprintf(stdout, "%s %d Start to create a OUTBOUND event.\n",__FILE__,__LINE__);
  146. event->type = CYTA_OUTBOUND_EVENT;
  147. //HID02=SysName + SendID
  148. sprintf(hlink, dc_args.sys_name);
  149. strcat(hlink, stLog->received.sSendID);
  150. //VID= SysName + FrontID
  151. sprintf(vlink, dc_args.sys_name);
  152. strcat(vlink, stLog->received.sFrntID);
  153. event->vertical_id.link_id = vlink;
  154. event->vertical_id.link_id_size = strlen(vlink);
  155. event->horizontal_id.link_id = hlink;
  156. event->horizontal_id.link_id_size = strlen(hlink);
  157. iRtn = _ttSend(config, event);
  158. ABLOG_Return_Code(iRtn);
  159. }
  160. /*-------
  161. * funTT3
  162. *
  163. * OUT : 0-DC_RET_OK;1-DC_RET_ABNORMAL
  164. *------- */
  165. int funTT3(tt_config_t *config, struct LogRecord *stLog, tt_event_t *event)
  166. {
  167. ABLOG_Entry(Public, funTT3);
  168. char hlink[50 + 1];
  169. char vlink[50 + 1];
  170. int iRtn = 0;
  171. memset(hlink, 0x00, sizeof(hlink));
  172. memset(vlink, 0x00, sizeof(vlink));
  173. //fprintf(stdout, "%s %d Start to create a INBOUND event.\n",__FILE__,__LINE__);
  174. event->type = CYTA_INBOUND_EVENT;
  175. //HID03=SysName + SendID + "R"
  176. sprintf(hlink, dc_args.sys_name);
  177. strcat(hlink, stLog->received.sSendID);
  178. strcat(hlink, "R");
  179. //VID =SysName + FrontID
  180. sprintf(vlink, dc_args.sys_name);
  181. strcat(vlink, stLog->received.sFrntID);
  182. event->vertical_id.link_id = vlink;
  183. event->vertical_id.link_id_size = strlen(vlink);
  184. event->horizontal_id.link_id = hlink;
  185. event->horizontal_id.link_id_size = strlen(hlink);
  186. iRtn = _ttSend(config, event);
  187. ABLOG_Return_Code(iRtn);
  188. }
  189. /*-------
  190. * funTT4
  191. *
  192. * OUT : 0-DC_RET_OK;1-DC_RET_ABNORMAL
  193. *------- */
  194. int funTT4(tt_config_t *config, struct LogRecord *stLog, tt_event_t *event)
  195. {
  196. ABLOG_Entry(Public, funTT4);
  197. char hlink[50 + 1];
  198. char vlink[50 + 1];
  199. int iRtn = 0;
  200. memset(hlink, 0x00, sizeof(hlink));
  201. memset(vlink, 0x00, sizeof(vlink));
  202. //fprintf(stdout, "Start to create an OUTBOUND_FINISHED event.\n");
  203. event->type = CYTA_OUTBOUND_FINISHED_EVENT;
  204. //HID04=LinkSys + FrontID + "R"
  205. sprintf(hlink, stLog->prepared.sLinkSys);
  206. strcat(hlink, stLog->received.sFrntID);
  207. strcat(hlink, "R");
  208. //VID =SysName + FrontID
  209. sprintf(vlink, dc_args.sys_name);
  210. strcat(vlink, stLog->received.sFrntID);
  211. event->vertical_id.link_id = vlink;
  212. event->vertical_id.link_id_size = strlen(vlink);
  213. event->horizontal_id.link_id = hlink;
  214. event->horizontal_id.link_id_size = strlen(hlink);
  215. iRtn = _ttSend(config, event);
  216. ABLOG_Return_Code(iRtn);
  217. }
  218. /*-------
  219. * Function name: TT_send
  220. * Description : Send event by function TT_track
  221. * IN : 1 struct LogRecord *; 2 tt_config_t *;
  222. * OUT : Return value of function
  223. *------- */
  224. int TT_send(int serverid, struct LogRecord *stLog)
  225. {
  226. ABLOG_Entry(Public, TT_send);
  227. int iRtn = 0;
  228. tt_event_t event;
  229. memset(&event, 0x00, sizeof(event));
  230. ABLOG_Printf(Detail,
  231. (ABLOG,"[D] Create a TT_Event object with the connection string :[%s]", dc_args.ttapi[serverid].server_addr));
  232. //set event timestamp value
  233. event.timestamp.sec = stLog->prepared.stTxnTime.tv_sec;
  234. event.timestamp.usec = stLog->prepared.stTxnTime.tv_usec;
  235. event.vertical_id.caller_type = TT_ANY_CALLER;
  236. event.horizontal_id.caller_type = TT_ANY_CALLER;
  237. event.instance_id.transaction_id = stLog->prepared.sTxnID; //2013-12-26 ADD
  238. event.instance_id.size = strlen(stLog->prepared.sTxnID); //2013-12-26 ADD
  239. //setting property of event
  240. event.vertical_context = vcontext;
  241. memset(vcontext_dynamic, 0x00, sizeof(vcontext_dynamic)); //clear vcontext[4],vcontext[5],vcontext[6]
  242. vcontext_dynamic[0].name = "TransactionName";
  243. vcontext_dynamic[0].value = stLog->prepared.sTxnID;
  244. vcontext_dynamic[0].size = strlen(vcontext_dynamic[0].value);
  245. vcontext_dynamic[1].name = "BankID";
  246. vcontext_dynamic[1].value = stLog->received.sBankID;
  247. vcontext_dynamic[1].size = strlen(vcontext_dynamic[1].value);
  248. vcontext_dynamic[2].name = "BranchID";
  249. vcontext_dynamic[2].value = stLog->received.sBrchID;
  250. vcontext_dynamic[2].size = strlen(vcontext_dynamic[2].value);
  251. //ABLOG_Printf(Detail,(ABLOG,"[D] ------------> stLog RTNCODE:[%s] <--------------",stLog->sRtnCode));
  252. vcontext_dynamic[3].name = "Status";
  253. vcontext_dynamic[3].value = stLog->prepared.sStatus;
  254. vcontext_dynamic[3].size = strlen(vcontext_dynamic[3].value);
  255. vcontext_dynamic[4].name = "FrontID";
  256. vcontext_dynamic[4].value = stLog->received.sFrntID;
  257. vcontext_dynamic[4].size = strlen(vcontext_dynamic[4].value);
  258. vcontext_dynamic[5].name = "SendID";
  259. vcontext_dynamic[5].value = stLog->received.sSendID;
  260. vcontext_dynamic[5].size = strlen(vcontext_dynamic[5].value);
  261. vcontext_dynamic[6].name = "ReturnCode";
  262. vcontext_dynamic[6].value = stLog->received.sRtnCode_orig;
  263. vcontext_dynamic[6].size = strlen(vcontext_dynamic[6].value);
  264. vcontext_dynamic[7].name = "TragetSystem";
  265. vcontext_dynamic[7].value = stLog->prepared.for_ttapi.sTargetSystem;
  266. vcontext_dynamic[7].size = strlen(vcontext_dynamic[7].value);
  267. vcontext_dynamic[8].name = "ChannelID";
  268. vcontext_dynamic[8].value = stLog->prepared.for_ttapi.sChannelID;
  269. vcontext_dynamic[8].size = strlen(vcontext_dynamic[8].value);
  270. vcontext_dynamic[0].next = &vcontext_dynamic[1];
  271. vcontext_dynamic[1].next = &vcontext_dynamic[2];
  272. vcontext_dynamic[2].next = &vcontext_dynamic[3];
  273. vcontext_dynamic[3].next = &vcontext_dynamic[4];
  274. vcontext_dynamic[4].next = &vcontext_dynamic[5];
  275. vcontext_dynamic[5].next = &vcontext_dynamic[6];
  276. vcontext_dynamic[6].next = &vcontext_dynamic[7];
  277. vcontext_dynamic[7].next = &vcontext_dynamic[8];
  278. //ABLOG_Printf(Detail,(ABLOG,"[D] event time - sec:[%ld] usec:[%ld]",stLog->stTxnTime.sec,stLog->stTxnTime.usec));
  279. switch (atoi(stLog->received.sTimeType))
  280. {
  281. case 1:
  282. iRtn = funTT1(&(ttconn[serverid].config), stLog, &event);
  283. break;
  284. case 2:
  285. iRtn = funTT2(&(ttconn[serverid].config), stLog, &event);
  286. break;
  287. case 3:
  288. iRtn = funTT3(&(ttconn[serverid].config), stLog, &event);
  289. break;
  290. case 4:
  291. iRtn = funTT4(&(ttconn[serverid].config), stLog, &event);
  292. break;
  293. default:
  294. ABLOG_Printf(Error,
  295. (ABLOG,"[E] TimeType of Event not in(01,02,03,04):[%s]",stLog->received.sTimeType));
  296. ABLOG_Return_Code(DC_RET_UNSUPPORT);
  297. }
  298. ABLOG_Return_Code(iRtn);
  299. }
  300. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  301. /**
  302. * 初始化TTAPI
  303. * 返回:
  304. * 正常返回 DC_RET_OK,否则返回错误码
  305. */
  306. int init_ttapi()
  307. {
  308. ABLOG_Entry(Public, init_ttapi);
  309. if (sizeof(ttconn) / sizeof(struct TTConnection) != sizeof(dc_args.ttapi) / sizeof(server_info))
  310. {
  311. ABLOG_Printf(Error,
  312. (ABLOG, "[E] program error. length of config(%d) != length of dc_args.ttapi(%d).", sizeof(ttconn) / sizeof(struct TTConnection), sizeof(dc_args.ttapi)/sizeof(server_info)));
  313. ABLOG_Return_Code(DC_RET_PARAMS_ERROR);
  314. }
  315. int iRtn;
  316. iRtn = _init_ttapi(IDX_DOMESTIC);
  317. if (iRtn != DC_RET_OK)
  318. {
  319. return iRtn;
  320. }
  321. iRtn = _init_ttapi(IDX_ABROAD);
  322. if (iRtn != DC_RET_OK)
  323. {
  324. _clear_ttapi(IDX_DOMESTIC);
  325. return iRtn;
  326. }
  327. ABLOG_Return_Code(DC_RET_OK);
  328. }
  329. /**
  330. * 发送TTAPI信息
  331. * 参数:
  332. * server_id ttas server标识
  333. * stLog 交易日志记录
  334. * 返回:
  335. * 正常返回 DC_RET_OK,否则返回错误码
  336. */
  337. int send_ttapi(int serverid, struct LogRecord *stLog)
  338. {
  339. ABLOG_Entry(Public, send_ttapi);
  340. int iRtn = DC_RET_TTAPI_NOT_INITIALIZED;
  341. if (!ttconn[serverid].is_configured)
  342. {
  343. ABLOG_Return_Code( DC_RET_TTAPI_NOT_CONFIGURED);
  344. }
  345. if (!ttconn[serverid].initialized)
  346. {
  347. iRtn = _init_ttapi(serverid);
  348. if (iRtn != DC_RET_OK)
  349. {
  350. ABLOG_Return_Code(iRtn);
  351. }
  352. }
  353. iRtn = TT_send(serverid, stLog);
  354. ABLOG_Return_Code(iRtn);
  355. }
  356. /**
  357. * 清除TTAPI
  358. */
  359. int clear_ttapi()
  360. {
  361. ABLOG_Entry(Public, clear_ttapi);
  362. int iRtn = DC_RET_TTAPI_NOT_INITIALIZED;
  363. _clear_ttapi(IDX_DOMESTIC);
  364. _clear_ttapi(IDX_ABROAD);
  365. ABLOG_Return_Code(iRtn);
  366. }