protobuf 2.2.5-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (256) hide show
  1. data/.gitignore +17 -0
  2. data/.travis.yml +9 -0
  3. data/.yardopts +5 -0
  4. data/Gemfile +3 -0
  5. data/README.md +316 -0
  6. data/Rakefile +29 -0
  7. data/UPGRADING.md +60 -0
  8. data/bin/rpc_server +5 -0
  9. data/bin/rprotoc +62 -0
  10. data/examples/addressbook.pb.rb +55 -0
  11. data/examples/addressbook.proto +24 -0
  12. data/examples/reading_a_message.rb +32 -0
  13. data/examples/writing_a_message.rb +46 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  92. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  93. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  94. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  95. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  96. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  97. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  98. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  99. data/ext/ruby_generator/Makefile +10 -0
  100. data/ext/ruby_generator/RubyGenerator.cpp +450 -0
  101. data/ext/ruby_generator/RubyGenerator.h +199 -0
  102. data/ext/ruby_generator/extconf.rb +36 -0
  103. data/ext/ruby_generator/protoc-ruby +0 -0
  104. data/lib/protobuf/cli.rb +188 -0
  105. data/lib/protobuf/enum.rb +58 -0
  106. data/lib/protobuf/enum_value.rb +59 -0
  107. data/lib/protobuf/evented.rb +22 -0
  108. data/lib/protobuf/exceptions.rb +11 -0
  109. data/lib/protobuf/ext/eventmachine.rb +14 -0
  110. data/lib/protobuf/field/base_field.rb +240 -0
  111. data/lib/protobuf/field/bool_field.rb +36 -0
  112. data/lib/protobuf/field/bytes_field.rb +38 -0
  113. data/lib/protobuf/field/double_field.rb +19 -0
  114. data/lib/protobuf/field/enum_field.rb +50 -0
  115. data/lib/protobuf/field/extension_fields.rb +32 -0
  116. data/lib/protobuf/field/field_array.rb +65 -0
  117. data/lib/protobuf/field/fixed32_field.rb +19 -0
  118. data/lib/protobuf/field/fixed64_field.rb +22 -0
  119. data/lib/protobuf/field/float_field.rb +31 -0
  120. data/lib/protobuf/field/int32_field.rb +12 -0
  121. data/lib/protobuf/field/int64_field.rb +12 -0
  122. data/lib/protobuf/field/integer_field.rb +19 -0
  123. data/lib/protobuf/field/message_field.rb +53 -0
  124. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  125. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  126. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  127. data/lib/protobuf/field/sint32_field.rb +12 -0
  128. data/lib/protobuf/field/sint64_field.rb +12 -0
  129. data/lib/protobuf/field/string_field.rb +14 -0
  130. data/lib/protobuf/field/uint32_field.rb +12 -0
  131. data/lib/protobuf/field/uint64_field.rb +12 -0
  132. data/lib/protobuf/field/varint_field.rb +61 -0
  133. data/lib/protobuf/field.rb +57 -0
  134. data/lib/protobuf/logger.rb +86 -0
  135. data/lib/protobuf/message/decoder.rb +83 -0
  136. data/lib/protobuf/message/encoder.rb +48 -0
  137. data/lib/protobuf/message/extend.rb +8 -0
  138. data/lib/protobuf/message/message.rb +1 -0
  139. data/lib/protobuf/message.rb +320 -0
  140. data/lib/protobuf/rpc/buffer.rb +79 -0
  141. data/lib/protobuf/rpc/client.rb +166 -0
  142. data/lib/protobuf/rpc/connector.rb +19 -0
  143. data/lib/protobuf/rpc/connectors/base.rb +38 -0
  144. data/lib/protobuf/rpc/connectors/common.rb +156 -0
  145. data/lib/protobuf/rpc/connectors/em_client.rb +84 -0
  146. data/lib/protobuf/rpc/connectors/eventmachine.rb +87 -0
  147. data/lib/protobuf/rpc/connectors/socket.rb +73 -0
  148. data/lib/protobuf/rpc/connectors/zmq.rb +69 -0
  149. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  150. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  151. data/lib/protobuf/rpc/error.rb +25 -0
  152. data/lib/protobuf/rpc/rpc.pb.rb +118 -0
  153. data/lib/protobuf/rpc/server.rb +89 -0
  154. data/lib/protobuf/rpc/servers/evented/server.rb +41 -0
  155. data/lib/protobuf/rpc/servers/evented_runner.rb +21 -0
  156. data/lib/protobuf/rpc/servers/socket/server.rb +111 -0
  157. data/lib/protobuf/rpc/servers/socket/worker.rb +66 -0
  158. data/lib/protobuf/rpc/servers/socket_runner.rb +27 -0
  159. data/lib/protobuf/rpc/servers/zmq/broker.rb +87 -0
  160. data/lib/protobuf/rpc/servers/zmq/server.rb +50 -0
  161. data/lib/protobuf/rpc/servers/zmq/util.rb +27 -0
  162. data/lib/protobuf/rpc/servers/zmq/worker.rb +60 -0
  163. data/lib/protobuf/rpc/servers/zmq_runner.rb +25 -0
  164. data/lib/protobuf/rpc/service.rb +173 -0
  165. data/lib/protobuf/rpc/service_dispatcher.rb +130 -0
  166. data/lib/protobuf/rpc/service_filters.rb +267 -0
  167. data/lib/protobuf/rpc/stat.rb +83 -0
  168. data/lib/protobuf/socket.rb +22 -0
  169. data/lib/protobuf/version.rb +4 -0
  170. data/lib/protobuf/wire_type.rb +10 -0
  171. data/lib/protobuf/zmq.rb +21 -0
  172. data/lib/protobuf.rb +86 -0
  173. data/proto/rpc.pb.rb +48 -0
  174. data/proto/rpc.proto +73 -0
  175. data/protobuf.gemspec +44 -0
  176. data/spec/benchmark/tasks.rb +179 -0
  177. data/spec/functional/embedded_service_spec.rb +7 -0
  178. data/spec/functional/evented_server_spec.rb +64 -0
  179. data/spec/functional/socket_server_spec.rb +58 -0
  180. data/spec/functional/zmq_server_spec.rb +58 -0
  181. data/spec/lib/protobuf/cli_spec.rb +212 -0
  182. data/spec/lib/protobuf/enum_spec.rb +98 -0
  183. data/spec/lib/protobuf/enum_value_spec.rb +15 -0
  184. data/spec/lib/protobuf/logger_spec.rb +131 -0
  185. data/spec/lib/protobuf/message/encoder_spec.rb +19 -0
  186. data/spec/lib/protobuf/message_spec.rb +209 -0
  187. data/spec/lib/protobuf/rpc/client_spec.rb +158 -0
  188. data/spec/lib/protobuf/rpc/connector_spec.rb +32 -0
  189. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +50 -0
  190. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +128 -0
  191. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +36 -0
  192. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +22 -0
  193. data/spec/lib/protobuf/rpc/servers/evented_server_spec.rb +18 -0
  194. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +43 -0
  195. data/spec/lib/protobuf/rpc/servers/zmq/broker_spec.rb +35 -0
  196. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +41 -0
  197. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +45 -0
  198. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +44 -0
  199. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +116 -0
  200. data/spec/lib/protobuf/rpc/service_filters_spec.rb +451 -0
  201. data/spec/lib/protobuf/rpc/service_spec.rb +165 -0
  202. data/spec/lib/protobuf_spec.rb +62 -0
  203. data/spec/spec_helper.rb +51 -0
  204. data/spec/support/all.rb +6 -0
  205. data/spec/support/server.rb +101 -0
  206. data/spec/support/test/enum.pb.rb +34 -0
  207. data/spec/support/test/enum.proto +12 -0
  208. data/spec/support/test/resource.pb.rb +58 -0
  209. data/spec/support/test/resource.proto +31 -0
  210. data/spec/support/test/resource_service.rb +14 -0
  211. data/spec/support/test_app_file.rb +2 -0
  212. data/spec/support/tolerance_matcher.rb +40 -0
  213. data/test/data/data.bin +3 -0
  214. data/test/data/data_source.py +14 -0
  215. data/test/data/types.bin +0 -0
  216. data/test/data/types_source.py +22 -0
  217. data/test/data/unk.png +0 -0
  218. data/test/proto/addressbook.pb.rb +66 -0
  219. data/test/proto/addressbook.proto +33 -0
  220. data/test/proto/addressbook_base.pb.rb +58 -0
  221. data/test/proto/addressbook_base.proto +26 -0
  222. data/test/proto/addressbook_ext.pb.rb +20 -0
  223. data/test/proto/addressbook_ext.proto +6 -0
  224. data/test/proto/collision.pb.rb +17 -0
  225. data/test/proto/collision.proto +5 -0
  226. data/test/proto/ext_collision.pb.rb +24 -0
  227. data/test/proto/ext_collision.proto +8 -0
  228. data/test/proto/ext_range.pb.rb +22 -0
  229. data/test/proto/ext_range.proto +7 -0
  230. data/test/proto/float_default.proto +10 -0
  231. data/test/proto/lowercase.pb.rb +30 -0
  232. data/test/proto/lowercase.proto +9 -0
  233. data/test/proto/merge.pb.rb +39 -0
  234. data/test/proto/merge.proto +15 -0
  235. data/test/proto/nested.pb.rb +30 -0
  236. data/test/proto/nested.proto +9 -0
  237. data/test/proto/optional_field.pb.rb +35 -0
  238. data/test/proto/optional_field.proto +12 -0
  239. data/test/proto/packed.pb.rb +22 -0
  240. data/test/proto/packed.proto +6 -0
  241. data/test/proto/rpc.proto +6 -0
  242. data/test/proto/types.pb.rb +84 -0
  243. data/test/proto/types.proto +37 -0
  244. data/test/test_addressbook.rb +56 -0
  245. data/test/test_enum_value.rb +41 -0
  246. data/test/test_extension.rb +36 -0
  247. data/test/test_lowercase.rb +11 -0
  248. data/test/test_message.rb +128 -0
  249. data/test/test_optional_field.rb +103 -0
  250. data/test/test_packed_field.rb +40 -0
  251. data/test/test_parse.rb +15 -0
  252. data/test/test_repeated_types.rb +132 -0
  253. data/test/test_serialize.rb +61 -0
  254. data/test/test_standard_message.rb +96 -0
  255. data/test/test_types.rb +226 -0
  256. metadata +461 -0
