Tuxedo system classic-jing Qian 9.6 TUXEDO/Q for reliable data transmission source code

Source: Internet
Author: User

Tuxedo system classic-jing Qian 9.6 TUXEDO/Q provides reliable source code for data transmission. The Code has been verified and run in Linux. There are not many articles related to TUXEDO middleware in China, this article is only intended for communication between tuxedo and colleagues who are about to learn tuxedo.

Client Program

F_send.c

# Include <stdlib. h> # include <stdio. h> # include <string. h> # include <sys/STAT. h> # include <sys/types. h> # include <fcntl. h> # include <unistd. h> # include "atmi. H "# include" userlog. H "# include" fml32.h "# include" ftpfml. H "# define header_len 32 # define f_blocksize 128 # define f_source_dir"/ngbss/tuxapp/src/Chapter 09/"char * data_buf; fbfr32 * desc_buf; int send_file (char * f_name ); int main (INT argc, char * argv []) {If (argc! = 2) {printf ("useage: commond filename \ n"); Return-1;} If (tpinit *) null) =-1) {fprintf (stderr, "tpinit failed! \ N "); Return-1;} If (data_buf = tpalloc (" carray ", null, f_blocksize + header_len) = NULL) {fprintf (stderr, "allocate carray buffer failed! \ N "); tpterm (); Return-1;} If (desc_buf = (fbfr32 *) tpalloc (" fml32 ", null, 1024) = NULL) {fprintf (stderr, "allocate fml32 buffer failed! \ N "); tpfree (data_buf); tpterm (); Return-1;} // send the file send_file (argv [1]); Return 0 ;} int send_file (char * f_name) {int FD, n_byte; char sitename [16], head_c [32] = "\ 0"; long startcid, head_l; long blockcnt, rcv_len; struct stat f_stat; char c_buf [f_blocksize]; tpqctl qctl; qctl. flags = tpqdeliveryqos; qctl. delivery_qos = tpqqosnonpersistent; // qctl. flags = tpqreplyq; // (void) strcpy (qctl. replyqueue, "replyq"); If (FD = open (f_name, O_binary | o_rdonly) <0) {printf ("Open File: % s error! \ N ", f_name); Return-1 ;}// calculate the number of file shards fstat (FD, & f_stat); blockcnt = f_stat.st_size/f_blocksize; If (f_stat.st_size % f_blocksize! = 0) blockcnt ++; sprintf (data_buf, "% d", blockcnt); If (tpcall ("common_op", data_buf, strlen (data_buf) + 1, & data_buf, & rcv_len, 0) =-1) {userlog ("common_op failed, % s! \ N ", tpstrerror (tperrno); close (FD); Return-1;} sscanf (data_buf," % S % d ", sitename, & startcid ); fchg32 (desc_buf, filename, 0, f_name, 0); fchg32 (desc_buf, sitename, 0, sitename, 0); fchg32 (desc_buf, blockcnt, 0, (char *) & blockcnt, 0); fchg32 (desc_buf, startcid, 0, (char *) & startcid, 0); printf ("info: \ tfilename: % s \ tblockcnt: % d \ n ", f_name, blockcnt); // memset (data_buf,'', header_len + f_blocksize); While (n_byte = read (FD, c_buf, f_blocksize)> 0) {printf ("n_byte = % d \ n", n_byte); // determine the message header sprintf (head_c, "% S % d", sitename, startcid ); head_l = strlen (head_c); memcpy (data_buf, head_c, head_l); // copy the content memcpy (data_buf + header_len, c_buf, n_byte); printf ("% s ", data_buf + header_len); printf ("header_len + n_byte = % d \ n", header_len + n_byte); // put in the sending queue if (tpenqueue ("qspace", "sendq ", & qctl, data_buf, header_len + n_byte, 0) =-1) {fprintf (stderr, "enqueue sendq failed, % s \ ndiagnostic: % d \ n ", tpstrerror (tperrno), qctl. diagnostic); break;} startcid ++;} Close (FD); // send notification message // printf ("Send End message \ n "); if (tpenqueue ("qspace", "sendq", & qctl, (char *) desc_buf, 0, 0) =-1) {fprintf (stderr, "snedq is full, % s \ ndiagnostic: % d \ n ", tpstrerror (tperrno), qctl. diagnostic); tpfree (data_buf); tpfree (char *) desc_buf); tpterm (); Return-1;} return 0 ;}

Server programs:

Common_op.c

#include<Uunix.h>#include<string.h>#include"atmi.h"#include"userlog.h"long currid=0;char sitename[16];void COMMON_OP(TPSVCINFO *rqst){strcpy(sitename,"site1");sprintf(rqst->data,"%s %d",sitename,currid);userlog("common_op:%s\n",rqst->data);tpreturn(TPSUCCESS,0,rqst->data,strlen(rqst->data)+1,0);}

B _recv.c

