rbczmq 1.6.2 → 1.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (398) hide show
  1. data/.gitignore +4 -3
  2. data/.gitmodules +6 -0
  3. data/.travis.yml +5 -1
  4. data/CHANGELOG.rdoc +15 -0
  5. data/Gemfile.lock +2 -2
  6. data/README.rdoc +5 -2
  7. data/Rakefile +8 -3
  8. data/ext/czmq/.gitignore +52 -0
  9. data/ext/czmq/.travis.yml +18 -0
  10. data/ext/czmq/AUTHORS +9 -0
  11. data/ext/czmq/COPYING +674 -0
  12. data/ext/czmq/COPYING.LESSER +178 -0
  13. data/ext/czmq/ChangeLog +0 -0
  14. data/ext/czmq/Makefile.am +22 -0
  15. data/ext/czmq/NEWS +263 -0
  16. data/ext/czmq/README +0 -0
  17. data/ext/czmq/README.md +1122 -0
  18. data/ext/czmq/README.txt +327 -0
  19. data/ext/czmq/autogen.sh +46 -0
  20. data/ext/czmq/builds/android/Android.mk +35 -0
  21. data/ext/czmq/builds/android/Application.mk +1 -0
  22. data/ext/czmq/builds/android/build.sh +59 -0
  23. data/ext/czmq/builds/android/clean.sh +26 -0
  24. data/ext/czmq/builds/mingw32/Makefile.mingw32 +38 -0
  25. data/ext/czmq/builds/mingw32/platform.h +0 -0
  26. data/ext/czmq/builds/msvc/.gitignore +18 -0
  27. data/ext/czmq/builds/msvc/README.txt +17 -0
  28. data/ext/czmq/builds/msvc/czmq.sln +69 -0
  29. data/ext/czmq/builds/msvc/czmq.vcproj +2246 -0
  30. data/ext/czmq/builds/msvc/czmq.vcxproj +329 -0
  31. data/ext/czmq/builds/msvc/czmq.vcxproj.filters +117 -0
  32. data/ext/czmq/builds/msvc/czmq11.sln +36 -0
  33. data/ext/czmq/builds/msvc/czmq_selftest.vcproj +840 -0
  34. data/ext/czmq/builds/msvc/czmq_selftest.vcxproj +189 -0
  35. data/ext/czmq/builds/msvc/czmq_selftest.vcxproj.filters +14 -0
  36. data/ext/czmq/c +520 -0
  37. data/ext/czmq/configure.ac +229 -0
  38. data/ext/czmq/doc/Makefile.am +49 -0
  39. data/ext/czmq/doc/asciidoc.conf +57 -0
  40. data/ext/czmq/doc/czmq.txt +334 -0
  41. data/ext/czmq/doc/mkman +100 -0
  42. data/ext/czmq/doc/mksite +65 -0
  43. data/ext/czmq/doc/wdput +43 -0
  44. data/ext/czmq/doc/xml2wd.pl +242 -0
  45. data/ext/czmq/doc/zbeacon.txt +173 -0
  46. data/ext/czmq/doc/zclock.txt +51 -0
  47. data/ext/czmq/doc/zconfig.txt +92 -0
  48. data/ext/czmq/doc/zctx.txt +111 -0
  49. data/ext/czmq/doc/zfile.txt +77 -0
  50. data/ext/czmq/doc/zframe.txt +222 -0
  51. data/ext/czmq/doc/zhash.txt +225 -0
  52. data/ext/czmq/doc/zlist.txt +176 -0
  53. data/ext/czmq/doc/zloop.txt +106 -0
  54. data/ext/czmq/doc/zmsg.txt +315 -0
  55. data/ext/czmq/doc/zmutex.txt +54 -0
  56. data/ext/czmq/doc/zsocket.txt +110 -0
  57. data/ext/czmq/doc/zsockopt.txt +528 -0
  58. data/ext/czmq/doc/zstr.txt +80 -0
  59. data/ext/czmq/doc/zsys.txt +44 -0
  60. data/ext/czmq/doc/zthread.txt +126 -0
  61. data/ext/czmq/doc/ztree.txt +236 -0
  62. data/ext/czmq/images/README_1.png +0 -0
  63. data/ext/czmq/images/README_2.png +0 -0
  64. data/ext/czmq/include/czmq.h +64 -0
  65. data/ext/czmq/include/czmq_prelude.h +504 -0
  66. data/ext/czmq/include/zbeacon.h +91 -0
  67. data/ext/czmq/include/zclock.h +56 -0
  68. data/ext/czmq/include/zconfig.h +117 -0
  69. data/ext/czmq/include/zctx.h +96 -0
  70. data/ext/czmq/include/zfile.h +82 -0
  71. data/ext/czmq/include/zframe.h +145 -0
  72. data/ext/czmq/include/zhash.h +127 -0
  73. data/ext/czmq/include/zlist.h +113 -0
  74. data/ext/czmq/include/zloop.h +98 -0
  75. data/ext/czmq/include/zmsg.h +165 -0
  76. data/ext/czmq/include/zmutex.h +62 -0
  77. data/ext/czmq/include/zsocket.h +104 -0
  78. data/ext/czmq/include/zsockopt.h +249 -0
  79. data/ext/czmq/include/zstr.h +69 -0
  80. data/ext/czmq/include/zsys.h +66 -0
  81. data/ext/czmq/include/zthread.h +62 -0
  82. data/ext/czmq/include/ztree.h +133 -0
  83. data/ext/czmq/mkdoc +14 -0
  84. data/ext/czmq/model/generate +2 -0
  85. data/ext/czmq/model/sockopts.xml +101 -0
  86. data/ext/czmq/notes.txt +21 -0
  87. data/ext/czmq/scripts/sockopts.gsl +325 -0
  88. data/ext/czmq/src/Makefile.am +61 -0
  89. data/ext/czmq/src/czmq_selftest.c +60 -0
  90. data/ext/czmq/src/libczmq.pc.in +11 -0
  91. data/ext/czmq/src/selftest +7 -0
  92. data/ext/czmq/src/selftest.cfg +5 -0
  93. data/ext/czmq/src/valgrind.supp +14 -0
  94. data/ext/czmq/src/vg +2 -0
  95. data/ext/czmq/src/zbeacon.c +787 -0
  96. data/ext/czmq/src/zclock.c +143 -0
  97. data/ext/czmq/src/zconfig.c +691 -0
  98. data/ext/czmq/src/zctx.c +287 -0
  99. data/ext/czmq/src/zfile.c +237 -0
  100. data/ext/czmq/src/zframe.c +551 -0
  101. data/ext/czmq/src/zhash.c +664 -0
  102. data/ext/czmq/src/zlist.c +459 -0
  103. data/ext/czmq/src/zloop.c +496 -0
  104. data/ext/czmq/src/zmsg.c +854 -0
  105. data/ext/czmq/src/zmutex.c +134 -0
  106. data/ext/czmq/src/zsocket.c +313 -0
  107. data/ext/czmq/src/zsockopt.c +1756 -0
  108. data/ext/czmq/src/zstr.c +297 -0
  109. data/ext/czmq/src/zsys.c +136 -0
  110. data/ext/czmq/src/zthread.c +269 -0
  111. data/ext/czmq/src/ztree.c +888 -0
  112. data/ext/czmq/version.sh +21 -0
  113. data/ext/rbczmq/extconf.rb +1 -18
  114. data/ext/rbczmq/poller.c +4 -1
  115. data/ext/rbczmq/socket.c +28 -5
  116. data/ext/rbczmq/socket.h +1 -0
  117. data/ext/zeromq/AUTHORS +110 -0
  118. data/ext/zeromq/CMakeLists.txt +392 -0
  119. data/ext/zeromq/COPYING +674 -0
  120. data/ext/zeromq/COPYING.LESSER +179 -0
  121. data/ext/zeromq/INSTALL +246 -0
  122. data/ext/zeromq/MAINTAINERS +56 -0
  123. data/ext/zeromq/Makefile.am +40 -0
  124. data/ext/zeromq/NEWS +333 -0
  125. data/ext/zeromq/README +39 -0
  126. data/ext/zeromq/acinclude.m4 +930 -0
  127. data/ext/zeromq/autogen.sh +45 -0
  128. data/ext/zeromq/branding.bmp +0 -0
  129. data/ext/zeromq/builds/msvc/Makefile.am +33 -0
  130. data/ext/zeromq/builds/msvc/c_local_lat/c_local_lat.vcproj +176 -0
  131. data/ext/zeromq/builds/msvc/c_local_lat/c_local_lat.vcxproj +87 -0
  132. data/ext/zeromq/builds/msvc/c_local_thr/c_local_thr.vcproj +176 -0
  133. data/ext/zeromq/builds/msvc/c_local_thr/c_local_thr.vcxproj +87 -0
  134. data/ext/zeromq/builds/msvc/c_remote_lat/c_remote_lat.vcproj +176 -0
  135. data/ext/zeromq/builds/msvc/c_remote_lat/c_remote_lat.vcxproj +87 -0
  136. data/ext/zeromq/builds/msvc/c_remote_thr/c_remote_thr.vcproj +176 -0
  137. data/ext/zeromq/builds/msvc/c_remote_thr/c_remote_thr.vcxproj +87 -0
  138. data/ext/zeromq/builds/msvc/errno.cpp +32 -0
  139. data/ext/zeromq/builds/msvc/errno.hpp +56 -0
  140. data/ext/zeromq/builds/msvc/inproc_lat/inproc_lat.vcproj +174 -0
  141. data/ext/zeromq/builds/msvc/inproc_lat/inproc_lat.vcxproj +86 -0
  142. data/ext/zeromq/builds/msvc/inproc_thr/inproc_thr.vcproj +174 -0
  143. data/ext/zeromq/builds/msvc/inproc_thr/inproc_thr.vcxproj +86 -0
  144. data/ext/zeromq/builds/msvc/libzmq/libzmq.vcproj +804 -0
  145. data/ext/zeromq/builds/msvc/libzmq/libzmq.vcxproj +252 -0
  146. data/ext/zeromq/builds/msvc/libzmq/libzmq.vcxproj.filters +431 -0
  147. data/ext/zeromq/builds/msvc/msvc.sln +89 -0
  148. data/ext/zeromq/builds/msvc/msvc10.sln +116 -0
  149. data/ext/zeromq/builds/msvc/platform.hpp +32 -0
  150. data/ext/zeromq/builds/msvc/properties/Common.props +21 -0
  151. data/ext/zeromq/builds/msvc/properties/Debug.props +19 -0
  152. data/ext/zeromq/builds/msvc/properties/Dynamic.props +20 -0
  153. data/ext/zeromq/builds/msvc/properties/Executable.props +19 -0
  154. data/ext/zeromq/builds/msvc/properties/Precompiled.props +14 -0
  155. data/ext/zeromq/builds/msvc/properties/Release.props +22 -0
  156. data/ext/zeromq/builds/msvc/properties/Win32.props +12 -0
  157. data/ext/zeromq/builds/msvc/properties/Win32_Release.props +17 -0
  158. data/ext/zeromq/builds/msvc/properties/WithOpenPGM.props +12 -0
  159. data/ext/zeromq/builds/msvc/properties/ZeroMQ.props +23 -0
  160. data/ext/zeromq/builds/msvc/properties/x64.props +12 -0
  161. data/ext/zeromq/builds/redhat/zeromq.spec.in +160 -0
  162. data/ext/zeromq/builds/valgrind/valgrind.supp +14 -0
  163. data/ext/zeromq/builds/valgrind/vg +1 -0
  164. data/ext/zeromq/cmake/Modules/TestZMQVersion.cmake +35 -0
  165. data/ext/zeromq/cmake/Modules/zmq_version.cpp +31 -0
  166. data/ext/zeromq/cmake/NSIS.template32.in +952 -0
  167. data/ext/zeromq/cmake/NSIS.template64.in +960 -0
  168. data/ext/zeromq/configure.in +428 -0
  169. data/ext/zeromq/doc/Makefile.am +51 -0
  170. data/ext/zeromq/doc/asciidoc.conf +56 -0
  171. data/ext/zeromq/doc/zmq.txt +233 -0
  172. data/ext/zeromq/doc/zmq_bind.txt +102 -0
  173. data/ext/zeromq/doc/zmq_close.txt +52 -0
  174. data/ext/zeromq/doc/zmq_connect.txt +98 -0
  175. data/ext/zeromq/doc/zmq_ctx_destroy.txt +66 -0
  176. data/ext/zeromq/doc/zmq_ctx_get.txt +67 -0
  177. data/ext/zeromq/doc/zmq_ctx_new.txt +49 -0
  178. data/ext/zeromq/doc/zmq_ctx_set.txt +75 -0
  179. data/ext/zeromq/doc/zmq_disconnect.txt +67 -0
  180. data/ext/zeromq/doc/zmq_epgm.txt +162 -0
  181. data/ext/zeromq/doc/zmq_errno.txt +50 -0
  182. data/ext/zeromq/doc/zmq_getsockopt.txt +516 -0
  183. data/ext/zeromq/doc/zmq_init.txt +52 -0
  184. data/ext/zeromq/doc/zmq_inproc.txt +85 -0
  185. data/ext/zeromq/doc/zmq_ipc.txt +85 -0
  186. data/ext/zeromq/doc/zmq_msg_close.txt +55 -0
  187. data/ext/zeromq/doc/zmq_msg_copy.txt +57 -0
  188. data/ext/zeromq/doc/zmq_msg_data.txt +48 -0
  189. data/ext/zeromq/doc/zmq_msg_get.txt +72 -0
  190. data/ext/zeromq/doc/zmq_msg_init.txt +65 -0
  191. data/ext/zeromq/doc/zmq_msg_init_data.txt +85 -0
  192. data/ext/zeromq/doc/zmq_msg_init_size.txt +58 -0
  193. data/ext/zeromq/doc/zmq_msg_more.txt +63 -0
  194. data/ext/zeromq/doc/zmq_msg_move.txt +52 -0
  195. data/ext/zeromq/doc/zmq_msg_recv.txt +125 -0
  196. data/ext/zeromq/doc/zmq_msg_send.txt +122 -0
  197. data/ext/zeromq/doc/zmq_msg_set.txt +45 -0
  198. data/ext/zeromq/doc/zmq_msg_size.txt +48 -0
  199. data/ext/zeromq/doc/zmq_pgm.txt +162 -0
  200. data/ext/zeromq/doc/zmq_poll.txt +132 -0
  201. data/ext/zeromq/doc/zmq_proxy.txt +97 -0
  202. data/ext/zeromq/doc/zmq_recv.txt +93 -0
  203. data/ext/zeromq/doc/zmq_recvmsg.txt +123 -0
  204. data/ext/zeromq/doc/zmq_send.txt +100 -0
  205. data/ext/zeromq/doc/zmq_sendmsg.txt +119 -0
  206. data/ext/zeromq/doc/zmq_setsockopt.txt +523 -0
  207. data/ext/zeromq/doc/zmq_socket.txt +369 -0
  208. data/ext/zeromq/doc/zmq_socket_monitor.txt +288 -0
  209. data/ext/zeromq/doc/zmq_strerror.txt +55 -0
  210. data/ext/zeromq/doc/zmq_tcp.txt +101 -0
  211. data/ext/zeromq/doc/zmq_term.txt +66 -0
  212. data/ext/zeromq/doc/zmq_unbind.txt +65 -0
  213. data/ext/zeromq/doc/zmq_version.txt +53 -0
  214. data/ext/zeromq/foreign/openpgm/Makefile.am +8 -0
  215. data/ext/zeromq/foreign/openpgm/libpgm-5.1.118~dfsg.tar.gz +0 -0
  216. data/ext/zeromq/include/zmq.h +402 -0
  217. data/ext/zeromq/include/zmq_utils.h +64 -0
  218. data/ext/zeromq/installer.ico +0 -0
  219. data/ext/zeromq/perf/Makefile.am +22 -0
  220. data/ext/zeromq/perf/inproc_lat.cpp +233 -0
  221. data/ext/zeromq/perf/inproc_thr.cpp +241 -0
  222. data/ext/zeromq/perf/local_lat.cpp +109 -0
  223. data/ext/zeromq/perf/local_thr.cpp +133 -0
  224. data/ext/zeromq/perf/remote_lat.cpp +122 -0
  225. data/ext/zeromq/perf/remote_thr.cpp +105 -0
  226. data/ext/zeromq/src/Makefile.am +171 -0
  227. data/ext/zeromq/src/address.cpp +78 -0
  228. data/ext/zeromq/src/address.hpp +52 -0
  229. data/ext/zeromq/src/array.hpp +155 -0
  230. data/ext/zeromq/src/atomic_counter.hpp +197 -0
  231. data/ext/zeromq/src/atomic_ptr.hpp +196 -0
  232. data/ext/zeromq/src/blob.hpp +129 -0
  233. data/ext/zeromq/src/clock.cpp +147 -0
  234. data/ext/zeromq/src/clock.hpp +60 -0
  235. data/ext/zeromq/src/command.hpp +154 -0
  236. data/ext/zeromq/src/config.hpp +89 -0
  237. data/ext/zeromq/src/ctx.cpp +352 -0
  238. data/ext/zeromq/src/ctx.hpp +173 -0
  239. data/ext/zeromq/src/dealer.cpp +133 -0
  240. data/ext/zeromq/src/dealer.hpp +92 -0
  241. data/ext/zeromq/src/decoder.cpp +166 -0
  242. data/ext/zeromq/src/decoder.hpp +248 -0
  243. data/ext/zeromq/src/devpoll.cpp +190 -0
  244. data/ext/zeromq/src/devpoll.hpp +105 -0
  245. data/ext/zeromq/src/dist.cpp +194 -0
  246. data/ext/zeromq/src/dist.hpp +105 -0
  247. data/ext/zeromq/src/encoder.cpp +102 -0
  248. data/ext/zeromq/src/encoder.hpp +200 -0
  249. data/ext/zeromq/src/epoll.cpp +178 -0
  250. data/ext/zeromq/src/epoll.hpp +101 -0
  251. data/ext/zeromq/src/err.cpp +291 -0
  252. data/ext/zeromq/src/err.hpp +155 -0
  253. data/ext/zeromq/src/fd.hpp +45 -0
  254. data/ext/zeromq/src/fq.cpp +141 -0
  255. data/ext/zeromq/src/fq.hpp +74 -0
  256. data/ext/zeromq/src/i_decoder.hpp +49 -0
  257. data/ext/zeromq/src/i_encoder.hpp +55 -0
  258. data/ext/zeromq/src/i_engine.hpp +55 -0
  259. data/ext/zeromq/src/i_msg_sink.hpp +43 -0
  260. data/ext/zeromq/src/i_msg_source.hpp +44 -0
  261. data/ext/zeromq/src/i_poll_events.hpp +47 -0
  262. data/ext/zeromq/src/io_object.cpp +108 -0
  263. data/ext/zeromq/src/io_object.hpp +81 -0
  264. data/ext/zeromq/src/io_thread.cpp +104 -0
  265. data/ext/zeromq/src/io_thread.hpp +91 -0
  266. data/ext/zeromq/src/ip.cpp +109 -0
  267. data/ext/zeromq/src/ip.hpp +41 -0
  268. data/ext/zeromq/src/ipc_address.cpp +84 -0
  269. data/ext/zeromq/src/ipc_address.hpp +67 -0
  270. data/ext/zeromq/src/ipc_connecter.cpp +265 -0
  271. data/ext/zeromq/src/ipc_connecter.hpp +128 -0
  272. data/ext/zeromq/src/ipc_listener.cpp +206 -0
  273. data/ext/zeromq/src/ipc_listener.hpp +99 -0
  274. data/ext/zeromq/src/kqueue.cpp +201 -0
  275. data/ext/zeromq/src/kqueue.hpp +107 -0
  276. data/ext/zeromq/src/lb.cpp +148 -0
  277. data/ext/zeromq/src/lb.hpp +73 -0
  278. data/ext/zeromq/src/libzmq.pc.in +10 -0
  279. data/ext/zeromq/src/likely.hpp +33 -0
  280. data/ext/zeromq/src/mailbox.cpp +87 -0
  281. data/ext/zeromq/src/mailbox.hpp +75 -0
  282. data/ext/zeromq/src/msg.cpp +299 -0
  283. data/ext/zeromq/src/msg.hpp +148 -0
  284. data/ext/zeromq/src/mtrie.cpp +428 -0
  285. data/ext/zeromq/src/mtrie.hpp +93 -0
  286. data/ext/zeromq/src/mutex.hpp +118 -0
  287. data/ext/zeromq/src/object.cpp +393 -0
  288. data/ext/zeromq/src/object.hpp +134 -0
  289. data/ext/zeromq/src/options.cpp +562 -0
  290. data/ext/zeromq/src/options.hpp +135 -0
  291. data/ext/zeromq/src/own.cpp +206 -0
  292. data/ext/zeromq/src/own.hpp +145 -0
  293. data/ext/zeromq/src/pair.cpp +136 -0
  294. data/ext/zeromq/src/pair.hpp +79 -0
  295. data/ext/zeromq/src/pgm_receiver.cpp +283 -0
  296. data/ext/zeromq/src/pgm_receiver.hpp +141 -0
  297. data/ext/zeromq/src/pgm_sender.cpp +218 -0
  298. data/ext/zeromq/src/pgm_sender.hpp +113 -0
  299. data/ext/zeromq/src/pgm_socket.cpp +706 -0
  300. data/ext/zeromq/src/pgm_socket.hpp +124 -0
  301. data/ext/zeromq/src/pipe.cpp +447 -0
  302. data/ext/zeromq/src/pipe.hpp +207 -0
  303. data/ext/zeromq/src/poll.cpp +176 -0
  304. data/ext/zeromq/src/poll.hpp +105 -0
  305. data/ext/zeromq/src/poller.hpp +82 -0
  306. data/ext/zeromq/src/poller_base.cpp +99 -0
  307. data/ext/zeromq/src/poller_base.hpp +86 -0
  308. data/ext/zeromq/src/precompiled.cpp +21 -0
  309. data/ext/zeromq/src/precompiled.hpp +47 -0
  310. data/ext/zeromq/src/proxy.cpp +150 -0
  311. data/ext/zeromq/src/proxy.hpp +32 -0
  312. data/ext/zeromq/src/pub.cpp +57 -0
  313. data/ext/zeromq/src/pub.hpp +69 -0
  314. data/ext/zeromq/src/pull.cpp +79 -0
  315. data/ext/zeromq/src/pull.hpp +81 -0
  316. data/ext/zeromq/src/push.cpp +76 -0
  317. data/ext/zeromq/src/push.hpp +80 -0
  318. data/ext/zeromq/src/random.cpp +52 -0
  319. data/ext/zeromq/src/random.hpp +37 -0
  320. data/ext/zeromq/src/reaper.cpp +117 -0
  321. data/ext/zeromq/src/reaper.hpp +80 -0
  322. data/ext/zeromq/src/rep.cpp +137 -0
  323. data/ext/zeromq/src/rep.hpp +80 -0
  324. data/ext/zeromq/src/req.cpp +185 -0
  325. data/ext/zeromq/src/req.hpp +91 -0
  326. data/ext/zeromq/src/router.cpp +364 -0
  327. data/ext/zeromq/src/router.hpp +138 -0
  328. data/ext/zeromq/src/select.cpp +216 -0
  329. data/ext/zeromq/src/select.hpp +126 -0
  330. data/ext/zeromq/src/session_base.cpp +503 -0
  331. data/ext/zeromq/src/session_base.hpp +156 -0
  332. data/ext/zeromq/src/signaler.cpp +406 -0
  333. data/ext/zeromq/src/signaler.hpp +63 -0
  334. data/ext/zeromq/src/socket_base.cpp +1236 -0
  335. data/ext/zeromq/src/socket_base.hpp +255 -0
  336. data/ext/zeromq/src/stdint.hpp +63 -0
  337. data/ext/zeromq/src/stream_engine.cpp +594 -0
  338. data/ext/zeromq/src/stream_engine.hpp +149 -0
  339. data/ext/zeromq/src/sub.cpp +93 -0
  340. data/ext/zeromq/src/sub.hpp +71 -0
  341. data/ext/zeromq/src/tcp.cpp +131 -0
  342. data/ext/zeromq/src/tcp.hpp +38 -0
  343. data/ext/zeromq/src/tcp_address.cpp +613 -0
  344. data/ext/zeromq/src/tcp_address.hpp +100 -0
  345. data/ext/zeromq/src/tcp_connecter.cpp +319 -0
  346. data/ext/zeromq/src/tcp_connecter.hpp +123 -0
  347. data/ext/zeromq/src/tcp_listener.cpp +293 -0
  348. data/ext/zeromq/src/tcp_listener.hpp +91 -0
  349. data/ext/zeromq/src/thread.cpp +107 -0
  350. data/ext/zeromq/src/thread.hpp +79 -0
  351. data/ext/zeromq/src/trie.cpp +337 -0
  352. data/ext/zeromq/src/trie.hpp +79 -0
  353. data/ext/zeromq/src/v1_decoder.cpp +162 -0
  354. data/ext/zeromq/src/v1_decoder.hpp +68 -0
  355. data/ext/zeromq/src/v1_encoder.cpp +103 -0
  356. data/ext/zeromq/src/v1_encoder.hpp +60 -0
  357. data/ext/zeromq/src/v1_protocol.hpp +43 -0
  358. data/ext/zeromq/src/version.rc.in +93 -0
  359. data/ext/zeromq/src/windows.hpp +181 -0
  360. data/ext/zeromq/src/wire.hpp +99 -0
  361. data/ext/zeromq/src/xpub.cpp +200 -0
  362. data/ext/zeromq/src/xpub.hpp +110 -0
  363. data/ext/zeromq/src/xsub.cpp +242 -0
  364. data/ext/zeromq/src/xsub.hpp +108 -0
  365. data/ext/zeromq/src/ypipe.hpp +210 -0
  366. data/ext/zeromq/src/yqueue.hpp +199 -0
  367. data/ext/zeromq/src/zmq.cpp +1058 -0
  368. data/ext/zeromq/src/zmq_utils.cpp +61 -0
  369. data/ext/zeromq/tests/Makefile.am +55 -0
  370. data/ext/zeromq/tests/test_connect_delay.cpp +260 -0
  371. data/ext/zeromq/tests/test_connect_resolve.cpp +54 -0
  372. data/ext/zeromq/tests/test_disconnect_inproc.cpp +120 -0
  373. data/ext/zeromq/tests/test_hwm.cpp +83 -0
  374. data/ext/zeromq/tests/test_invalid_rep.cpp +92 -0
  375. data/ext/zeromq/tests/test_last_endpoint.cpp +60 -0
  376. data/ext/zeromq/tests/test_monitor.cpp +289 -0
  377. data/ext/zeromq/tests/test_msg_flags.cpp +78 -0
  378. data/ext/zeromq/tests/test_pair_inproc.cpp +53 -0
  379. data/ext/zeromq/tests/test_pair_ipc.cpp +53 -0
  380. data/ext/zeromq/tests/test_pair_tcp.cpp +54 -0
  381. data/ext/zeromq/tests/test_reqrep_device.cpp +143 -0
  382. data/ext/zeromq/tests/test_reqrep_inproc.cpp +53 -0
  383. data/ext/zeromq/tests/test_reqrep_ipc.cpp +53 -0
  384. data/ext/zeromq/tests/test_reqrep_tcp.cpp +54 -0
  385. data/ext/zeromq/tests/test_router_mandatory.cpp +62 -0
  386. data/ext/zeromq/tests/test_shutdown_stress.cpp +93 -0
  387. data/ext/zeromq/tests/test_sub_forward.cpp +99 -0
  388. data/ext/zeromq/tests/test_term_endpoint.cpp +118 -0
  389. data/ext/zeromq/tests/test_timeo.cpp +119 -0
  390. data/ext/zeromq/tests/testutil.hpp +77 -0
  391. data/ext/zeromq/version.sh +21 -0
  392. data/lib/zmq/version.rb +1 -1
  393. data/rbczmq.gemspec +16 -3
  394. data/test/test_socket.rb +13 -1
  395. metadata +398 -9
  396. checksums.yaml +0 -15
  397. data/ext/czmq-1.4.1.tar.gz +0 -0
  398. data/ext/zeromq-3.2.3.tar.gz +0 -0
