Home | History | Annotate | Download | only in tirpc_simple_rpc_broadcast_exp
      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 PROCNUM 1
     41 #define VERSNUM 1
     42 
     43 static int *thread_array_result;
     44 int run_mode;
     45 int progNum;
     46 char *nettype;
     47 int callNb;
     48 
     49 int eachresult(char *out, struct sockaddr_in *addr)
     50 {
     51 	//Nothing to do for that test
     52 	return 1;
     53 }
     54 
     55 void *my_thread_process(void *arg)
     56 {
     57 	enum clnt_stat rslt;
     58 	int recVar;
     59 	int i;
     60 
     61 	int iTimeOut = 1;
     62 	int mTimeOut = 1;
     63 
     64 	if (run_mode == 1) {
     65 		fprintf(stderr, "Thread %ld\n", (long)arg);
     66 	}
     67 
     68 	for (i = 0; i < callNb; i++) {
     69 		rslt = rpc_broadcast_exp(progNum, VERSNUM, PROCNUM,
     70 					 (xdrproc_t) xdr_int, (char *)&recVar,
     71 					 (xdrproc_t) xdr_int, (char *)&recVar,
     72 					 (resultproc_t) eachresult, iTimeOut,
     73 					 mTimeOut, nettype);
     74 
     75 		thread_array_result[atoi(arg)] += (rslt == RPC_SUCCESS);
     76 	}
     77 
     78 	pthread_exit(0);
     79 }
     80 
     81 int main(int argn, char *argc[])
     82 {
     83 	//Program parameters : argc[1] : HostName or Host IP
     84 	//                                         argc[2] : Server Program Number
     85 	//                                         argc[3] : Number of threads
     86 	//                                         argc[4] : Number of calls per thread
     87 	//                                         other arguments depend on test case
     88 
     89 	//run_mode can switch into stand alone program or program launch by shell script
     90 	//1 : stand alone, debug mode, more screen information
     91 	//0 : launch by shell script as test case, only one printf -> result status
     92 	run_mode = 0;
     93 	int test_status = 1;	//Default test result set to FAILED
     94 	int threadNb = atoi(argc[3]);
     95 	long i;
     96 	pthread_t *pThreadArray;
     97 	void *ret;
     98 
     99 	nettype = "visible";
    100 	progNum = atoi(argc[2]);
    101 	callNb = atoi(argc[4]);
    102 
    103 	if (run_mode == 1) {
    104 		printf("Server #%d\n", progNum);
    105 		printf("Thread to create %d\n", threadNb);
    106 	}
    107 	//Initialization : create threads results array, init elements to 0
    108 	//Each thread will put function result (pas/fail) into array
    109 	thread_array_result = malloc(threadNb * sizeof(int));
    110 	memset(&thread_array_result[0], 0, threadNb * sizeof(int));
    111 
    112 	//Create all threads
    113 	//Run all threads
    114 	pThreadArray = malloc(threadNb * sizeof(pthread_t));
    115 	for (i = 0; i < threadNb; i++) {
    116 		if (run_mode == 1)
    117 			fprintf(stderr, "Try to create thread %ld\n", i);
    118 		if (pthread_create(&pThreadArray[i], NULL, my_thread_process, (void*)i)
    119 		    < 0) {
    120 			fprintf(stderr, "pthread_create error for thread 1\n");
    121 			exit(1);
    122 		}
    123 	}
    124 
    125 	//Clean threads
    126 	for (i = 0; i < threadNb; i++) {
    127 		(void)pthread_join(pThreadArray[i], &ret);
    128 	}
    129 
    130 	//Check if all threads results are ok
    131 	test_status = 0;
    132 	for (i = 0; i < threadNb; i++) {
    133 		if (thread_array_result[i] != callNb) {
    134 			test_status = 1;
    135 			break;
    136 		}
    137 	}
    138 
    139 	if (run_mode == 1) {
    140 		for (i = 0; i < threadNb; i++) {
    141 			fprintf(stderr, "Result[%ld]=%d\n", i,
    142 				thread_array_result[i]);
    143 		}
    144 	}
    145 	//This last printf gives the result status to the tests suite
    146 	//normally should be 0: test has passed or 1: test has failed
    147 	printf("%d\n", test_status);
    148 
    149 	return test_status;
    150 }
    151