# Include <string. h> # include <stdlib. h> # include "atmi. H "# include" userlog. H "Void sendq (tpsvcinfo * rqst) {userlog (" Enter sendq -- \ n "); tpqctl qctl; char type [20], subtype [20], f_name [20]; long head_l, blockcnt; fbfr32 * rbuf = (fbfr32 *) rqst-> data; tptypes (rqst-> data, type, subtype); If (strcmp (type, "fml32 ") = 0) {// userlog ("received filearrived -- \ n"); tppost ("filearrived", rqst-> data, 0, tpsigrstrt ); tpreturn (tpsuccess, 0, null, 0, 0);} else {userlog ("tpenqueuing -- \ n"); qctl. flags = tpqcorrid; memset (qctl. corrid, 0, sizeof (qctl. corrid); strcpy (qctl. corrid, rqst-> data); // strncpyuserlog ("qctl. corrid: % s \ n ", qctl. corrid); qctl. flags | = tpqdeliveryqos; qctl. delivery_qos = tpqqosnonpersistent; userlog ("rqst-> Len = % d \ t strlen (rqst-> data) = % d \ n", rqst-> Len, strlen (rqst-> data); If (tpenqueue ("qspace", "recvq", & qctl, rqst-> data, rqst-> Len, 0) =-1) {userlog ("tpenqueue recvq failed, % s \ n", tpstrerror (tperrno); tpreturn (tpfail, 0, null );}} tpreturn (tpsuccess, 0, null, 0, 0 );}

F_recv.c

#include<stdlib.h>#include<string.h>#include"atmi.h"#include"userlog.h"#include"ftpfml.h"#include<fcntl.h>#define HEADER_LEN 32  //==tpqctl.corrid[32]#define F_STEP 128#define F_RECV_PATH "/ngbss/tuxapp/src/chapter09/Recv"long sub_handle;char *tux_buf;int tpsvrinit(int argc,char *argv[]){if(tx_open()==-1){userlog("tpsvrinit: failed to open database");return -1;}TPEVCTL ectl;ectl.flags=TPEVSERVICE;strcpy(ectl.name1,"F_RECV");if((sub_handle=tpsubscribe("FileArrived",NULL,&ectl,TPSIGRSTRT))==-1){userlog("tpsubscribe() failed,%s\n",tpstrerror(tperrno));return -1;}if((tux_buf=tpalloc("CARRAY",NULL,HEADER_LEN+F_STEP))==NULL){userlog("Can`t allocate buffer CARRAY,%s\n",tpstrerror(tperrno));return -1;}return 0;}void F_RECV(TPSVCINFO *rqst){int fd,n_byte;long fn_len,sn_len,blockcnt,startcid,buflen;char c_buf[F_STEP],f_name[20],ff_name[80],s_name[20];TPQCTL qctl;FBFR32 *rbuf=(FBFR32*)rqst->data;qctl.flags=TPQWAIT|TPQGETBYCORRID;qctl.flags|=TPQREPLYQOS;qctl.reply_qos=TPQQOSNONPERSISTENT;fn_len=sizeof(f_name);sn_len=sizeof(s_name);Fget32(rbuf,FILENAME,0,f_name,&fn_len);Fget32(rbuf,SITENAME,0,s_name,&sn_len);Fget32(rbuf,BLOCKCNT,0,(char*)&blockcnt,0);Fget32(rbuf,STARTCID,0,(char*)&startcid,0);sprintf(ff_name,"%s%s",F_RECV_PATH,f_name);userlog("FileName:%s\n",ff_name);if((fd=open(ff_name,O_BINARY|O_RDWR|O_CREAT))<0){userlog("Open file error,%s\n",tpstrerror(tperrno));tpreturn(TPFAIL,0,NULL,0,0);}memset(qctl.corrid,0,sizeof(qctl.corrid));while(blockcnt-- > 0){sprintf(qctl.corrid,"%s %d",s_name,startcid++);userlog("F_RECV:\t qctl.corrid:%s\n",qctl.corrid);userlog("Begining dequeue....\n");if(tpdequeue("QSPACE","RECVQ",&qctl,(char**)&tux_buf,&buflen,0)==-1){userlog("tpdequeue failed,%s\n",tpstrerror(tperrno));break;}//userlog("tux_buf:%s",tux_buf+HEADER_LEN);userlog("buflen=%d\t buflen-HEADER_LEN=%d\n",buflen,buflen-HEADER_LEN);if((n_byte=write(fd,tux_buf+HEADER_LEN,buflen-HEADER_LEN))<0){close(fd);userlog("write to file :%s error!\n",ff_name);tpreturn(TPFAIL,0,NULL,0,0);}}close(fd);tpreturn(TPSUCCESS,0,NULL,0,0);}void tpsvrdone(){tpunsubscribe(sub_handle,0);tpfree(tux_buf);}

Message Queue creation script:

