Home | History | Annotate | Download | only in tirpc_bottomlevel_clnt_call
      1 /*
      2 * Copyright (c) Bull S.A.  2007 All Rights Reserved.
      3 *
      4 * This program is free software; you can redistribute it and/or modify it
      5 * under the terms of version 2 of the GNU General Public License as
      6 * published by the Free Software Foundation.
      7 *
      8 * This program is distributed in the hope that it would be useful, but
      9 * WITHOUT ANY WARRANTY; without even the implied warranty of
     10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11 *
     12 * Further, this software is distributed without any warranty that it is
     13 * free of the rightful claim of any third person regarding infringement
     14 * or the like.  Any license provided herein, whether implied or
     15 * otherwise, applies only to this software file.  Patent licenses, if
     16 * any, provided herein do not apply to combinations of this program with
     17 * other software, or any other product whatsoever.
     18 *
     19 * You should have received a copy of the GNU General Public License along
     20 * with this program; if not, write the Free Software Foundation, Inc.,
     21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     22 *
     23 * History:
     24 * Created by: Cyril Lacabanne (Cyril.Lacabanne (at) bull.net)
     25 *
     26 */
     27 
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <time.h>
     31 #include <pthread.h>
     32 #include <tirpc/netconfig.h>
     33 #include <tirpc/rpc/rpc.h>
     34 #include <tirpc/rpc/types.h>
     35 #include <tirpc/rpc/xdr.h>
     36 #include <tirpc/rpc/svc.h>
     37 #include <errno.h>
     38 
     39 //Standard define
     40 #define CALCTHREADPROC	1000
     41 #define VERSNUM 1
     42 
     43 #define ADDRBUFSIZE 100
     44 
     45 static int *thread_array_result;
     46 int run_mode;
     47 int progNum;
     48 char *hostname;
     49 char *nettype;
     50 
     51 struct RES {
     52 	double locRes;
     53 	double svcRes;
     54 };
     55 
     56 struct RES *resTbl;
     57 
     58 struct datas {
     59 	double a;
     60 	double b;
     61 	double c;
     62 };
     63 
     64 bool_t xdr_datas(XDR * pt_xdr, struct datas *pt)
     65 {
     66 	return (xdr_double(pt_xdr, &(pt->a)) &&
     67 		xdr_double(pt_xdr, &(pt->b)) && xdr_double(pt_xdr, &(pt->c)));
     68 }
     69 
     70 double getRand(void)
     71 {
     72 	return (drand48() * 1000);
     73 }
     74 
     75 void *my_thread_process(void *arg)
     76 {
     77 	CLIENT *clnt = NULL;
     78 	struct datas vars;
     79 	struct timeval total_timeout;
     80 	struct netconfig *nconf = NULL;
     81 	struct netbuf svcaddr;
     82 	char addrbuf[ADDRBUFSIZE];
     83 
     84 	total_timeout.tv_sec = 1;
     85 	total_timeout.tv_usec = 1;
     86 
     87 	nconf = getnetconfigent("udp");
     88 
     89 	if ((struct netconfig *)nconf == NULL) {
     90 		//Test failed
     91 		printf("5\n");
     92 		pthread_exit((void*)5l);
     93 	}
     94 
     95 	svcaddr.len = 0;
     96 	svcaddr.maxlen = ADDRBUFSIZE;
     97 	svcaddr.buf = addrbuf;
     98 
     99 	if (svcaddr.buf == NULL) {
    100 		printf("5\n");
    101 		pthread_exit((void*)5l);
    102 	}
    103 
    104 	if (!rpcb_getaddr(progNum, VERSNUM, nconf, &svcaddr, hostname)) {
    105 		fprintf(stderr, "rpcb_getaddr failed!!\n");
    106 		printf("5\n");
    107 		pthread_exit((void*)5l);
    108 	}
    109 
    110 	clnt = clnt_dg_create(RPC_ANYFD, &svcaddr,
    111 			      progNum, VERSNUM, 1024, 1024);
    112 
    113 	if (clnt == NULL) {
    114 		printf("5\n");
    115 		pthread_exit((void*)5l);
    116 	}
    117 
    118 	if (run_mode == 1) {
    119 		fprintf(stderr, "Thread %ld\n", (long)arg);
    120 	}
    121 
    122 	vars.a = getRand();
    123 	vars.b = getRand();
    124 	vars.c = getRand();
    125 
    126 	resTbl[atoi(arg)].locRes = vars.a + (vars.b * vars.c);
    127 
    128 	clnt_call((CLIENT *) clnt, CALCTHREADPROC, (xdrproc_t) xdr_datas, (char *)&vars,	// xdr_in
    129 		  (xdrproc_t) xdr_double, (char *)&resTbl[atoi(arg)].svcRes,	// xdr_out
    130 		  total_timeout);
    131 
    132 	thread_array_result[atoi(arg)] =
    133 	    (resTbl[atoi(arg)].svcRes == resTbl[atoi(arg)].locRes) ? 0 : 1;
    134 
    135 	if (run_mode == 1) {
    136 		fprintf(stderr, "Thread #%d calc : %lf, received : %lf\n",
    137 			atoi(arg), resTbl[atoi(arg)].locRes,
    138 			resTbl[atoi(arg)].svcRes);
    139 	}
    140 
    141 	pthread_exit(0);
    142 }
    143 
    144 int main(int argn, char *argc[])
    145 {
    146 	//Program parameters : argc[1] : HostName or Host IP
    147 	//                                         argc[2] : Server Program Number
    148 	//                                         argc[3] : Number of threads
    149 	//                                         other arguments depend on test case
    150 
    151 	//run_mode can switch into stand alone program or program launch by shell script
    152 	//1 : stand alone, debug mode, more screen information
    153 	//0 : launch by shell script as test case, only one printf -> result status
    154 	run_mode = 0;
    155 	int test_status = 0;	//Default test result set to FAILED
    156 	int threadNb = atoi(argc[3]);
    157 	long i;
    158 	pthread_t *pThreadArray;
    159 	void *ret;
    160 
    161 	hostname = argc[1];
    162 	nettype = "VISIBLE";
    163 
    164 	resTbl = malloc(threadNb * sizeof(struct RES));
    165 
    166 	progNum = atoi(argc[2]);
    167 
    168 	if (run_mode == 1) {
    169 		printf("Server #%d\n", progNum);
    170 		printf("Thread to create %d\n", threadNb);
    171 	}
    172 	//Initialization : create threads results array, init elements to 0
    173 	//Each thread will put function result (pas/fail) into array
    174 	thread_array_result = malloc(threadNb * sizeof(int));
    175 
    176 	//Create all threads
    177 	//Run all threads
    178 	pThreadArray = malloc(threadNb * sizeof(pthread_t));
    179 	for (i = 0; i < threadNb; i++) {
    180 		thread_array_result[i] = 1;
    181 		if (run_mode == 1)
    182 			fprintf(stderr, "Try to create thread %ld\n", i);
    183 		if (pthread_create(&pThreadArray[i], NULL, my_thread_process, (void*)i)
    184 		    < 0) {
    185 			fprintf(stderr, "pthread_create error for thread 1\n");
    186 			exit(1);
    187 		}
    188 	}
    189 
    190 	//Clean threads
    191 	for (i = 0; i < threadNb; i++) {
    192 		(void)pthread_join(pThreadArray[i], &ret);
    193 	}
    194 
    195 	//Check if all threads results are ok
    196 	test_status = 0;
    197 	for (i = 0; i < threadNb; i++) {
    198 		if (thread_array_result[i] != 0) {
    199 			test_status = 1;
    200 			break;
    201 		}
    202 	}
    203 
    204 	if (run_mode == 1) {
    205 		for (i = 0; i < threadNb; i++) {
    206 			fprintf(stderr, "Result[%ld]=%d\n", i,
    207 				thread_array_result[i]);
    208 		}
    209 	}
    210 	//This last printf gives the result status to the tests suite
    211 	//normally should be 0: test has passed or 1: test has failed
    212 	printf("%d\n", test_status);
    213 
    214 	return test_status;
    215 }
    216