@@ -0,0 +1,118 @@
1
+ ### Generated by rprotoc. DO NOT EDIT!
2
+ ### <proto file: rpc.proto>
3
+ # // Copyright (c) 2009 Shardul Deo
4
+ # //
5
+ # // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # // of this software and associated documentation files (the "Software"), to deal
7
+ # // in the Software without restriction, including without limitation the rights
8
+ # // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # // copies of the Software, and to permit persons to whom the Software is
10
+ # // furnished to do so, subject to the following conditions:
11
+ # //
12
+ # // The above copyright notice and this permission notice shall be included in
13
+ # // all copies or substantial portions of the Software.
14
+ # //
15
+ # // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # // THE SOFTWARE.
22
+ #
23
+ # // Author: Shardul Deo
24
+ # //
25
+ # // Protobufs needed for socket rpcs.
26
+ #
27
+ # package protobuf.socketrpc;
28
+ #
29
+ # message Request {
30
+ #
31
+ # // RPC service full name
32
+ # required string service_name = 1;
33
+ #
34
+ # // RPC method name
35
+ # required string method_name = 2;
36
+ #
37
+ # // RPC request proto
38
+ # required bytes request_proto = 3;
39
+ # }
40
+ #
41
+ # message Response {
42
+ #
43
+ # // RPC response proto
44
+ # optional bytes response_proto = 1;
45
+ #
46
+ # // Error, if any
47
+ # optional string error = 2;
48
+ #
49
+ # // Was callback invoked
50
+ # optional bool callback = 3 [default = false];
51
+ #
52
+ # // Error Reason
53
+ # optional ErrorReason error_reason = 4;
54
+ # }
55
+ #
56
+ # // Possible error reasons
57
+ # // The server-side errors are returned in the response from the server.
58
+ # // The client-side errors are returned by the client-side code when it doesn't
59
+ # // have a response from the server.
60
+ # enum ErrorReason {
61
+ #
62
+ # // Server-side errors
63
+ # BAD_REQUEST_DATA = 0; // Server received bad request data
64
+ # BAD_REQUEST_PROTO = 1; // Server received bad request proto
65
+ # SERVICE_NOT_FOUND = 2; // Service not found on server
66
+ # METHOD_NOT_FOUND = 3; // Method not found on server
67
+ # RPC_ERROR = 4; // Rpc threw exception on server
68
+ # RPC_FAILED = 5; // Rpc failed on server
69
+ #
70
+ # // Client-side errors (these are returned by the client-side code)
71
+ # INVALID_REQUEST_PROTO = 6; // Rpc was called with invalid request proto
72
+ # BAD_RESPONSE_PROTO = 7; // Server returned a bad response proto
73
+ # UNKNOWN_HOST = 8; // Could not find supplied host
74
+ # IO_ERROR = 9; // I/O error while communicating with server
75
+ # }
76
+ require 'protobuf/message'
77
+
78
+ module Protobuf
79
+ module Socketrpc
80
+ ##
81
+ # Enum Classes
82
+ #
83
+ class ErrorReason < ::Protobuf::Enum; end
84
+
85
+ ##
86
+ # Message Classes
87
+ #
88
+ class Request < ::Protobuf::Message; end
89
+ class Response < ::Protobuf::Message; end
90
+
91
+ ##
92
+ # Enum Values
93
+ #
94
+ ::Protobuf::Socketrpc::ErrorReason.define :BAD_REQUEST_DATA, 0
95
+ ::Protobuf::Socketrpc::ErrorReason.define :BAD_REQUEST_PROTO, 1
96
+ ::Protobuf::Socketrpc::ErrorReason.define :SERVICE_NOT_FOUND, 2
97
+ ::Protobuf::Socketrpc::ErrorReason.define :METHOD_NOT_FOUND, 3
98
+ ::Protobuf::Socketrpc::ErrorReason.define :RPC_ERROR, 4
99
+ ::Protobuf::Socketrpc::ErrorReason.define :RPC_FAILED, 5
100
+ ::Protobuf::Socketrpc::ErrorReason.define :INVALID_REQUEST_PROTO, 6
101
+ ::Protobuf::Socketrpc::ErrorReason.define :BAD_RESPONSE_PROTO, 7
102
+ ::Protobuf::Socketrpc::ErrorReason.define :UNKNOWN_HOST, 8
103
+ ::Protobuf::Socketrpc::ErrorReason.define :IO_ERROR, 9
104
+
105
+ ##
106
+ # Message Fields
107
+ #
108
+ ::Protobuf::Socketrpc::Request.required(::Protobuf::Field::StringField, :service_name, 1)
109
+ ::Protobuf::Socketrpc::Request.required(::Protobuf::Field::StringField, :method_name, 2)
110
+ ::Protobuf::Socketrpc::Request.required(::Protobuf::Field::BytesField, :request_proto, 3)
111
+
112
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Field::BytesField, :response_proto, 1)
113
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Field::StringField, :error, 2)
114
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Field::BoolField, :callback, 3, :default => false)
115
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Socketrpc::ErrorReason, :error_reason, 4)
116
+
117
+ end
118
+ end
@@ -0,0 +1,89 @@
1
+ require 'protobuf/logger'
2
+ require 'protobuf/rpc/rpc.pb'
3
+ require 'protobuf/rpc/buffer'
4
+ require 'protobuf/rpc/error'
5
+ require 'protobuf/rpc/stat'
6
+ require 'protobuf/rpc/service_dispatcher'
7
+
8
+ module Protobuf
9
+ module Rpc
10
+ module Server
11
+
12
+ def initialize_request!
13
+ log_debug { sign_message('Post init') }
14
+ @request = ::Protobuf::Socketrpc::Request.new
15
+ @response = ::Protobuf::Socketrpc::Response.new
16
+ @stats = Protobuf::Rpc::Stat.new(:SERVER)
17
+ set_peer
18
+ end
19
+
20
+ def disable_gc!
21
+ ::GC.disable if ::Protobuf.gc_pause_server_request?
22
+ end
23
+
24
+ def enable_gc!
25
+ ::GC.enable && ::GC.start if ::Protobuf.gc_pause_server_request?
26
+ end
27
+
28
+ # no-op, implemented by including class if desired.
29
+ def set_peer; end
30
+
31
+ # Invoke the service method dictated by the proto wrapper request object
32
+ def handle_client
33
+ parse_request_from_buffer
34
+ @dispatcher = ::Protobuf::Rpc::ServiceDispatcher.new(@request)
35
+ @stats.dispatcher = @dispatcher
36
+
37
+ disable_gc!
38
+ @dispatcher.invoke!
39
+ if @dispatcher.success?
40
+ @response.response_proto = @dispatcher.response
41
+ else
42
+ handle_error(@dispatcher.error)
43
+ end
44
+ rescue => error
45
+ log_exception(error)
46
+ handle_error(error)
47
+ ensure
48
+ send_response
49
+ end
50
+
51
+ # Client error handler. Receives an exception object and writes it into the @response
52
+ def handle_error(error)
53
+ log_debug { sign_message("handle_error: #{error.inspect}") }
54
+ if error.respond_to?(:to_response)
55
+ error.to_response(@response)
56
+ else
57
+ message = error.respond_to?(:message) ? error.message : error.to_s
58
+ code = error.respond_to?(:code) ? error.code : 'RPC_ERROR'
59
+ ::Protobuf::Rpc::PbError.new(message, code).to_response(@response)
60
+ end
61
+ end
62
+
63
+ def log_signature
64
+ @_log_signature ||= "[server-#{self.class.name}]"
65
+ end
66
+
67
+ # Parse the incoming request object into our expected request object
68
+ def parse_request_from_buffer
69
+ log_debug { sign_message("Parsing request from buffer: #{@request_data}") }
70
+ @request.parse_from_string(@request_data)
71
+ rescue => error
72
+ exc = ::Protobuf::Rpc::BadRequestData.new("Unable to parse request: #{error.message}")
73
+ log_error { exc.message }
74
+ raise exc
75
+ end
76
+
77
+ # Write the response wrapper to the client
78
+ def send_response
79
+ log_debug { sign_message("Sending response to client: #{@response.inspect}") }
80
+ send_data
81
+ ensure
82
+ @stats.stop
83
+ log_info { @stats.to_s }
84
+ enable_gc!
85
+ end
86
+ end
87
+ end
88
+ end
89
+
@@ -0,0 +1,41 @@
1
+ require 'protobuf/rpc/server'
2
+
3
+ module Protobuf
4
+ module Rpc
5
+ module Evented
6
+ class Server < ::EventMachine::Connection
7
+ include ::Protobuf::Rpc::Server
8
+ include ::Protobuf::Logger::LogMethods
9
+
10
+ # Initialize a new read buffer for storing client request info
11
+ def post_init
12
+ initialize_request!
13
+ @request_buffer = Protobuf::Rpc::Buffer.new(:read)
14
+ end
15
+
16
+ # Receive a chunk of data, potentially flushed to handle_client
17
+ def receive_data(data)
18
+ log_debug { sign_message("receive_data: #{data}") }
19
+
20
+ @request_buffer << data
21
+ @request_data = @request_buffer.data
22
+ @stats.request_size = @request_buffer.size
23
+
24
+ handle_client if @request_buffer.flushed?
25
+ end
26
+
27
+ def send_data
28
+ response_buffer = Protobuf::Rpc::Buffer.new(:write)
29
+ response_buffer.set_data(@response)
30
+ @stats.response_size = response_buffer.size
31
+ log_debug { sign_message("sending data: #{response_buffer.inspect}") }
32
+ super(response_buffer.write)
33
+ end
34
+
35
+ def set_peer
36
+ @stats.client = ::Socket.unpack_sockaddr_in(get_peername)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ module Protobuf
2
+ module Rpc
3
+ class EventedRunner
4
+
5
+ def self.stop
6
+ ::EventMachine.stop_event_loop if ::EventMachine.reactor_running?
7
+ end
8
+
9
+ def self.run(options)
10
+ # Startup and run the rpc server
11
+ ::EventMachine.schedule do
12
+ ::EventMachine.start_server(options[:host], options[:port], ::Protobuf::Rpc::Evented::Server)
13
+ end
14
+
15
+ # Join or start the reactor
16
+ yield if block_given?
17
+ ::EM.reactor_running? ? ::EM.reactor_thread.join : ::EM.run
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,111 @@
1
+ require 'protobuf/rpc/server'
2
+ require 'protobuf/rpc/servers/socket/worker'
3
+
4
+ module Protobuf
5
+ module Rpc
6
+ module Socket
7
+
8
+ class Server
9
+ include ::Protobuf::Rpc::Server
10
+ include ::Protobuf::Logger::LogMethods
11
+
12
+ AUTO_COLLECT_TIMEOUT = 20 # seconds
13
+
14
+ def self.cleanup?
15
+ # every 10 connections run a cleanup routine after closing the response
16
+ @threads.size > (@threshold - 1) && (@threads.size % @threshold) == 0
17
+ end
18
+
19
+ def self.cleanup_threads
20
+ log_debug { sign_message("Thread cleanup - #{@threads.size} - start") }
21
+
22
+ @threads = @threads.select do |t|
23
+ if t[:thread].alive?
24
+ true
25
+ else
26
+ t[:thread].join
27
+ @working.delete(t[:socket])
28
+ false
29
+ end
30
+ end
31
+
32
+ log_debug { sign_message("Thread cleanup - #{@threads.size} - complete") }
33
+ end
34
+
35
+ def self.log_signature
36
+ @_log_signature ||= "server-#{self.class.name}"
37
+ end
38
+
39
+ def self.new_worker(socket)
40
+ Thread.new(socket) do |sock|
41
+ ::Protobuf::Rpc::Socket::Worker.new(sock) do |s|
42
+ s.close
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.run(options = {})
48
+ log_debug { sign_message("Run") }
49
+ host = options[:host]
50
+ port = options[:port]
51
+ backlog = options[:backlog]
52
+ @threshold = options[:threshold]
53
+
54
+ @threads = []
55
+ @server = ::TCPServer.new(host, port)
56
+ raise "The server was unable to start properly." if @server.closed?
57
+
58
+ @server.listen(backlog)
59
+ @working = []
60
+ @listen_fds = [@server]
61
+ @running = true
62
+
63
+ while running?
64
+ log_debug { sign_message("Waiting for connections") }
65
+
66
+ if ready_cnxns = IO.select(@listen_fds, [], [], AUTO_COLLECT_TIMEOUT)
67
+ cnxns = ready_cnxns.first
68
+ cnxns.each do |client|
69
+ case
70
+ when !running? then
71
+ # no-op
72
+ when client == @server then
73
+ log_debug { sign_message("Accepted new connection") }
74
+ client, sockaddr = @server.accept
75
+ @listen_fds << client
76
+ else
77
+ if !@working.include?(client)
78
+ @working << @listen_fds.delete(client)
79
+ log_debug { sign_message("Working") }
80
+ @threads << { :thread => new_worker(client), :socket => client }
81
+
82
+ cleanup_threads if cleanup?
83
+ end
84
+ end
85
+ end
86
+ else
87
+ # Run a cleanup if select times out while waiting
88
+ cleanup_threads if @threads.size > 1
89
+ end
90
+ end
91
+
92
+ rescue Errno::EADDRINUSE
93
+ raise
94
+ rescue
95
+ # Closing the server causes the loop to raise an exception here
96
+ raise #if running?
97
+ end
98
+
99
+ def self.running?
100
+ @running
101
+ end
102
+
103
+ def self.stop
104
+ @running = false
105
+ @server.close if @server
106
+ end
107
+ end
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,66 @@
1
+ require 'protobuf/rpc/server'
2
+ require 'protobuf/logger'
3
+
4
+ module Protobuf
5
+ module Rpc
6
+ module Socket
7
+
8
+ class Worker
9
+ include ::Protobuf::Rpc::Server
10
+ include ::Protobuf::Logger::LogMethods
11
+
12
+ def initialize(sock, &complete_cb)
13
+ @socket = sock
14
+ initialize_request!
15
+
16
+ request_buffer = Protobuf::Rpc::Buffer.new(:read)
17
+ @complete_cb = complete_cb
18
+
19
+ log_debug { sign_message("stats are #{@stats.to_s}") }
20
+ request_buffer << read_data
21
+ @request_data = request_buffer.data
22
+
23
+ @stats.request_size = request_buffer.size
24
+
25
+ log_debug { sign_message("handling request") }
26
+ handle_client if request_buffer.flushed?
27
+ end
28
+
29
+ def read_data
30
+ size_io = StringIO.new
31
+
32
+ until (size_reader = @socket.getc) == "-"
33
+ size_io << size_reader
34
+ end
35
+ str_size_io = size_io.string
36
+
37
+ "#{str_size_io}-#{@socket.read(str_size_io.to_i)}"
38
+ end
39
+
40
+ def send_data
41
+ raise 'Socket closed unexpectedly' unless socket_writable?
42
+ response_buffer = Protobuf::Rpc::Buffer.new(:write)
43
+ response_buffer.set_data(@response)
44
+ @stats.response_size = response_buffer.size
45
+ log_debug { sign_message("sending data : #{response_buffer.data}") }
46
+ @socket.write(response_buffer.write)
47
+ @socket.flush
48
+ @complete_cb.call(@socket)
49
+ end
50
+
51
+ def log_signature
52
+ @_log_signature ||= "server-#{self.class}-#{object_id}"
53
+ end
54
+
55
+ def set_peer
56
+ @stats.client = ::Socket.unpack_sockaddr_in(@socket.getpeername)
57
+ end
58
+
59
+ def socket_writable?
60
+ ! @socket.nil? && ! @socket.closed?
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,27 @@
1
+ module Protobuf
2
+ module Rpc
3
+ class SocketRunner
4
+
5
+ def self.stop
6
+ ::Protobuf::Rpc::Socket::Server.stop
7
+ end
8
+
9
+ def self.run(server)
10
+ server_config = case
11
+ when server.is_a?(OpenStruct) then
12
+ server.marshal_dump
13
+ when server.is_a?(Hash) then
14
+ server
15
+ when server.respond_to?(:to_hash) then
16
+ server.to_hash
17
+ else
18
+ raise "Cannot parser Socket Server - server options"
19
+ end
20
+
21
+ yield if block_given?
22
+ ::Protobuf::Rpc::Socket::Server.run(server_config)
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,87 @@
1
+ require 'resolv'
2
+ require 'protobuf/rpc/servers/zmq/util'
3
+
4
+ module Protobuf
5
+ module Rpc
6
+ module Zmq
7
+ class Broker
8
+ include ::Protobuf::Rpc::Zmq::Util
9
+ attr_reader :frontend, :backend, :poller, :context
10
+
11
+ ##
12
+ # Constructor
13
+ #
14
+ def initialize(options = {})
15
+ @context = ::ZMQ::Context.new
16
+ @frontend = setup_frontend(options)
17
+ @backend = setup_backend(options)
18
+ @poller = setup_poller
19
+ end
20
+
21
+ ##
22
+ # Instance Methods
23
+ #
24
+ def poll
25
+ poller.poll(1000)
26
+ poller.readables.each do |socket|
27
+ case socket
28
+ when frontend then
29
+ move_to(backend, socket)
30
+ when backend then
31
+ move_to(frontend, socket)
32
+ end
33
+ end
34
+ end
35
+
36
+ def teardown
37
+ frontend.close
38
+ backend.close
39
+ context.terminate
40
+ end
41
+
42
+ private
43
+
44
+ def move_to(frontend_or_backend, socket)
45
+ more_data = true
46
+
47
+ while more_data do
48
+ socket.recv_string(data = "")
49
+ more_data = socket.more_parts?
50
+ more_data_flag = (more_data ? ::ZMQ::SNDMORE : 0)
51
+ frontend_or_backend.send_string(data, more_data_flag)
52
+ end
53
+ end
54
+
55
+ def setup_backend(options = {})
56
+ dealer_options = options.merge(:port => options[:port] + 1)
57
+ host = dealer_options[:host]
58
+ port = dealer_options[:port]
59
+
60
+ zmq_backend = context.socket(::ZMQ::DEALER)
61
+ zmq_error_check(zmq_backend.bind(bind_address(host, port)))
62
+ zmq_backend
63
+ end
64
+
65
+ def setup_frontend(options = {})
66
+ host = options[:host]
67
+ port = options[:port]
68
+
69
+ zmq_frontend = context.socket(::ZMQ::ROUTER)
70
+ zmq_error_check(zmq_frontend.bind(bind_address(host, port)))
71
+ zmq_frontend
72
+ end
73
+
74
+ def bind_address(host, port)
75
+ "tcp://#{resolve_ip(host)}:#{port}"
76
+ end
77
+
78
+ def setup_poller
79
+ zmq_poller = ::ZMQ::Poller.new
80
+ zmq_poller.register(frontend, ::ZMQ::POLLIN)
81
+ zmq_poller.register(backend, ::ZMQ::POLLIN)
82
+ zmq_poller
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,50 @@
1
+ require 'protobuf/rpc/servers/zmq/broker'
2
+ require 'protobuf/rpc/servers/zmq/worker'
3
+ require 'protobuf/rpc/servers/zmq/util'
4
+
5
+ module Protobuf
6
+ module Rpc
7
+ module Zmq
8
+ class Server
9
+ include ::Protobuf::Rpc::Zmq::Util
10
+
11
+ ##
12
+ # Class Methods
13
+ #
14
+ def self.run(options = {})
15
+ log_debug { sign_message("initializing broker") }
16
+ @broker = ::Protobuf::Rpc::Zmq::Broker.new(options)
17
+ local_worker_threads = options[:threads]
18
+
19
+ worker_options = options.merge(:port => options[:port] + 1)
20
+ log_debug { sign_message("starting server workers") }
21
+ local_worker_threads.times do
22
+ @threads << Thread.new { ::Protobuf::Rpc::Zmq::Worker.new(worker_options).run }
23
+ end
24
+ @running = true
25
+
26
+ log_debug { sign_message("server started") }
27
+ while self.running? do
28
+ @broker.poll
29
+ end
30
+ ensure
31
+ @broker.teardown if @broker
32
+ end
33
+
34
+ def self.running?
35
+ !!@running
36
+ end
37
+
38
+ def self.stop
39
+ @running = false
40
+
41
+ @threads.each do |t|
42
+ t.join
43
+ end
44
+ end
45
+
46
+ @threads ||= []
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ module Protobuf
2
+ module Rpc
3
+ module Zmq
4
+
5
+ module Util
6
+ include ::Protobuf::Logger::LogMethods
7
+ def self.included(base)
8
+ base.extend(::Protobuf::Rpc::Zmq::Util)
9
+ end
10
+
11
+ def zmq_error_check(return_code)
12
+ raise "Last API call failed with \"#{::ZMQ::Util.error_string}\"#{$/}#{$/}#{caller(1)}" unless return_code >= 0
13
+ end
14
+
15
+ def log_signature
16
+ @_log_signature ||= "server-#{self.class}-#{object_id}"
17
+ end
18
+
19
+ def resolve_ip(hostname)
20
+ ::Resolv.getaddress(hostname)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
27
+ end