/* #ident "@(#) samples/atmi/simpapp/simpserv.c $Revision: 1.7 $" */ #include <stdio.h> #include <ctype.h> #include <atmi.h> /* TUXEDO Header File */ #include <userlog.h> /* TUXEDO Header File */ #include </home/zhangenhao/OraHome_1/tuxedo12.1.3.0.0/simpapp/clsinterface.h> #include <fml.h> #include "strPort.h" #include <string.h> #include <stdlib.h> #include "cJSON.h" #define ROW 4422 #define STRLEN 120 #define MOSTDOMAIN 200 #define DATALEN 2048 int lastIndexOf(char *dst, char *src); int indexOf(char *dst, char *src); void ltrim(char *str); void rtrim(char *str); void trim(char *str); void substring(char *dst, char *src, int start, int end); //储存域值对应表 int k; struct node{ char fname[40]; long fid; char ftype[20]; }gather[ROW]; //单条报文域 typedef struct dataVolume{ char code[40]; char data[4096]; char type[10]; }dataVolu; //储存所有报文信息 typedef struct msg{ char msgType[20]; int reqLen; int resLen; dataVolu request[MOSTDOMAIN]; dataVolu response[MOSTDOMAIN]; struct msg *next; }msgCount; int indexOf(char *dst, char *src) { char *p = NULL; int i = 0; p = strstr(dst, src); if(p != NULL){ while(dst != p){ dst ++;//移动指针直到匹配到p i++; } }else return -1; return i; } /*返回dst中最后一次出现src的位置(下标索引),不存在返回-1*/ int lastIndexOf(char *dst, char *src) { char *p = NULL; int i = 0; int len = strlen(src); p = strstr(dst, src); if(p != NULL){ while(p != NULL){ for(;dst != p; dst++) i++;//记录匹配地址下标 p = p + len;//移动p,到p+len的位置 p = strstr(p, src);//找到下一个相等的指针地址 } }else return -1; return i; } /*删除str左边第一个非空白字符前面的空白字符(空格符和横向制表符)*/ void ltrim(char *str) { int i =0; int len=strlen(str); while(str[i] != '\0'){ if(str[i] != 32 && str[i] != 9){ break; } i++; } if(i != 0){ int j = 0; while(j <= len-i){ str[j] = str[j+i]; j++; } } } /*删除str最后一个非空白字符后面的所有空白字符(空格符和横向制表符)*/ void rtrim(char *str) { int i = strlen(str) - 1; while(i >= 0){ if(str[i] != 32 && str[i] != 9) break; i--; } str[++i] = '\0'; } void trim(char *str) { rtrim(str); ltrim(str); } /*截取src字符串中,从下标为start开始到end-1(end前面)的字符串保存在dst中(下标从0开始)*/ void substring(char *dst, char *src, int start, int end) { int i = start; if(start > strlen(src)) return; if(end > strlen(src)) end = strlen(src); while(i < end) { dst[i - start] = src[i]; i++; } dst[i - start] = '\0'; return; } msgCount *appendNode(msgCount *head, msgCount *node); msgCount *appendNode(msgCount *head, msgCount *node) { msgCount *pr = head; if(node == NULL){ printf("node is NULL!"); return head; } if(head == NULL){ head = node; }else{ while(pr->next != NULL){//is NULL,it's last node pr = pr->next; } pr->next = node; } node->next = NULL; return head; } void displayNode(msgCount *head); void displayNode(msgCount *head){ msgCount *p = head; int i = 0; while(p != NULL){ int num = atoi(p->msgType); userlog("msgtype :%d", num); for(i = 0; i < p->reqLen; i++){ userlog("request %d: [%s.%s.%s]\n", i, p->request[i].code, p->request[i].data, p->request[i].type); } for(i = 0; i < p->resLen; i++){ userlog("response %d: [%s.%s.%s]\n", i, p->response[i].code, p->response[i].data, p->response[i].type); } p = p->next; } } struct msg *getNode(msgCount *head, int msgtype); struct msg *getNode(msgCount *head, int msgtype){ struct msg *p = head; while(p != NULL){ int num = atoi(p->msgType); if(num == msgtype){ userlog("Find the same packet,msgType :%d", num); return p; } p = p->next; } return NULL; } msgCount* analyTuxedo(msgCount *head); msgCount* analyTuxedo(msgCount *head){ char data[DATALEN] = {0}; char input[DATALEN] = {0}; //msgCount *head = NULL;//创建头指针 FILE *file; file = fopen("./MessageTableCopy.json", "r"); if(file == NULL){ printf("MessageTableCopy file not exist.\n"); } else{ int inde1 = 0; int inde2 = 0; int end = 0; memset(data, 0, sizeof(data)); memset(input, 0, sizeof(input)); while(fgets(data, DATALEN - 1, file) != NULL){ if(indexOf(data, "#") == 0){ inde1 = indexOf(data, ":"); inde2 = indexOf(data, "\n"); substring(input, data, 1, inde1); trim(input); //userlog("inde1 is:%s\n", input); if(indexOf(input, "MSGTYPE") >= 0){ //初始化链表节点 msgCount *node = NULL; node = (msgCount *)malloc(sizeof(msgCount)); //获取msgtype值 memset(input, 0, sizeof(input)); substring(input, data, inde1 + 1, inde2 - 1); trim(input); strcpy(node->msgType, input); //userlog("msgtype is:%s\n", node->msgType); if(fgets(data, DATALEN - 1, file) != NULL){//读下一行 //userlog("next line is:%s", data); inde1 = indexOf(data, ":"); inde2 = indexOf(data, "\n"); end = inde2; memset(input, 0, sizeof(input)); substring(input, data, 1, inde1); trim(input); if(indexOf(input, "Request") >= 0){ //读取Request memset(input, 0, sizeof(input)); substring(input, data, inde1 + 1, inde2 - 1); trim(input); node->reqLen = atoi(input); //userlog("Request len is:%d\n", node->reqLen); int j = 0; for(j = 0; j < node->reqLen; j++){ if(fgets(data, DATALEN - 1, file) != NULL){//循环读取Request的报文数据 inde1 = indexOf(data, ","); //inde2 = lastIndexOf(data, ","); end = indexOf(data, "\n"); int tag1 = indexOf(data, "code:"); memset(input, 0, sizeof(input)); substring(input, data, tag1 + 5, inde1); trim(input); strcpy(node->request[j].code, input); userlog("%d req code is.%s.\n", j, node->request[j].code); int tag2 = indexOf(data, "data:"); memset(input, 0, sizeof(input)); //substring(input, data, tag2 + 5, inde2); substring(input, data, tag2 + 5, end - 1); trim(input); strcpy(node->request[j].data, input); userlog("%d req data is.%s.\n", j, node->request[j].data); //int tag3 = indexOf(data, "type:"); //memset(input, 0, sizeof(input)); //substring(input, data, tag3 + 5, end -1); //trim(input); //strcpy(node->request[j].type, input); //userlog("%d req type is.%s.\n", j, node->request[j].type); } } } } if(fgets(data, DATALEN - 1, file) != NULL){// //userlog("next line is:%s", data); inde1 = indexOf(data, ":"); inde2 = indexOf(data, "\n"); end = inde2; memset(input, 0, sizeof(input)); substring(input, data, 1, inde1); trim(input); if(indexOf(input, "Response") >= 0){ //读取Response memset(input, 0, sizeof(input)); substring(input, data, inde1 + 1, inde2 - 1); trim(input); node->resLen = atoi(input); //userlog("Response len is:%d\n", node->resLen); int j = 0; for(j = 0; j < node->resLen; j++){ if(fgets(data, DATALEN - 1, file) != NULL){//循环读取Response的报文数据 inde1 = indexOf(data, ","); //inde2 = lastIndexOf(data, ","); end = indexOf(data, "\n"); int tag1 = indexOf(data, "code:"); memset(input, 0, sizeof(input)); substring(input, data, tag1 + 5, inde1); trim(input); strcpy(node->response[j].code, input); //userlog("%d res code is.%s.\n", j, node->response[j].code); int tag2 = indexOf(data, "data:"); memset(input, 0, sizeof(input)); //substring(input, data, tag2 + 5, inde2); substring(input, data, tag2 + 5, end - 1); trim(input); strcpy(node->response[j].data, input); //userlog("%d res data is.%s.\n", j, node->response[j].data); //int tag3 = indexOf(data, "type:"); //memset(input, 0, sizeof(input)); //substring(input, data, tag3 + 5, end -1); //trim(input); //strcpy(node->response[j].type, input); //userlog("%d res type is.%s.\n", j, node->response[j].type); } } } } head = appendNode(head, node); } } } //displayNode(head); if(head != NULL){ userlog("init MessageTableCopy.json success."); } return head; } } long getFid(char *fname); long getFid(char *fname){ int i = 0; userlog("fname is:%s.", fname); for(i = 0; i < k; i++){ //userlog("gather fname:%s.", gather[i].fname); int n = strcmp(gather[i].fname, fname); //userlog("n is:%d.", n); if(n == 0){ //userlog("fid:%d.", gather[i].fid); return gather[i].fid; } } return 0; } void analyFMLfile(); void analyFMLfile() { char data[STRLEN] = {0}; char num[20]; //域-域值对应表 FILE *file; file = fopen("clsinterface.h", "r"); if(file == NULL){ printf("fml file not exist.\n"); } else{ int j = 0; k = 0; while(j < ROW){ memset(data, 0, sizeof(data)); memset(num, 0, sizeof(num)); while(fgets(data, STRLEN, file) != NULL){ //printf("str is:%s", data); if(indexOf(data, "#de") == 0){ int inde1 = indexOf(data, "ne"); int inde2 = indexOf(data, "(("); substring(gather[k].fname, data, inde1 + 3, inde2); trim(gather[k].fname); //printf("fname is:%s.\n", gather[k].fname); inde1 = indexOf(data, "2)"); inde2 = lastIndexOf(data, ")"); substring(num, data, inde1 + 2, inde2); gather[k].fid = atol(num); //printf("fid is:%d.\n", gather[k].fid); //inde1 = lastIndexOf(data, "e:"); //inde2 = lastIndexOf(data, "*"); //substring(gath.ftype, data, inde1 + 3, inde2 - 1); //printf("fid is:%s.\n", gather[k].ftype); //mp[gath.fname] = gath.fid; //printf("fid is:%s.%d\n", gath.fname, gath.fid); k++; } } j++; } userlog("The count number of domain values is %d", k); } } /* tpsvrinit is executed when a server is booted, before it begins processing requests. It is not necessary to have this function. Also available is tpsvrdone (not used in this example), which is called at server shutdown time. */ msgCount *head = NULL;//创建头指针,储存报文信息 #if defined(__STDC__) || defined(__cplusplus) tpsvrinit(int argc, char *argv[]) #else tpsvrinit(argc, argv) int argc; char **argv; #endif { /* Some compilers warn if argc and argv aren't used. */ argc = argc; argv = argv; /* simpapp is non-transactional, so there is no need for tpsvrinit() to call tx_open() or tpopen(). However, if this code is modified to run in a Tuxedo group associated with a Resource Manager then either a call to tx_open() or a call to tpopen() must be inserted here. */ head = analyTuxedo(head);//初始化链表 analyFMLfile();//初始化域值对应表 /* userlog writes to the central TUXEDO message log */ userlog("Welcome to the simple server test"); return(0); } /* This function performs the actual service requested by the client. Its argument is a structure containing among other things a pointer to the data buffer, and the length of the data buffer. */ #ifdef __cplusplus extern "C" #endif void #if defined(__STDC__) || defined(__cplusplus) TOUPPER(TPSVCINFO *rqst) #else TOUPPER(rqst) TPSVCINFO *rqst; #endif { head = NULL; head = analyTuxedo(head);//初始化链表 if(head == NULL){ userlog("head is NULL."); } int ret; long rcvlen = rqst->len; char *rebuff; int relen; char *requestBuf; FBFR32 *senbuf; if((rebuff = (char *)malloc(rcvlen)) == NULL){ (void) fprintf(stderr,"Error allocating rebuff buffer\n"); return (1); } if((senbuf = (FBFR32 *)tpalloc("FML32", NULL, rcvlen)) == NULL){ free(rebuff); (void) fprintf(stderr,"Error allocating senbuf buffer\n"); return (1); } //copy data to fmlbuf //memcpy(fmlbuf, (FBFR32*)rqst->data, rqst->len); requestBuf = rqst->data; relen = 0; ret = Fget32((FBFR32 *)requestBuf, MSGTYPE, 0, (char *)rebuff, (FLDLEN32 *)&relen); if(ret == -1){ userlog("Fget32 failed with error: %s\n",Fstrerror(Ferror32)); }else{ int rebu = atoi(rebuff); struct msg *node = getNode(head, rebu); //获取对应报文的组包数据节点 if(node != NULL){ userlog("node not null."); int i = 0; for(i = 0; i < node->resLen; i++){ dataVolu res = node->response[i]; long fid = getFid(res.code); userlog("fid is:%d, res.data is %s.", fid, res.data); ret = Fchg32(senbuf, ((FLDID32)fid), 0, (char*)res.data, (FLDLEN32)0); /* if(indexOf(res.type, "long") >= 0){ long data = atol(res.data); //userlog("long data is %d.", data); ret = Fchg32(senbuf, ((FLDID32)fid), 0, (char*)&data, (FLDLEN32)0); }else if(indexOf(res.type, "double") >= 0){ double data = atof(res.data); ret = Fchg32(senbuf, ((FLDID32)fid), 0, (char*)&data, (FLDLEN32)0); }else{ ret = Fchg32(senbuf, ((FLDID32)fid), 0, (char*)res.data, (FLDLEN32)0); } */ } } } //userlog(Fprint((FBFR32*)senbuf)); //char * n = "调用成功"; //ret = Fchg32(senbuf, RESPINFO, 0, n, (FLDLEN32)0); /* Return the transformed buffer to the requestor. */ tpreturn(TPSUCCESS, 0, (char *)senbuf, 0L, 0); }
tuxedo的服务器,用于返回报文;解报文暂时没有增加;
由于cJSON不能加进去编译,修改了数据结构自己解数据;