Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / int / net / rpc / srvr.c
1 #include <stdio.h>
2 #include <rpc/rpc.h>
3 #include "srvr.h"
4
5 void dispatch();
6
7 #define MAXPROCS 64
8
9 #define disable(p) (mask&=~(1<<(sockets[p])))
10 #define enable(p)  (mask|= (1<<(sockets[p])))
11 int sockets[MAXPROCS];
12 unsigned int mask=0xffffffff;
13
14
15 main(){
16    SVCXPRT *transp;
17
18    int i;
19    for(i=0;i<MAXPROCS;i++) sockets[i]=-1;
20
21    transp = svcudp_create(RPC_ANYSOCK);
22    if( transp == NULL ){
23       fprintf(stderr,"could not create an RPC1 server\n");
24       exit(1);
25    }
26    pmap_unset(SRVRPROG,SRVRVERS);
27    if(!svc_register(transp,SRVRPROG,SRVRVERS,dispatch,IPPROTO_UDP)){
28       fprintf(stderr,"could not register service 1\n");
29       exit(1);
30    }
31    sockets[0]=transp->xp_sock;
32
33    transp = svcudp_create(RPC_ANYSOCK);
34    if( transp == NULL ){
35       fprintf(stderr,"could not create an RPC2 server\n");
36       exit(1);
37    }
38    pmap_unset(SRVRPROG,SRVRVERS+1);
39    if(!svc_register(transp,SRVRPROG,SRVRVERS+1,dispatch,IPPROTO_UDP)){
40       fprintf(stderr,"could not register service 2\n");
41       exit(1);
42    }
43    sockets[1]=transp->xp_sock;
44
45    disable(1);
46
47    svc_run();
48    fprintf(stderr,"should never reach this point!\n");
49 }
50
51 void dispatch(rqstp,transp) struct svc_req *rqstp; SVCXPRT *transp; {
52    char *s=NULL;
53    static int sem=0;
54    static int cnt=0;
55    switch( rqstp->rq_proc ){
56
57       case NULLPROC:
58          printf("nullproc\n");
59          if(!svc_sendreply(transp,xdr_void,0)){
60             fprintf(stderr,"could not reply to RPC NULL call\n");
61             exit(1);
62          }
63          return;
64
65       case RENDERSTR:
66          sem++;
67          if(sem>1){ printf("ERROR sem=%d\n",sem); exit(1); }
68          enable(0);
69          if(cnt>50) enable(1);
70          if(!svc_getargs(transp,xdr_wrapstring,&s)){
71             fprintf(stderr,"could not decode arguments in RPC render call\n");
72             exit(1);
73          }
74          printf("got:%s\n",s);
75          if(!svc_sendreply(transp,xdr_void,0)){
76             fprintf(stderr,"could not reply to RPC render call\n");
77             exit(1);
78          }
79          if(rqstp->rq_vers-SRVRVERS==1)
80             if((cnt%9)==2)
81                disable(0);
82          cnt++;
83          sem--;
84          break;
85
86       case SRVR_END:
87          if(!svc_sendreply(transp,xdr_void,0)){
88             fprintf(stderr,"could not reply to RPC END call\n");
89             exit(1);
90          }
91          svc_unregister(SRVRPROG,rqstp->rq_vers);
92          svc_destroy(transp);
93          printf("server %d closed\n",rqstp->rq_vers-SRVRVERS);
94
95       default :
96          svcerr_noproc(transp);
97          return;
98
99    }
100    svc_freeargs(transp,xdr_wrapstring,&s);
101 }
102
103
104 void svc_run()
105 {
106    int readfds;
107    for(;;){
108       readfds=svc_fds&mask;
109 /*      printf("sel:svc_fds=%x\n",svc_fds);*/
110       if( svc_fds == 0 ){
111          printf("server has no services - closing\n");
112          exit(0);
113       }
114       switch( select(32,&readfds,NULL,NULL,NULL) ){
115
116       case -1: perror("rstat: select");
117                return;
118
119       case 0:  break;
120
121       default:
122 /*             printf(" req:svc_fds=%x\n",readfds);*/
123                svc_getreq(readfds);
124       }
125    }
126 }
127