Project

General

Profile

Bug #4871 ยป t_sync-test.c

Marcel Telka, 2014-05-20 08:23 PM

 
1
#include <xti.h>
2
#include <fcntl.h>
3
#include <thread.h>
4
#include <stdio.h>
5
#include <rpc/nettype.h>
6
#include <netdir.h>
7
#include <stdlib.h>
8

    
9
volatile int c_syn = 0;
10
volatile int c_con = 0;
11

    
12
struct netbuf *addr;
13
int fs;
14

    
15
void
16
srv(void)
17
{
18
	int fsa;
19
	struct t_bind *fsb;
20
	struct t_call *call;
21

    
22
	fs = t_open("/dev/tcp", O_RDWR, NULL);
23
	if (fs < 0) {
24
		t_error("t_open failed (s)");
25
		exit(1);
26
	}
27

    
28
	fsb = t_alloc(fs, T_BIND, T_ALL);
29
	call = t_alloc(fs, T_CALL, T_ALL);
30
	if (fsb == NULL || call == NULL) {
31
		printf("t_alloc failed (s)");
32
		exit(1);
33
	}
34

    
35
	fsb->addr = *addr;
36
	fsb->qlen = 16;
37

    
38
	if (t_bind(fs, fsb, NULL) != 0) {
39
		t_error("t_bind failed (s)");
40
		exit(1);
41
	}
42

    
43
	for (;;) {
44

    
45
		if (t_listen(fs, call) != 0) {
46
			t_error("t_listen failed (s)");
47
			exit(1);
48
		}
49

    
50
		fsa = t_open("/dev/tcp", O_RDWR, NULL);
51
		if (fsa < 0) {
52
			t_error("t_open failed (sa)");
53
			exit(1);
54
		}
55

    
56
		if (t_accept(fs, fsa, call) != 0) {
57
			t_error("t_accept failed (s)");
58
			exit(1);
59
		}
60

    
61
		if (t_close(fsa) != 0) {
62
			t_error("t_close failed (s)");
63
			exit(1);
64
		}
65
	}
66
}
67

    
68
void
69
clnt(void)
70
{
71
	int fc;
72
	struct t_call sndcall;
73

    
74
	for (;;) {
75
		fc = t_open("/dev/tcp", O_RDWR, NULL);
76
		if (fc < 0) {
77
			t_error("t_open failed (c)");
78
			exit(1);
79
		}
80

    
81
		if (t_bind(fc, NULL, NULL) < 0) {
82
			t_error("t_bind failed (c)");
83
			exit(1);
84
		}
85

    
86
		sndcall.addr = *addr;
87
		sndcall.opt.len = sndcall.opt.maxlen = 0;
88
		sndcall.udata.len = sndcall.udata.maxlen = 0;
89

    
90
		if (t_connect(fc, &sndcall, NULL) != 0) {
91
			t_error("t_connect failed (c)");
92
			exit(1);
93
		}
94

    
95
		t_close(fc);
96

    
97
		atomic_add_int(&c_con, 1);
98
	}
99
}
100

    
101
void
102
sync(void)
103
{
104
	for (;;) {
105
		if (t_sync(fs) == -1) {
106
			t_error("t_sync failed");
107
			exit(1);
108
		}
109
		atomic_add_int(&c_syn, 1);
110
	}
111
}
112

    
113
void
114
atex(void)
115
{
116
	printf("c_syn: %d\n", c_syn);
117
	printf("c_con: %d\n", c_con);
118
}
119

    
120
int main(void)
121
{
122
	struct netconfig *nconf;
123

    
124
	atexit(atex);
125

    
126
	nconf = __rpc_getconfip("tcp");
127
	if (nconf == NULL) {
128
		printf("__rpc_getconfip failed\n");
129
		return -1;
130
	}
131
	addr = uaddr2taddr(nconf, "127.0.0.1.5.3");
132
	if (addr == NULL) {
133
		printf("uaddr2taddr failed\n");
134
		return -1;
135
	}
136

    
137
	thr_create(NULL, 0, (void *(*)(void *))srv, NULL, THR_DETACHED, NULL);
138

    
139
	sleep(1);
140

    
141
#ifndef NOSYNC
142
	thr_create(NULL, 0, (void *(*)(void *))sync, NULL, THR_DETACHED, NULL);
143
#endif
144
	thr_create(NULL, 0, (void *(*)(void *))clnt, NULL, THR_DETACHED, NULL);
145

    
146
	sleep(10);
147

    
148
	return 0;
149
}
    (1-1/1)