Project

General

Profile

Bug #8250 ยป test.c

Marcel Telka, 2017-05-18 04:40 PM

 
1
#include <stdio.h>
2
#include <rpc/rpc.h>
3
#include <unistd.h>
4

    
5
void
6
dispatch(struct svc_req *req, SVCXPRT *xprt)
7
{
8
	int i;
9

    
10
	if (req->rq_proc != 1) {
11
		svcerr_noproc(xprt);
12
		return;
13
	}
14

    
15
	if (!svc_getargs(xprt, xdr_int, (char *)&i)) {
16
		svcerr_decode(xprt);
17
		return;
18
	}
19

    
20
	printf("Server received: %d\n", i);
21

    
22
	if (!svc_sendreply(xprt, xdr_void, NULL))
23
		svcerr_systemerr(xprt);
24
}
25

    
26

    
27
void
28
testsend(CLIENT *clnt)
29
{
30
	int n = 12345678;
31
	enum clnt_stat stat;
32
	struct timeval tout = {0};
33

    
34
	stat = clnt_call(clnt, 1, xdr_int, (char *)&n, xdr_void, NULL, tout);
35
	if (stat != RPC_SUCCESS) {
36
		clnt_perror(clnt, "clnt_send #1 failed");
37
		return;
38
	}
39

    
40
	stat = clnt_call(clnt, 1, xdr_void, NULL, xdr_void, NULL, tout);
41
	if (stat != RPC_SUCCESS) {
42
		clnt_perror(clnt, "clnt_send #2 failed");
43
		return;
44
	}
45
}
46

    
47
void *
48
run_door_server(void *d)
49
{
50
	if (svc_door_create(dispatch, 567, 8, 0) == NULL) {
51
		printf("svc_door_create failed\n");
52
		return NULL;
53
	}
54

    
55
	svc_run();
56

    
57
	return NULL;
58
}
59

    
60
int
61
main(void)
62
{
63
	SVCXPRT *svc = svc_raw_create();
64
	CLIENT *clnt = clnt_raw_create(123, 4);
65

    
66
	if (svc == NULL || clnt == NULL) {
67
		printf("raw create failed\n");
68
		return 1;
69
	}
70

    
71
	if (svc_reg(svc, 123, 4, dispatch, NULL) == 0) {
72
		printf("svc_reg failed\n");
73
		return 1;
74
	}
75

    
76
	printf("Raw:\n");
77
	testsend(clnt);
78

    
79
	if (thr_create(NULL, 0, run_door_server, NULL, THR_DETACHED, NULL) != NULL) {
80
		printf("run_door_server thread creation failed\n");
81
		return 1;
82
	}
83

    
84
	/*
85
	 * Wait for a second to make sure the door server had enough time for
86
	 * its creation.
87
	 */
88
	sleep(1);
89

    
90
	CLIENT *clntd = clnt_door_create(567, 8, 0);
91
	if (clntd == NULL) {
92
		printf("clnt_door_create failed\n");
93
		return 1;
94
	}
95

    
96
	printf("\nDoor:\n");
97
	testsend(clntd);
98

    
99
	return 0;
100
}
    (1-1/1)