Browse code

added functions for rr_t serialization/deserialization (will be used from dialog module)

Vaclav Kubart authored on 10/08/2006 12:21:27
Showing 4 changed files
... ...
@@ -1,3 +1,6 @@
1
+2006-08-08
2
+	* added functions for rr_t serialization/deserialization
3
+
1 4
 2006-06-29
2 5
 	* added function has_to_tag common for more presence modules
3 6
 
... ...
@@ -228,4 +228,3 @@ unsigned int rshash(const char* str, unsigned int len)
228 228
 	return (hash & 0x7FFFFFFF);
229 229
 }
230 230
 
231
-
232 231
new file mode 100644
... ...
@@ -0,0 +1,124 @@
1
+#include <cds/memory.h>
2
+#include <cds/logger.h>
3
+#include <cds/rr_serialize.h>
4
+
5
+#ifdef SER
6
+
7
+static void rr_dup(rr_t **dst, rr_t *pkg_rr)
8
+{
9
+	rr_t *tmp = NULL;
10
+	int res;
11
+	
12
+	tmp = pkg_rr;
13
+	
14
+	while (pkg_rr) {
15
+		res = shm_duplicate_rr(dst, pkg_rr);
16
+		dst = &(*dst)->next;
17
+		pkg_rr = pkg_rr->next;
18
+	}
19
+	if (tmp) free_rr(&tmp);
20
+}
21
+
22
+int serialize_route(sstream_t *ss, rr_t **_r)
23
+{
24
+	int do_it = 0;
25
+	int res = 0;
26
+	if (is_input_sstream(ss)) { /* read route */
27
+		if (serialize_int(ss, &do_it) != 0) return -1;
28
+		*_r = NULL;
29
+	}
30
+	else { /* store route */
31
+		if (*_r) do_it = 1;
32
+		else do_it = 0;
33
+		if (serialize_int(ss, &do_it) != 0) return -1;
34
+	}
35
+		
36
+	if (do_it) {
37
+		str s;
38
+		if (*_r) {
39
+			s.s = (*_r)->nameaddr.name.s;
40
+			s.len = (*_r)->len;
41
+		}
42
+
43
+		res = serialize_str(ss, &s) | res;
44
+		if (is_input_sstream(ss)) {
45
+			rr_t *pkg_rr = NULL;
46
+			parse_rr_body(s.s, s.len, &pkg_rr);
47
+			rr_dup(_r, pkg_rr);
48
+		}
49
+	}
50
+	
51
+	return res;
52
+}
53
+
54
+int serialize_route_set(sstream_t *ss, rr_t **route_set)
55
+{
56
+	rr_t *r, *first = NULL, *last = NULL;
57
+	int res = 0;
58
+
59
+	if (is_input_sstream(ss)) { /* read */
60
+		do {
61
+			res = serialize_route(ss, &r) | res;
62
+			if (last) last->next = r; 
63
+			else first = r;
64
+			last = r;
65
+			if (last) {
66
+				/* due to parsing rr (may be more rr_t than 1) */
67
+				while (last->next) last = last->next; 
68
+			}
69
+		} while (r);
70
+		*route_set = first;
71
+	}
72
+	else {	/* store */
73
+		r = *route_set;
74
+		while (r) {
75
+			serialize_route(ss, &r);
76
+			r = r->next;
77
+		}
78
+		r = NULL;
79
+		serialize_route(ss, &r); /* store terminating route */
80
+	}
81
+	
82
+	return 0;
83
+}
84
+
85
+int route_set2str(rr_t *rr, str_t *dst_str)
86
+{
87
+	int res = 0;
88
+	sstream_t store;
89
+	
90
+	init_output_sstream(&store, 256);
91
+	
92
+	if (serialize_route_set(&store, &rr) != 0) {
93
+		ERROR_LOG("can't serialize route set\n");
94
+		res = -1;
95
+	}
96
+	else {
97
+		if (get_serialized_sstream(&store, dst_str) != 0) {
98
+			ERROR_LOG("can't get serialized data\n");
99
+			res = -1;
100
+		}
101
+	}
102
+
103
+	destroy_sstream(&store);
104
+	return res;
105
+}
106
+
107
+int str2route_set(const str_t *s, rr_t **rr)
108
+{
109
+	int res = 0;
110
+	sstream_t store;
111
+
112
+	if (!s) return -1;
113
+	
114
+	init_input_sstream(&store, s->s, s->len);
115
+	if (serialize_route_set(&store, rr) != 0) {
116
+		ERROR_LOG("can't de-serialize route set\n");
117
+		res = -1;
118
+	}	
119
+	destroy_sstream(&store);
120
+	
121
+	return res;
122
+}
123
+
124
+#endif
0 125
new file mode 100644
... ...
@@ -0,0 +1,17 @@
1
+#ifndef __RR_SERIALIZE_H
2
+#define __RR_SERIALIZE_H
3
+
4
+#ifdef SER
5
+
6
+/* only within ser */
7
+#include <cds/sstr.h>
8
+#include <cds/serialize.h>
9
+#include <parser/parse_rr.h>
10
+
11
+int serialize_route(sstream_t *ss, rr_t **_r);
12
+int route_set2str(rr_t *rr, str_t *dst_str);
13
+int str2route_set(const str_t *s, rr_t **rr);
14
+
15
+#endif
16
+
17
+#endif