File: tp.c

package info (click to toggle)
tarantool 1.5.2.20.g5f5d924-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,568 kB
  • ctags: 18,697
  • sloc: ansic: 109,092; sh: 21,312; cpp: 20,633; xml: 9,666; asm: 2,488; python: 2,195; java: 1,759; perl: 1,002; makefile: 679
file content (131 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <tp.h>

static void reply_print(struct tp *rep) {
	while (tp_next(rep)) {
		printf("tuple fields: %d\n", tp_tuplecount(rep));
		printf("tuple size: %d\n", tp_tuplesize(rep));
		printf("[");
		while (tp_nextfield(rep)) {
			printf("%-.*s", tp_getfieldsize(rep), tp_getfield(rep));
			if (tp_hasnextfield(rep))
				printf(", ");
		}
		printf("]\n");
	}
}

static inline int
test_check_read_reply(int fd) {
	struct tp rep;
	tp_init(&rep, NULL, 0, tp_realloc, NULL);
	while (1) {
		ssize_t to_read = tp_req(&rep);
		if (to_read <= 0)
			break;
		ssize_t new_size = tp_ensure(&rep, to_read);
		if (new_size == -1) {
			// no memory (?)
			return 1;
		}
		ssize_t res = read(fd, rep.p, to_read);
		if (res == 0) {
			// eof
			return 1;
		} else if (res < 0) {
			// error
			return 1;
		}
		tp_use(&rep, res);
	}

	ssize_t server_code = tp_reply(&rep);

	if (server_code != 0) {
		printf("error: %-.*s\n", tp_replyerrorlen(&rep),
		   tp_replyerror(&rep));
		tp_free(&rep);
		return 1;
	}
	if (tp_replyop(&rep) == 17) { /* select */
		reply_print(&rep);
	} else
	if (tp_replyop(&rep) == 13) { /* insert */
	} else {
		return 1;
	}
	tp_free(&rep);
	return 0;
}

static inline int
test_check_read(void)
{
	int fd;
	struct sockaddr_in tt;
	if ((fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		printf("Failed to create socket\n");
		return 1;
	}

	memset(&tt, 0, sizeof(tt));
	tt.sin_family = AF_INET;
	tt.sin_addr.s_addr = inet_addr("127.0.0.1");
	tt.sin_port = htons(33013);
	if (connect(fd, (struct sockaddr *) &tt, sizeof(tt)) < 0) {
		printf("Failed to connect\n");
		return 1;
	}

	struct tp req;
	tp_init(&req, NULL, 0, tp_realloc, NULL);
	tp_insert(&req, 0, 0);
	tp_tuple(&req);
	tp_sz(&req, "_i32");
	tp_sz(&req, "0e72ae1a-d0be-4e49-aeb9-aebea074363c");
	tp_select(&req, 0, 0, 0, 1);
	tp_tuple(&req);
	tp_sz(&req, "_i32");
	int rc = write(fd, tp_buf(&req), tp_used(&req));
	if (rc != tp_used(&req))
		return 1;

	tp_free(&req);

	rc = test_check_read_reply(fd);
	if (rc != 0)
		return 1;
	rc = test_check_read_reply(fd);
	if (rc != 0)
		return 1;

	close(fd);
	return 0;
}

static inline void
test_check_buffer_initialized(void) {
	struct tp req;
	tp_init(&req, NULL, 0, tp_realloc, NULL);
	tp_select(&req, 0, 0, 0, 0); /* could fail on assert */
	tp_tuple(&req);
	tp_sz(&req, "key");
	tp_free(&req);
}

int
main(int argc, char *argv[])
{
	(void)argc;
	(void)argv;
	test_check_buffer_initialized();
	test_check_read();
	return 0;
}