@@ -0,0 +1,888 @@
1
+ /* =========================================================================
2
+ ztree - generic type-free red-black tree container
3
+
4
+ -------------------------------------------------------------------------
5
+ Copyright (c) 2013 Stephen Hemminger <stephen@networkplumber.org>
6
+ Copyright other contributors as noted in the AUTHORS file.
7
+
8
+ This file is part of CZMQ, the high-level C binding for 0MQ:
9
+ http://czmq.zeromq.org.
10
+
11
+ This is free software; you can redistribute it and/or modify it under
12
+ the terms of the GNU Lesser General Public License as published by the
13
+ Free Software Foundation; either version 3 of the License, or (at your
14
+ option) any later version.
15
+
16
+ This software is distributed in the hope that it will be useful, but
17
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABIL-
18
+ ITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
19
+ Public License for more details.
20
+
21
+ You should have received a copy of the GNU Lesser General Public License
22
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
23
+ =========================================================================
24
+
25
+ Derived from Emin Martianan's Red Black which is licensed for free use.
26
+
27
+ http://web.mit.edu/~emin/www.old/source_code/red_black_tree/index.html
28
+ */
29
+
30
+ /*
31
+ @header
32
+ Red black tree container
33
+ @discuss
34
+ @end
35
+ */
36
+
37
+ #include "../include/czmq.h"
38
+
39
+ // Tree node, used internally
40
+
41
+ typedef struct _node_t {
42
+ char *key; // User's key
43
+ void *value; // and datum
44
+ bool red; // if !red then node is black
45
+ struct _node_t *left, *right, *parent;
46
+ zhash_free_fn *free_fn; // Value free function if any
47
+ } node_t;
48
+
49
+ struct _ztree_t {
50
+ ztree_compare_fn *compare; // Comparison function
51
+ // A sentinel is used for root and nil.
52
+ // nil points to anode which should always be black but has
53
+ // aribtrary children and parent and no key or value.
54
+ // The point of using these sentinels is so that the root
55
+ // and nil nodes do not require special cases in the code
56
+ node_t root; // Red-black tree root
57
+ node_t nil; // See above
58
+ bool autofree; // If true, free value in destructor
59
+ };
60
+
61
+ // --------------------------------------------------------------------------
62
+ // Tree constructor
63
+
64
+ ztree_t *
65
+ ztree_new (ztree_compare_fn *compare)
66
+ {
67
+ assert (compare);
68
+
69
+ ztree_t *self = zmalloc (sizeof (ztree_t));
70
+ if (!self)
71
+ return NULL;
72
+
73
+ self->compare = compare;
74
+
75
+ node_t *nil = &self->nil;
76
+ nil->parent = nil->left = nil->right = nil;
77
+ nil->red = false;
78
+
79
+ node_t *root = &self->root;
80
+ root->parent = root->left = root->right = nil;
81
+ root->red = false;
82
+
83
+ return self;
84
+ }
85
+
86
+ static void
87
+ s_free_value (ztree_t *self, node_t *x)
88
+ {
89
+ if (x->free_fn)
90
+ (x->free_fn) (x->value);
91
+ else
92
+ if (self->autofree)
93
+ free (x->value);
94
+ }
95
+
96
+ // Local helper function for recursive tree destruction
97
+
98
+ static void
99
+ s_destroy (ztree_t *self, node_t *x)
100
+ {
101
+ if (x == &self->nil)
102
+ return;
103
+
104
+ s_destroy (self, x->left);
105
+ s_destroy (self, x->right);
106
+
107
+ free (x->key);
108
+ s_free_value (self, x);
109
+ free (x);
110
+ }
111
+
112
+ // --------------------------------------------------------------------------
113
+ // Tree destructor
114
+
115
+ void
116
+ ztree_destroy (ztree_t **self_p)
117
+ {
118
+ assert (self_p);
119
+ if (*self_p) {
120
+ ztree_t *self = *self_p;
121
+ s_destroy (self, self->root.left);
122
+ free (self);
123
+ *self_p = NULL;
124
+ }
125
+ }
126
+
127
+ // Rotates as described in _Introduction_To_Algorithms by
128
+ // Cormen, Leiserson, Rivest (Chapter 14). Basically this
129
+ // makes the parent of x be to the left of x, x the parent of
130
+ // its parent before the rotation and fixes other pointers accordingly.
131
+
132
+ static void
133
+ s_left_rotate (ztree_t *tree, node_t *x)
134
+ {
135
+ node_t *y;
136
+ node_t *nil = &tree->nil;
137
+
138
+ // I originally wrote this function to use the sentinel for
139
+ // nil to avoid checking for nil. However this introduces a
140
+ // very subtle bug because sometimes this function modifies
141
+ // the parent pointer of nil. This can be a problem if a
142
+ // function which calls LeftRotate also uses the nil sentinel
143
+ // and expects the nil sentinel's parent pointer to be unchanged
144
+ // after calling this function. For example, when RBDeleteFixUP
145
+ // calls LeftRotate it expects the parent pointer of nil to be
146
+ // unchanged.
147
+
148
+ y = x->right;
149
+ x->right = y->left;
150
+
151
+ if (y->left != nil)
152
+ y->left->parent=x;
153
+
154
+ y->parent = x->parent;
155
+
156
+ // instead of checking if x->parent is the root as in the book, we
157
+ // count on the root sentinel to implicitly take care of this case
158
+ if( x == x->parent->left)
159
+ x->parent->left = y;
160
+ else
161
+ x->parent->right = y;
162
+
163
+ y->left = x;
164
+ x->parent = y;
165
+
166
+ assert(!tree->nil.red);
167
+ }
168
+
169
+ static void
170
+ s_right_rotate (ztree_t *tree, node_t *y)
171
+ {
172
+ node_t *x;
173
+ node_t *nil = &tree->nil;
174
+
175
+ x = y->left;
176
+ y->left = x->right;
177
+
178
+ if (nil != x->right)
179
+ x->right->parent = y;
180
+
181
+ // instead of checking if x->parent is the root as in the book,
182
+ // we count on the root sentinel to implicitly take care of this case
183
+ x->parent = y->parent;
184
+ if (y == y->parent->left)
185
+ y->parent->left = x;
186
+ else
187
+ y->parent->right = x;
188
+
189
+ x->right = y;
190
+ y->parent = x;
191
+
192
+ assert(!tree->nil.red);
193
+ }
194
+
195
+ // Inserts z into the tree as if it were a regular binary tree
196
+ // using the algorithm described in _Introduction_To_Algorithms_
197
+ // by Cormen et al.
198
+ // Returns existing node if key is a duplicate
199
+
200
+ static node_t *s_insert_node (ztree_t *tree, node_t *z)
201
+ {
202
+ node_t *root = &tree->root;
203
+ node_t *nil = &tree->nil;
204
+
205
+ z->left = z->right = nil;
206
+
207
+ node_t *y = root;
208
+ node_t *x = root->left;
209
+ while (x != nil) {
210
+ y = x;
211
+ int cmp = tree->compare (x->key, z->key);
212
+
213
+ // do not allow duplicate keys
214
+ if (cmp == 0)
215
+ return x;
216
+
217
+ if (cmp > 0) // x.key > z.key
218
+ x = x->left;
219
+ else // x,key <= z.key
220
+ x = x->right;
221
+ }
222
+
223
+ z->parent = y;
224
+ if ( y == root ||
225
+ tree->compare (y->key, z->key) > 0) // y.key > z.key
226
+ y->left = z;
227
+ else
228
+ y->right = z;
229
+
230
+ assert(!tree->nil.red);
231
+ return NULL;
232
+ }
233
+
234
+ // Recolor the binary tree after insertion of node x
235
+
236
+ static void s_insert_recolor (ztree_t *tree, node_t *x)
237
+ {
238
+ while (x->parent->red) { // use sentinel instead of checking for root
239
+ if (x->parent == x->parent->parent->left) {
240
+ node_t *y = x->parent->parent->right;
241
+ if (y->red) {
242
+ x->parent->red = false;
243
+ y->red = false;
244
+ x->parent->parent->red = true;
245
+ x = x->parent->parent;
246
+ } else {
247
+ if (x == x->parent->right) {
248
+ x = x->parent;
249
+ s_left_rotate (tree, x);
250
+ }
251
+
252
+ x->parent->red = false;
253
+ x->parent->parent->red = true;
254
+ s_right_rotate (tree, x->parent->parent);
255
+ }
256
+ } else { //case for x->parent == x->parent->parent->right
257
+ node_t *y = x->parent->parent->left;
258
+ if (y->red) {
259
+ x->parent->red = false;
260
+ y->red = false;
261
+ x->parent->parent->red = true;
262
+ x = x->parent->parent;
263
+ } else {
264
+ if (x == x->parent->left) {
265
+ x = x->parent;
266
+ s_right_rotate (tree,x);
267
+ }
268
+ x->parent->red = false;
269
+ x->parent->parent->red = true;
270
+ s_left_rotate (tree,x->parent->parent);
271
+ }
272
+ }
273
+ }
274
+ tree->root.left->red = false;
275
+
276
+ assert(!tree->nil.red);
277
+ assert(!tree->root.red);
278
+ }
279
+
280
+ // returns the a node with key equal to q.
281
+
282
+ static node_t *
283
+ s_lookup (ztree_t *tree, const char *q)
284
+ {
285
+ node_t *nil = &tree->nil;
286
+ node_t *x = tree->root.left;
287
+
288
+ while (x != nil) {
289
+ int cmp = tree->compare (x->key, q);
290
+ if (cmp == 0)
291
+ break;
292
+ x = cmp > 0 ? x->left : x->right;
293
+ }
294
+
295
+ return x;
296
+ }
297
+
298
+ // --------------------------------------------------------------------------
299
+ // Insert node into tree with specified key and value
300
+ // If key is already present returns -1 and leaves existing node unchanged
301
+ // Returns 0 on success.
302
+
303
+ int
304
+ ztree_insert (ztree_t *self, const char *key, void *value)
305
+ {
306
+ assert (self);
307
+ assert (key);
308
+
309
+ node_t *x = zmalloc (sizeof (node_t));
310
+ if (!x)
311
+ return -1;
312
+
313
+ // If necessary, take duplicate of item (string) value
314
+ if (self->autofree)
315
+ value = strdup ((char *) value);
316
+
317
+ x->key = strdup (key);
318
+ x->value = value;
319
+ x->red = true;
320
+
321
+ // unwind if duplicate node
322
+ if (s_insert_node (self, x)) {
323
+ free (x->key);
324
+ if (self->autofree)
325
+ free (value);
326
+ free (x);
327
+ return -1;
328
+ }
329
+
330
+ s_insert_recolor (self, x);
331
+ return 0;
332
+ }
333
+
334
+ // --------------------------------------------------------------------------
335
+ // Update node into tree with specified key and value.
336
+ // If key is already present, destroys old node and inserts new one.
337
+ // Use free_fn method to ensure deallocator is properly called on value.
338
+
339
+ void
340
+ ztree_update (ztree_t *self, const char *key, void *value)
341
+ {
342
+ assert (self);
343
+ assert (key);
344
+
345
+ node_t *x = zmalloc (sizeof (node_t));
346
+ if (!x)
347
+ return;
348
+
349
+ // If necessary, take duplicate of item (string) value
350
+ if (self->autofree)
351
+ value = strdup ((char *) value);
352
+
353
+ x->key = strdup (key);
354
+ x->value = value;
355
+ x->red = true;
356
+
357
+ node_t *y = s_insert_node (self, x);
358
+ if (y) {
359
+ s_free_value (self, y);
360
+ y->value = value;
361
+ free (x->key);
362
+ free (x);
363
+ } else {
364
+ s_insert_recolor (self, x);
365
+ }
366
+ }
367
+
368
+ // Performs rotations and changes colors to restore red-black
369
+ // properties after a node is deleted
370
+
371
+ static void s_delete_fix (ztree_t *tree, node_t *x)
372
+ {
373
+ node_t *root = tree->root.left;
374
+
375
+ while( (!x->red) && (root != x)) {
376
+ if (x == x->parent->left) {
377
+ node_t *w = x->parent->right;
378
+ if (w->red) {
379
+ w->red = false;
380
+ x->parent->red = true;
381
+ s_left_rotate (tree,x->parent);
382
+ w = x->parent->right;
383
+ }
384
+ if ( (!w->right->red) && (!w->left->red) ) {
385
+ w->red = true;
386
+ x = x->parent;
387
+ } else {
388
+ if (!w->right->red) {
389
+ w->left->red = false;
390
+ w->red = true;
391
+ s_right_rotate (tree,w);
392
+ w = x->parent->right;
393
+ }
394
+ w->red = x->parent->red;
395
+ x->parent->red = false;
396
+ w->right->red = false;
397
+ s_left_rotate (tree,x->parent);
398
+ x = root; // this is to exit while loop
399
+ }
400
+ } else { // the code below is has left and right switched from above
401
+ node_t *w = x->parent->left;
402
+ if (w->red) {
403
+ w->red = false;
404
+ x->parent->red = true;
405
+ s_right_rotate (tree,x->parent);
406
+ w = x->parent->left;
407
+ }
408
+ if ( (!w->right->red) && (!w->left->red) ) {
409
+ w->red = true;
410
+ x = x->parent;
411
+ } else {
412
+ if (!w->left->red) {
413
+ w->right->red = false;
414
+ w->red = true;
415
+ s_left_rotate (tree,w);
416
+ w = x->parent->left;
417
+ }
418
+ w->red=x->parent->red;
419
+ x->parent->red = false;
420
+ w->left->red = false;
421
+ s_right_rotate (tree,x->parent);
422
+ x = root; // this is to exit while loop
423
+ }
424
+ }
425
+ }
426
+ x->red = false;
427
+
428
+ assert(!tree->nil.red);
429
+ }
430
+
431
+ // Helper function return a pointer to the smallest key greater than x
432
+
433
+ static node_t *
434
+ s_successor (ztree_t *tree, const node_t *x)
435
+ {
436
+ node_t *y;
437
+ node_t *nil = &tree->nil;
438
+ node_t *root = &tree->root;
439
+
440
+ if (nil != (y = x->right)) { // assignment to y is intentional
441
+ while (y->left != nil) { // returns the minium of the right subtree of x
442
+ y = y->left;
443
+ }
444
+ } else {
445
+ y = x->parent;
446
+ while (x == y->right) { // sentinel used instead of checking for nil
447
+ x = y;
448
+ y = y->parent;
449
+ }
450
+
451
+ if (y == root)
452
+ return nil;
453
+ }
454
+
455
+ return y;
456
+ }
457
+
458
+ // Helper function to delete node from tree
459
+ // The algorithm from this function is from _Introduction_To_Algorithms_
460
+ static void
461
+ s_delete (ztree_t *tree, node_t *z)
462
+ {
463
+ node_t *x, *y;
464
+ node_t *nil = &tree->nil;
465
+ node_t *root = &tree->root;
466
+
467
+ if (z->left == nil || z->right == nil)
468
+ y = z;
469
+ else
470
+ y = s_successor (tree, z);
471
+
472
+ x = (y->left == nil) ? y->right : y->left;
473
+
474
+ if (root == (x->parent = y->parent)) { // assignment of y->p to x->p is intentional
475
+ root->left = x;
476
+ } else {
477
+ if (y == y->parent->left)
478
+ y->parent->left = x;
479
+ else
480
+ y->parent->right = x;
481
+ }
482
+
483
+ if (y != z) {
484
+ assert(y != &tree->nil);
485
+
486
+ // y is the node to splice out and x is its child
487
+ if (!y->red)
488
+ s_delete_fix (tree, x);
489
+
490
+ free (z->key);
491
+ s_free_value (tree, z);
492
+
493
+ y->left = z->left;
494
+ y->right = z->right;
495
+ y->parent = z->parent;
496
+ y->red = z->red;
497
+ z->left->parent = z->right->parent=y;
498
+ if (z == z->parent->left)
499
+ z->parent->left = y;
500
+ else
501
+ z->parent->right = y;
502
+ free(z);
503
+ } else {
504
+ free (y->key);
505
+ s_free_value (tree, y);
506
+ if (!y->red)
507
+ s_delete_fix (tree, x);
508
+ free(y);
509
+ }
510
+
511
+ assert(!tree->nil.red);
512
+ }
513
+
514
+
515
+ // --------------------------------------------------------------------------
516
+ // Remove a node specified by key from the tree. If there was no such
517
+ // node, this function does nothing.
518
+
519
+ void
520
+ ztree_delete (ztree_t *self, const char *key)
521
+ {
522
+ assert (self);
523
+ assert (key);
524
+
525
+ node_t *x = s_lookup (self, key);
526
+ if (x != &self->nil)
527
+ s_delete (self, x);
528
+ }
529
+
530
+
531
+ // --------------------------------------------------------------------------
532
+ // Look for node in tree and return its value, or NULL
533
+
534
+ void *
535
+ ztree_lookup (ztree_t *self, const char *key)
536
+ {
537
+ assert (self);
538
+ assert (key);
539
+
540
+ node_t *n = s_lookup (self, key);
541
+ return (n == &self->nil) ? NULL : n->value;
542
+ }
543
+
544
+ // --------------------------------------------------------------------------
545
+ // Set a free function for the specified tree node. When the ndoe is
546
+ // destroyed, the free function, if any, is called on that node.
547
+ // Use this when tree nodes are dynamically allocated, to ensure that
548
+ // you don't have memory leaks. You can pass 'free' or NULL as a free_fn.
549
+ // Returns the value, or NULL if there is no such node.
550
+
551
+ void *
552
+ ztree_freefn (ztree_t *self, const char *key, ztree_free_fn *free_fn)
553
+ {
554
+ assert (self);
555
+ assert (key);
556
+
557
+ node_t *x = ztree_lookup (self, key);
558
+ if (x) {
559
+ x->free_fn = free_fn;
560
+ return x->value;
561
+ }
562
+ else
563
+ return NULL;
564
+ }
565
+
566
+ // Local helper function
567
+ // Recursively walk tree, stopping if callback returns non-zero
568
+
569
+ static int s_walk (ztree_t *tree, node_t *x,
570
+ ztree_walk_fn *callback, void *argument)
571
+ {
572
+ if (x == &tree->nil)
573
+ return 0; // empty
574
+
575
+ int ret = s_walk (tree, x->left, callback, argument);
576
+ if (ret == 0)
577
+ ret = (*callback) (x->key, x->value, argument);
578
+ if (ret == 0)
579
+ ret = s_walk (tree, x->right, callback, argument);
580
+ return ret;
581
+ }
582
+
583
+ // --------------------------------------------------------------------------
584
+ // Apply function to each node in the tree. Nodes are iterated in order
585
+ // defined by comparison function. Stops if callback function returns
586
+ // non-zero and returns final return code from callback function
587
+ // (zero = success).
588
+
589
+ int
590
+ ztree_walk (ztree_t *self, ztree_walk_fn *callback, void *argument)
591
+ {
592
+ assert (self);
593
+ return s_walk (self, self->root.left, callback, argument);
594
+ }
595
+
596
+ // Local helper to count nodes
597
+ static int s_count (const char *key, void *value, void *arg)
598
+ {
599
+ *(size_t *) arg += 1;
600
+ return 0;
601
+ }
602
+
603
+
604
+ size_t
605
+ ztree_size (ztree_t *self)
606
+ {
607
+ assert (self);
608
+ size_t nodes = 0;
609
+ s_walk (self, self->root.left, s_count, &nodes);
610
+ return nodes;
611
+ }
612
+
613
+ // Local helper to insert key into list
614
+ static int s_addkey (const char *key, void *value, void *arg)
615
+ {
616
+ zlist_t *keys = arg;
617
+ zlist_append (keys, (char *) key);
618
+ return 0;
619
+ }
620
+
621
+ // -------------------------------------------------------------------------
622
+ // Return keys for nodes in tree
623
+
624
+ zlist_t *
625
+ ztree_keys (ztree_t *self)
626
+ {
627
+ assert (self);
628
+ zlist_t *keys = zlist_new ();
629
+ zlist_autofree (keys);
630
+ s_walk (self, self->root.left, s_addkey, keys);
631
+ return keys;
632
+ }
633
+
634
+ // Local helper to duplicate tree
635
+ static int s_dupkey (const char *key, void *value, void *arg)
636
+ {
637
+ ztree_t *copy = arg;
638
+ ztree_insert (copy, key, value);
639
+ return 0;
640
+ }
641
+
642
+ // --------------------------------------------------------------------------
643
+ // Make copy of tree
644
+ // Does not copy values themselves. Could be faster since in order
645
+ // inserts cause lots of rebalancing.
646
+ // NOTE: only works with values that are strings
647
+ // since there's no other way to know how to duplicate the value.
648
+
649
+ ztree_t *
650
+ ztree_dup (ztree_t *self)
651
+ {
652
+ if (!self)
653
+ return NULL;
654
+
655
+ ztree_t *copy = ztree_new (self->compare);
656
+ if (copy) {
657
+ ztree_autofree (copy);
658
+ ztree_walk (self, s_dupkey, copy);
659
+ }
660
+
661
+ return copy;
662
+ }
663
+
664
+ // Local helper function to print node to file
665
+
666
+ static int s_print (const char *key, void *value, void *arg)
667
+ {
668
+ FILE *handle = arg;
669
+ fprintf (handle, "%s=%s\n", key, (char *) value);
670
+ return 0;
671
+ }
672
+
673
+ // --------------------------------------------------------------------------
674
+ // Save tree to a text file in name=value format
675
+ // Values must be printable strings; keys may not contain '=' character
676
+ // Returns 0 if OK, else -1 if a file error occurred
677
+
678
+ int
679
+ ztree_save (ztree_t *self, const char *filename)
680
+ {
681
+ assert (self);
682
+ FILE *handle = fopen (filename, "w");
683
+ if (!handle)
684
+ return -1; // Failed to create file
685
+
686
+ s_walk (self, self->root.left, s_print, handle);
687
+ fclose (handle);
688
+ return 0;
689
+ }
690
+
691
+ // --------------------------------------------------------------------------
692
+ // Load tree from a text file in name=value format; tree must
693
+ // already exist. Values must printable strings; keys may not contain
694
+ // '=' character. Returns 0 if OK, else -1 if a file was not readable.
695
+
696
+ int
697
+ ztree_load (ztree_t *self, const char *filename)
698
+ {
699
+ assert (self);
700
+ ztree_autofree (self);
701
+
702
+ FILE *handle = fopen (filename, "r");
703
+ if (!handle)
704
+ return -1; // Failed to create file
705
+
706
+ char buffer [1024];
707
+ while (fgets (buffer, 1024, handle)) {
708
+ // Buffer will end in newline, which we don't want
709
+ if (buffer [strlen (buffer) - 1] == '\n')
710
+ buffer [strlen (buffer) - 1] = 0;
711
+ // Split at equals, if any
712
+ char *equals = strchr (buffer, '=');
713
+ if (!equals)
714
+ break; // Some error, stop parsing it
715
+ *equals++ = 0;
716
+ ztree_update (self, buffer, equals);
717
+ }
718
+ fclose (handle);
719
+ return 0;
720
+ }
721
+
722
+ // --------------------------------------------------------------------------
723
+ // Set tree for automatic value destruction
724
+
725
+ void
726
+ ztree_autofree (ztree_t *self)
727
+ {
728
+ assert (self);
729
+ self->autofree = true;
730
+ }
731
+
732
+ static int test_walk (const char *key, void *value, void *arg)
733
+ {
734
+ assert (NULL != ztree_lookup ((ztree_t *) arg, key));
735
+ return 0;
736
+ }
737
+
738
+ static int test_walk_error (const char *key, void *value, void *arg)
739
+ {
740
+ return -1;
741
+ }
742
+
743
+ void
744
+ ztree_test (int verbose)
745
+ {
746
+ printf (" * ztree: ");
747
+
748
+ // @selftest
749
+ ztree_t *tree = ztree_new (strcmp);
750
+ assert (tree);
751
+ assert (ztree_size (tree) == 0);
752
+
753
+ assert (ztree_lookup (tree, "NOTHING") == NULL);
754
+
755
+ // Insert some nodes
756
+ int rc;
757
+ rc = ztree_insert (tree, "DEADBEEF", "dead beef");
758
+ assert (rc == 0);
759
+ rc = ztree_insert (tree, "ABADCAFE", "a bad cafe");
760
+ assert (rc == 0);
761
+ rc = ztree_insert (tree, "C0DEDBAD", "coded bad");
762
+ assert (rc == 0);
763
+ rc = ztree_insert (tree, "DEADF00D", "dead food");
764
+ assert (rc == 0);
765
+ assert (ztree_size (tree) == 4);
766
+
767
+ // Look for existing nodes
768
+ char *value;
769
+ value = (char *) ztree_lookup (tree, "DEADBEEF");
770
+ assert (streq (value, "dead beef"));
771
+ value = (char *) ztree_lookup (tree, "ABADCAFE");
772
+ assert (streq (value, "a bad cafe"));
773
+ value = (char *) ztree_lookup (tree, "C0DEDBAD");
774
+ assert (streq (value, "coded bad"));
775
+ value = (char *) ztree_lookup (tree, "DEADF00D");
776
+ assert (streq (value, "dead food"));
777
+
778
+ // Look for non-existent nodes
779
+ value = (char *) ztree_lookup (tree, "foo");
780
+ assert (value == NULL);
781
+
782
+ // Try to insert duplicate nodes
783
+ rc = ztree_insert (tree, "DEADBEEF", "foo");
784
+ assert (rc == -1);
785
+ value = (char *) ztree_lookup (tree, "DEADBEEF");
786
+ assert (streq (value, "dead beef"));
787
+
788
+ // Test keys method
789
+ zlist_t *keys = ztree_keys (tree);
790
+ assert (zlist_size (keys) == 4);
791
+
792
+ // Test that keys are in order
793
+ void *key, *pred;
794
+ pred = zlist_first (keys);
795
+ assert (pred);
796
+ while ((key = zlist_next (keys))) {
797
+ assert (strcmp (key, pred) > 0);
798
+ pred = key;
799
+ }
800
+ zlist_destroy (&keys);
801
+
802
+ // Test dup method
803
+ ztree_t *copy = ztree_dup (tree);
804
+ assert (ztree_size (copy) == ztree_size (tree));
805
+ value = (char *) ztree_lookup (copy, "DEADF00D");
806
+ assert (value);
807
+ assert (streq (value, "dead food"));
808
+ ztree_destroy (&copy);
809
+
810
+ // Test walk
811
+ assert (0 == ztree_walk (tree, test_walk, tree));
812
+ assert (-1 == ztree_walk (tree, test_walk_error, tree));
813
+
814
+ // Test save and load
815
+ ztree_save (tree, ".cache");
816
+ copy = ztree_new (strcasecmp);
817
+ ztree_load (copy, ".cache");
818
+ assert (ztree_size (copy) == ztree_size (tree));
819
+ value = (char *) ztree_lookup (copy, "DEADBEEF");
820
+ assert (value);
821
+ assert (streq (value, "dead beef"));
822
+ ztree_destroy (&copy);
823
+ #if (defined (WIN32))
824
+ DeleteFile (".cache");
825
+ #else
826
+ unlink (".cache");
827
+ #endif
828
+
829
+ // Delete some nodes
830
+ assert (ztree_size (tree) == 4);
831
+ ztree_delete (tree, "DEADF00D");
832
+ value = (char *) ztree_lookup (tree, "DEADF00D");
833
+ assert (value == NULL);
834
+ assert (ztree_size (tree) == 3);
835
+
836
+ ztree_delete (tree, "C0DEDBAD");
837
+ value = (char *) ztree_lookup (tree, "C0DEDBAD");
838
+ assert (value == NULL);
839
+ assert (ztree_size (tree) == 2);
840
+
841
+ // Change value of an existing node
842
+ ztree_update (tree, "ABADCAFE", "A Bad Cafe");
843
+ value = (char *) ztree_lookup (tree, "ABADCAFE");
844
+ assert (value != NULL);
845
+ assert (streq(value, "A Bad Cafe"));
846
+
847
+ // Update with non-existant node
848
+ ztree_update (tree, "C0DEDEBAD", "Coded Bad");
849
+ value = (char *) ztree_lookup (tree, "C0DEDEBAD");
850
+ assert (value);
851
+ assert (streq(value, "Coded Bad"));
852
+
853
+ // Check that the queue is robust against random usage
854
+ struct {
855
+ char name [100];
856
+ bool exists;
857
+ } testset [200];
858
+ memset (testset, 0, sizeof (testset));
859
+ int testmax = 200, testnbr, iteration;
860
+
861
+ srandom ((unsigned) time (NULL));
862
+ for (iteration = 0; iteration < 25000; iteration++) {
863
+ testnbr = randof (testmax);
864
+ if (testset [testnbr].exists) {
865
+ value = (char *) ztree_lookup (tree, testset [testnbr].name);
866
+ assert (value);
867
+ ztree_delete (tree, testset [testnbr].name);
868
+ testset [testnbr].exists = false;
869
+ }
870
+ else {
871
+ sprintf (testset [testnbr].name, "%x-%x", rand (), rand ());
872
+ if (ztree_insert (tree, testset [testnbr].name, "") == 0)
873
+ testset [testnbr].exists = true;
874
+ }
875
+ }
876
+
877
+ // Test 10K lookups
878
+ for (iteration = 0; iteration < 10000; iteration++)
879
+ value = (char *) ztree_lookup (tree, "DEADBEEFABADCAFE");
880
+
881
+ // Destructor should be safe to call twice
882
+ ztree_destroy (&tree);
883
+ ztree_destroy (&tree);
884
+ assert (tree == NULL);
885
+ // @end
886
+
887
+ printf ("OK\n");
888
+ }