Home | History | Annotate | Download | only in rpc_svc_2
      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 <rpc/rpc.h>
     30 #include <utmp.h>
     31 
     32 //Standard define
     33 #define VERSNUM 1
     34 //Complex procs
     35 #define CALCPROC   10000
     36 
     37 void rcp_service(register struct svc_req *rqstp, register SVCXPRT * transp);
     38 
     39 struct datas {
     40 	double a;
     41 	double b;
     42 	double c;
     43 } argument;
     44 
     45 //XDR Struct function
     46 bool_t xdr_datas(XDR * pt_xdr, struct datas *pt)
     47 {
     48 	return (xdr_double(pt_xdr, &(pt->a)) &&
     49 		xdr_double(pt_xdr, &(pt->b)) && xdr_double(pt_xdr, &(pt->c)));
     50 }
     51 
     52 //****************************************//
     53 //***           Main Function          ***//
     54 //****************************************//
     55 int main(int argn, char *argc[])
     56 {
     57 	//Server parameter is : argc[1] : Server Program Number
     58 	//                                          others arguments depend on server program
     59 	int run_mode = 1;
     60 	int progNum = atoi(argc[1]);
     61 	SVCXPRT *transpTCP = NULL;
     62 	SVCXPRT *transpUDP = NULL;
     63 	//char *simplePing();
     64 
     65 	//Initialization
     66 	pmap_unset(progNum, VERSNUM);
     67 	svc_unregister(progNum, VERSNUM);
     68 
     69 	//registerrpc(progNum, VERSNUM, PROCSIMPLEPING,
     70 	//                  simplePing, xdr_int, xdr_int);
     71 	transpTCP = svctcp_create(RPC_ANYSOCK, 1000, 1000);
     72 	transpUDP = svcudp_create(RPC_ANYSOCK);
     73 
     74 	if (run_mode) {
     75 		printf("SVC TCP : %p\n", transpTCP);
     76 		printf("SVC UDP : %p\n", transpUDP);
     77 	}
     78 
     79 	if (!svc_register
     80 	    (transpTCP, progNum, VERSNUM, (void *)rcp_service, IPPROTO_TCP)) {
     81 		fprintf(stderr, "svc_register: error (TCP)\n");
     82 	}
     83 
     84 	if (!svc_register
     85 	    (transpUDP, progNum, VERSNUM, (void *)rcp_service, IPPROTO_UDP)) {
     86 		fprintf(stderr, "svc_register: error (UDP)\n");
     87 	}
     88 
     89 	svc_run();
     90 	fprintf(stderr, "Error: svc_run returned!\n");
     91 	exit(1);
     92 }
     93 
     94 //****************************************//
     95 //***        Remotes Procedures        ***//
     96 //****************************************//
     97 
     98 char *calcProc(struct datas *dt, SVCXPRT * svc)
     99 {
    100 	//Makes a + b * c from structure dt and returns double
    101 	//printf("*** In calcProc ***\n");
    102 	static double result = 0;
    103 	result = dt->a + (dt->b * dt->c);
    104 	//printf("Received : %lf, %lf, %lf\n", dt->a, dt->b, dt->c);
    105 	return (char *)&result;
    106 }
    107 
    108 //****************************************//
    109 //***       Dispatch Function          ***//
    110 //****************************************//
    111 void rcp_service(register struct svc_req *rqstp, register SVCXPRT * transp)
    112 {
    113 	//printf("* in Dispatch Func.\n");
    114 
    115 	char *result;
    116 	xdrproc_t xdr_argument;
    117 	xdrproc_t xdr_result;
    118 	char *(*proc) (struct datas *, SVCXPRT *);
    119 
    120 	switch (rqstp->rq_proc) {
    121 	case CALCPROC:
    122 		{
    123 			//printf("** in CALCPROC dispatch Func.\n");
    124 			xdr_argument = (xdrproc_t) xdr_datas;
    125 			xdr_result = (xdrproc_t) xdr_double;
    126 			proc = (char *(*)(struct datas *, SVCXPRT *))calcProc;
    127 			break;
    128 		}
    129 	default:
    130 		{
    131 			//printf("** in NOT DEFINED dispatch Func.\n");
    132 			//Proc is unavaible
    133 			svcerr_noproc(transp);
    134 			return;
    135 		}
    136 	}
    137 
    138 	memset((char *)&argument, (int)0, sizeof(argument));
    139 	if (svc_getargs(transp, xdr_argument, (char *)&argument) == FALSE) {
    140 		svcerr_decode(transp);
    141 		return;
    142 	}
    143 
    144 	result = (char *)(*proc) ((struct datas *)&argument, transp);
    145 
    146 	if ((result != NULL)
    147 	    && (svc_sendreply(transp, xdr_result, result) == FALSE)) {
    148 		svcerr_systemerr(transp);
    149 	}
    150 	if (svc_freeargs(transp, xdr_argument, (char *)&argument) == FALSE) {
    151 		(void)fprintf(stderr, "unable to free arguments\n");
    152 		exit(1);
    153 	}
    154 }
    155