crdl /ngbss/tuxapp/etc/QUE 0 3000qspacecreate -n 1000 QSPACE 67775 2000 6 4 9 1000 errque y 16qopen QSPACEqcreate SENDQ fifo none 2 30 80% 0% echo "STRING is nearly full"qcreate RECVQ fifo none 2 30 80% 0% echo "RPLYQ is nearly full"qcreate errque fifo none 2 30 80% 0% echo "errque is nearly full"quit

 

UBB Configuration:

*GROUPSCHP9GRPLMID=site1GRPNO=309TMSNAME=TMS_QMTMSCOUNT=2OPENINFO="TUXEDO/QM:/ngbss/tuxapp/etc/QUE:QSPACE"CHP9GRP_1LMID=site1GRPNO=310TMSNAME=TMSTMSCOUNT=2*SERVERS#add for chapter09commonopserverSRVGRP=CHP9GRP_1SRVID=4046brecvserverSRVGRP=CHP9GRP_1SRVID=4048frecvserverSRVGRP=CHP9GRP_1SRVID=4050TMUSREVTSRVGRP=CHP9GRP_1  SRVID=4044CLOPT="-A -- -p 10"TMQUEUESRVGRP=CHP9GRP SRVID=4040 CLOPT="-s QSPACE:TMQUEUE -- -t 60"TMQFORWARDSRVGRP=CHP9GRP SRVID=4042 CLOPT="-- -q SENDQ"

Running result:

./F_send file. Data

[tuxapp@REDHAT5_FG chapter09]$ ./f_send file.dataINFO:   filename:file.data      blockcnt:4n_byte=128ddddddddddddddddddddddddddddddddttttttttttttttttttttttttttttttttffffffffffffffffffffffffffffffffdddddddddddddddddddddddddddddHEADER_LEN+n_byte=160n_byte=128dddttttttttttttttttttttttttttttttttffffffffffffffffffffffffffffffffddddddddddddddddddddddddddddddddtttttttttttttttttttttttttHEADER_LEN+n_byte=160n_byte=128tttttttffffffffffffffffffffffffffffffffddddddddddddddddddddddddddddddddttttttttttttttttttttttttttttttttfffffffffffffffffffffHEADER_LEN+n_byte=160n_byte=111fffffffffffddddddddddddddddddddddddddddddddttttttttttttttttttttttttttttttttfffffffffffffffffffffffffffffffffffffffffffffffffHEADER_LEN+n_byte=143

Server ulog. xxx

185356.REDHAT5_FG!commonopserver.3785.3086702800.0: common_op:site1 0185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x85:        Enter SENDQ--185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x85:        Tpenqueuing -- 185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x85:        qctl.corrid:site1 0185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x85:        rqst->len=160    strlen(rqst->data)=7185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x86:        Enter SENDQ--185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x86:        Tpenqueuing -- 185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x86:        qctl.corrid:site1 1185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x86:        rqst->len=160    strlen(rqst->data)=7185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x87:        Enter SENDQ--185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x87:        Tpenqueuing -- 185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x87:        qctl.corrid:site1 2185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x87:        rqst->len=160    strlen(rqst->data)=7185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x88:        Enter SENDQ--185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x88:        Tpenqueuing -- 185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x88:        qctl.corrid:site1 3185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x88:        rqst->len=143    strlen(rqst->data)=7185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x89:        Enter SENDQ--185419.REDHAT5_FG!brecvserver.3786.3086985424.0: gtrid x0 x51b2801b x89:        Received FileArrived--185419.REDHAT5_FG!frecvserver.3799.3086547152.0: FileName:/ngbss/tuxapp/src/chapter09/Recvfile.data185419.REDHAT5_FG!frecvserver.3799.3086547152.0: F_RECV:         qctl.corrid:site1 0185419.REDHAT5_FG!frecvserver.3799.3086547152.0: Begining dequeue....185419.REDHAT5_FG!frecvserver.3799.3086547152.0: buflen=160      buflen-HEADER_LEN=128185419.REDHAT5_FG!frecvserver.3799.3086547152.0: F_RECV:         qctl.corrid:site1 1185419.REDHAT5_FG!frecvserver.3799.3086547152.0: Begining dequeue....185419.REDHAT5_FG!frecvserver.3799.3086547152.0: buflen=160      buflen-HEADER_LEN=128185419.REDHAT5_FG!frecvserver.3799.3086547152.0: F_RECV:         qctl.corrid:site1 2185419.REDHAT5_FG!frecvserver.3799.3086547152.0: Begining dequeue....185419.REDHAT5_FG!frecvserver.3799.3086547152.0: buflen=160      buflen-HEADER_LEN=128185419.REDHAT5_FG!frecvserver.3799.3086547152.0: F_RECV:         qctl.corrid:site1 3185419.REDHAT5_FG!frecvserver.3799.3086547152.0: Begining dequeue....185419.REDHAT5_FG!frecvserver.3799.3086547152.0: buflen=143      buflen-HEADER_LEN=111

The client and server files are exactly the same.

 

Note: This article is for my own Study and Preparation. It is only used for learning and communication. repost and indicate the source. Thank you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.