summaryrefslogtreecommitdiff
path: root/cpp/test.cpp
blob: 7c91fbdff90c62ef0fcbd4c98abc8268c99fbaf2 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <string>
//#include <msgpack/unpack.hpp>
//#include <msgpack/pack.hpp>
#include <msgpack.hpp>
#include <sstream>
#include <boost/scoped_ptr.hpp>

class checker {
public:
	void check(const char* d, size_t len, msgpack::object should) {
		using msgpack::object;
		try {
			std::cout << "----" << std::endl;

			object o;
			try {
				o = msgpack::unpack(d, len, m_zone);
			} catch (std::runtime_error& e) {
				std::cout << should << std::endl;
				std::cout << "**" << e.what() << "**" << std::endl;
				return;
			}

			std::cout << o << std::endl;
			if(o != should) {
				std::cout << "** TEST FAILED **" << std::endl;
			}

			try {
				std::string s;
				msgpack::pack(s, o);
				object ro = msgpack::unpack(s.data(), s.size(), m_zone);
				if(ro != o) { throw std::runtime_error("NOT MATCH"); }
			} catch (std::runtime_error& e) {
				std::cout << "** REUNPACK FAILED **" << std::endl;
				std::cout << e.what() << std::endl;
			} catch (...) {
				std::cout << "** REUNPACK FAILED **" << std::endl;
				std::cout << "unknown error" << std::endl;
			}

		} catch (...) { m_zone.clear(); throw; }
		m_zone.clear();
	}
private:
	msgpack::zone m_zone;
};

int main(void)
{
	checker c;

	{  // SimpleValue
		msgpack::zone z;
		const char d[] = {
			0x93, 0xc0, 0xc2, 0xc3,
		};
		c.check(d, sizeof(d),
			z.narray(
				z.nnil(), z.nfalse(), z.ntrue()
			)
		);
	}

	{  // Fixnum
		msgpack::zone z;
		const char d[] = {
			0x92,
				0x93, 0x00, 0x40, 0x7f,
				0x93, 0xe0, 0xf0, 0xff,
		};
		c.check(d, sizeof(d),
			z.narray(
				z.narray(
					z.nu8(0),
					z.nu8(64),
					z.nu8(127)
				),
				z.narray(
					z.ni8(-32),
					z.ni8(-16),
					z.ni8(-1)
				)
			)
		);
	}

	{  // FixArray
		msgpack::zone z;
		const char d[] = {
			0x92,
				0x90,
				0x91,
					0x91, 0xc0,
		};
		c.check(d, sizeof(d),
			z.narray(
				z.narray(),
				z.narray(
					z.narray(
						z.nnil()
					)
				)
			)
		);
	}

	{  // FixRaw
		msgpack::zone z;
		const char d[] = {
			0x94,
				0xa0,
				0xa1, 'a',
				0xa2, 'b', 'c',
				0xa3, 'd', 'e', 'f',
		};
		c.check(d, sizeof(d),
			z.narray(
				z.nraw_ref("", 0),
				z.nraw_ref("a", 1),
				z.nraw_ref("bc", 2),
				z.nraw_ref("def", 3)
			)
		);
	}

	static const uint16_t TASK_ARRAY = 100;
	static char tarray[3];
	static char traw[64];

	{
		memset(traw, 'a', sizeof(traw));
		traw[0] = 0xda;
		uint16_t n = htons(sizeof(traw)-3);
		traw[1] = ((char*)&n)[0];
		traw[2] = ((char*)&n)[1];

		msgpack::zone z;
		std::cout << msgpack::unpack(traw, sizeof(traw), z) << std::endl;;
	}

	{
		tarray[0] = 0xdc;
		uint16_t n = htons(TASK_ARRAY);
		tarray[1] = ((char*)&n)[0];
		tarray[2] = ((char*)&n)[1];
	}

	{
		// write message
		ssize_t total_bytes = 0;
		std::stringstream stream;
		for(unsigned q=0; q < 10; ++q) {
			stream.write(tarray, sizeof(tarray));
			total_bytes += sizeof(tarray);
			for(uint16_t i=0; i < TASK_ARRAY; ++i) {
				stream.write(traw, sizeof(traw));
				total_bytes += sizeof(traw);
			}
		}

		stream.seekg(0);

		// reserive message
		unsigned num_msg = 0;

		static const size_t RESERVE_SIZE = 32;//*1024;

		msgpack::unpacker upk;
		while(stream.good() && total_bytes > 0) {

			// 1. reserve buffer
			upk.reserve_buffer(RESERVE_SIZE);

			// 2. read data to buffer() up to buffer_capacity() bytes
			size_t sz = stream.readsome(
					(char*)upk.buffer(),
					upk.buffer_capacity());

			total_bytes -= sz;
			std::cout << "read " << sz << " bytes to capacity "
					<< upk.buffer_capacity() << " bytes"
					<< std::endl;

			// 3. specify the number of  bytes actually copied
			upk.buffer_consumed(sz);

			// 4. repeat execute() until it returns false
			while( upk.execute() ) {
				std::cout << "message parsed" << std::endl;

				// 5.1. take out the parsed object
				msgpack::object o = upk.data();

				// 5.2. the parsed object is valid until the zone is deleted
				boost::scoped_ptr<msgpack::zone> pz(upk.release_zone());

				std::cout << o << std::endl;
				++num_msg;

				// 5.3 re-initialize unpacker
				upk.reset();
			}

		}

		std::cout << "stream finished" << std::endl;
		std::cout << num_msg << " messages reached" << std::endl;
	}

	return 0;
}