summaryrefslogtreecommitdiff
path: root/test/xa/src2/client.c
blob: eb0e75b0eba695fd60242f10a7e0c802b074df74 (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2011, 2015 Oracle and/or its affiliates.  All rights reserved.
 */

/*
 * This client is part of Test 2 of the XA test suite.  It calls bdb1, which
 * in turn calls bdb2.  In the test several clients are executed at once to test
 * how XA performs with multiple processes.
 */

#include <stdio.h>
#include <string.h>
#include "atmi.h"		/* TUXEDO  Header File */
#include "../utilities/bdb_xa_util.h"


void my_exit();
char *sendbuf, *rcvbuf;

void
my_exit(string)
char *string;
{
	tpfree(sendbuf);
	tpfree(rcvbuf);
	fprintf(stderr,"SIMPAPP:ERROR call to service %s failed \n",string);
	tpterm();
	exit(1);
}

int
main(argc, argv)
int argc;
char *argv[];
{

	extern char *optarg;
	char * msg = NULL;
	TPQCTL qctl;	/* Q control structures */
	long sendlen=10, rcvlen=10, len;
	int ret;
	char ch;
	char * service;
	int times = 1;
	int i;
	int delete = 0;
	int need_print = 0;
	int nostop =0;

	if(argc < 5) {
		fprintf(stderr, 
		    "Usage: %s -s service -m messgae [-t times][-d][-p]\n",
		    argv[0] );
		exit(1);
	}

	while ((ch = getopt(argc, argv, "s:m:t:dpn")) != EOF)
		switch (ch) {
		case 's':
			service = optarg;
			break;
		case 'm':
			msg = optarg;
			break;
		case 't':
			times = atoi(optarg);
			break;
		case 'd':
			delete = 1;
			break;
		case 'n':
			nostop = 1;
			break;
		}
	
	/* Attach to System/T as a Client Process */
	if (tpinit((TPINIT *) NULL) == -1) {
		userlog("SIMPAPP:ERROR  tpinit failed \n");
		userlog("SIMPAPP:SYSTEM ERROR %s \n", tpstrerror(tperrno));
		fprintf(stderr, "Simpapp:ERROR Tpinit failed\n");
		my_exit("tpinit");
	}
	
	if(msg){
		sendlen = strlen(msg);
	}

	/* Allocate STRING buffers for the request and the reply */

	if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
		fprintf(stderr,"Simpapp:ERRROR Allocating send buffer\n");
		tpterm();
		my_exit("tpalloc");
	}

	if((rcvbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
		fprintf(stderr,"Simpapp:ERROR allocating receive buffer\n");
		tpfree(sendbuf);
		tpterm();
		my_exit("tpalloc");
	}

		strcpy(sendbuf, msg);

	for(i=0; i<times; i++){
		ret = tpcall(service, sendbuf, 0, &rcvbuf, &rcvlen,(long)0);
		if(ret == -1) {
			fprintf(stderr,"Simapp:ERROR tpacall to service %s \n", 
			    service);
			userlog("SIMPAPP:SYSTEM ERROR %s \n", 
			    tpstrerror(tperrno));
			if(nostop && (tperrno != TPENOENT))
				continue;
			else
				my_exit("WRITE");
		}

		if(verbose) printf("Simpapp: Returned string from : %s\n", 
		    rcvbuf);
	
		if(!delete) continue;

		memset(&qctl, '\0', sizeof(qctl));
		if (tpdequeue("QSPACE", "MYQUE", (TPQCTL *)&qctl, 
		    (char **)&rcvbuf, &len, TPNOTIME) == -1) 
		{
			userlog("QM diagnostic %ld\n", qctl.diagnostic);
			my_exit("queue");
	        }
		else if(verbose){
			printf("Simpapp: get string from queue : %s\n", rcvbuf);
		}
	}

	/* Free Buffers & Detach from System/T */
	tpfree(sendbuf);
	tpfree(rcvbuf);
	tpterm();
	exit(0);
}