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,83 @@
1
+ require 'date'
2
+ require 'protobuf/logger'
3
+
4
+ module Protobuf
5
+ module Rpc
6
+ class Stat
7
+ attr_accessor :mode, :start_time, :end_time, :request_size, :dispatcher
8
+ attr_accessor :response_size, :client, :server, :service, :method_name
9
+
10
+ MODES = [:SERVER, :CLIENT].freeze
11
+
12
+ def initialize(mode = :SERVER)
13
+ @mode = mode
14
+ start
15
+ end
16
+
17
+ def client=(peer)
18
+ @client = {:port => peer[0], :ip => peer[1]}
19
+ end
20
+
21
+ def client
22
+ @client ? "#{@client[:ip]}:#{@client[:port]}" : nil
23
+ end
24
+
25
+ def method_name
26
+ @method_name ||= @dispatcher.try(:service).try(:rpc)
27
+ end
28
+
29
+ def server=(peer)
30
+ @server = {:port => peer[0], :ip => peer[1]}
31
+ end
32
+
33
+ def server
34
+ @server ? "#{@server[:ip]}:#{@server[:port]}" : nil
35
+ end
36
+
37
+ def service
38
+ @service ||= @dispatcher.try(:service).class.name
39
+ end
40
+
41
+ def sizes
42
+ "#{@request_size || 0}B/#{@response_size || 0}B"
43
+ end
44
+
45
+ def start
46
+ @start_time ||= Time.now
47
+ end
48
+
49
+ def stop
50
+ start if !@start_time
51
+ @end_time ||= Time.now
52
+ end
53
+
54
+ def rpc
55
+ service && method_name ? "#{service}##{method_name}" : nil
56
+ end
57
+
58
+ def elapsed_time
59
+ (start_time && end_time ? "#{(end_time - start_time).round(4)}s" : nil)
60
+ end
61
+
62
+ def server?
63
+ @mode == :SERVER
64
+ end
65
+
66
+ def client?
67
+ @mode == :CLIENT
68
+ end
69
+
70
+ def to_s
71
+ [
72
+ server? ? "[SRV]" : "[CLT]",
73
+ rpc,
74
+ elapsed_time,
75
+ sizes,
76
+ server? ? server : client
77
+ ].compact.join(' - ')
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,22 @@
1
+ ##
2
+ ## Socket Mode
3
+ ##
4
+ #
5
+ # Require this file if you wish to run your server and/or client RPC
6
+ # with the raw socket handlers. This is the default run mode for bin/rpc_server.
7
+ #
8
+ # To run with rpc_server either omit any mode switches, or explicitly pass `socket`:
9
+ #
10
+ # rpc_server myapp.rb
11
+ # rpc_server --socket myapp.rb
12
+ #
13
+ # To run for client-side only override the require in your Gemfile:
14
+ #
15
+ # gem 'protobuf', :require => 'protobuf/socket'
16
+ #
17
+ require 'protobuf'
18
+ ::Protobuf.connector_type = :socket
19
+
20
+ require 'protobuf/rpc/servers/socket/server'
21
+ require 'protobuf/rpc/connectors/socket'
22
+
@@ -0,0 +1,4 @@
1
+ module Protobuf
2
+ VERSION = '2.2.5'
3
+ PROTOC_VERSION = '2.4.1'
4
+ end
@@ -0,0 +1,10 @@
1
+ module Protobuf
2
+ module WireType
3
+ VARINT = 0
4
+ FIXED64 = 1
5
+ LENGTH_DELIMITED = 2
6
+ START_GROUP = 3
7
+ END_GROUP = 4
8
+ FIXED32 = 5
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ ##
2
+ ## ZMQ Mode
3
+ ##
4
+ #
5
+ # Require this file if you wish to run your server and/or client RPC
6
+ # with the ZeroMQ handlers.
7
+ #
8
+ # To run with rpc_server specify the switch `zmq`:
9
+ #
10
+ # rpc_server --zmq myapp.rb
11
+ #
12
+ # To run for client-side only override the require in your Gemfile:
13
+ #
14
+ # gem 'protobuf', :require => 'protobuf/zmq'
15
+ #
16
+ require 'protobuf'
17
+ Protobuf.connector_type = :zmq
18
+
19
+ require 'ffi-rzmq'
20
+ require 'protobuf/rpc/servers/zmq/server'
21
+ require 'protobuf/rpc/connectors/zmq'
data/lib/protobuf.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'logger'
2
+ require 'socket'
3
+ require 'pp'
4
+ require 'stringio'
5
+ require 'active_support/all'
6
+
7
+ module Protobuf
8
+
9
+ # See Protobuf#connector_type documentation.
10
+ CONNECTORS = [ :socket, :zmq, :evented ].freeze
11
+
12
+ # Default is Socket as it has no external dependencies.
13
+ DEFAULT_CONNECTOR = :socket
14
+
15
+ module_function
16
+
17
+ # Connector Type
18
+ #
19
+ # Default: socket
20
+ #
21
+ # Symbol value which denotes the type of connector to use
22
+ # during client requests to an RPC server.
23
+ def self.connector_type
24
+ @_connector_type ||= DEFAULT_CONNECTOR
25
+ end
26
+
27
+ def self.connector_type=(type)
28
+ raise ArgumentError, 'Invalid connector type given' unless CONNECTORS.include?(type)
29
+ @_connector_type = type
30
+ end
31
+
32
+ # GC Pause during server requests
33
+ #
34
+ # Default: false
35
+ #
36
+ # Boolean value to tell the server to disable
37
+ # the Garbage Collector when handling an rpc request.
38
+ # Once the request is completed, the GC is enabled again.
39
+ # This optomization provides a huge boost in speed to rpc requests.
40
+ def self.gc_pause_server_request?
41
+ return @_gc_pause_server_request unless @_gc_pause_server_request.nil?
42
+ gc_pause_server_request = false
43
+ end
44
+
45
+ def self.gc_pause_server_request=(value)
46
+ @_gc_pause_server_request = !!value
47
+ end
48
+
49
+ # Print Deprecation Warnings
50
+ #
51
+ # Default: true
52
+ #
53
+ # Simple boolean to define whether we want field deprecation warnings to
54
+ # be printed to stderr or not. The rpc_server has an option to set this value
55
+ # explicitly, or you can turn this option off by setting
56
+ # ENV['PB_IGNORE_DEPRECATIONS'] to a non-empty value.
57
+ #
58
+ # The rpc_server option will override the ENV setting.
59
+ def self.print_deprecation_warnings?
60
+ return @_print_deprecation_warnings unless @_print_deprecation_warnings.nil?
61
+ print_deprecation_warnings = ENV.key?('PB_IGNORE_DEPRECATIONS') ? false : true
62
+ end
63
+
64
+ def self.print_deprecation_warnings=(value)
65
+ @_print_deprecation_warnings = !!value
66
+ end
67
+
68
+ end
69
+
70
+ require 'protobuf/rpc/client'
71
+ require 'protobuf/rpc/service'
72
+
73
+ env_connector_type = ENV.fetch('PB_CLIENT_TYPE') {
74
+ ::Protobuf::DEFAULT_CONNECTOR
75
+ }.to_s.downcase.strip.to_sym
76
+
77
+ if ::Protobuf::CONNECTORS.include?(env_connector_type)
78
+ require "protobuf/#{env_connector_type}"
79
+ else
80
+ $stderr.puts <<-WARN
81
+ [WARNING] Require attempted on an invalid connector type '#{env_connector_type}'.
82
+ Falling back to default '#{::Protobuf::DEFAULT_CONNECTOR}' connector.
83
+ WARN
84
+
85
+ require "protobuf/#{::Protobuf::DEFAULT_CONNECTOR}"
86
+ end
data/proto/rpc.pb.rb ADDED
@@ -0,0 +1,48 @@
1
+ ##
2
+ # This file is auto-generated. DO NOT EDIT!
3
+ #
4
+ require 'protobuf/message'
5
+
6
+ module Protobuf
7
+ module Socketrpc
8
+ ##
9
+ # Enum Classes
10
+ #
11
+ class ErrorReason < ::Protobuf::Enum; end
12
+
13
+ ##
14
+ # Message Classes
15
+ #
16
+ class Request < ::Protobuf::Message; end
17
+ class Response < ::Protobuf::Message; end
18
+
19
+ ##
20
+ # Enum Values
21
+ #
22
+ ::Protobuf::Socketrpc::ErrorReason.define :BAD_REQUEST_DATA, 0
23
+ ::Protobuf::Socketrpc::ErrorReason.define :BAD_REQUEST_PROTO, 1
24
+ ::Protobuf::Socketrpc::ErrorReason.define :SERVICE_NOT_FOUND, 2
25
+ ::Protobuf::Socketrpc::ErrorReason.define :METHOD_NOT_FOUND, 3
26
+ ::Protobuf::Socketrpc::ErrorReason.define :RPC_ERROR, 4
27
+ ::Protobuf::Socketrpc::ErrorReason.define :RPC_FAILED, 5
28
+ ::Protobuf::Socketrpc::ErrorReason.define :INVALID_REQUEST_PROTO, 6
29
+ ::Protobuf::Socketrpc::ErrorReason.define :BAD_RESPONSE_PROTO, 7
30
+ ::Protobuf::Socketrpc::ErrorReason.define :UNKNOWN_HOST, 8
31
+ ::Protobuf::Socketrpc::ErrorReason.define :IO_ERROR, 9
32
+
33
+
34
+ ##
35
+ # Message Fields
36
+ #
37
+ ::Protobuf::Socketrpc::Request.required(::Protobuf::Field::StringField, :service_name, 1)
38
+ ::Protobuf::Socketrpc::Request.required(::Protobuf::Field::StringField, :method_name, 2)
39
+ ::Protobuf::Socketrpc::Request.required(::Protobuf::Field::BytesField, :request_proto, 3)
40
+
41
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Field::BytesField, :response_proto, 1)
42
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Field::StringField, :error, 2)
43
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Field::BoolField, :callback, 3, :default => false)
44
+ ::Protobuf::Socketrpc::Response.optional(::Protobuf::Socketrpc::ErrorReason, :error_reason, 4)
45
+
46
+
47
+ end
48
+ end
data/proto/rpc.proto ADDED
@@ -0,0 +1,73 @@
1
+ // Copyright (c) 2009 Shardul Deo
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+
21
+ // Author: Shardul Deo
22
+ //
23
+ // Protobufs needed for socket rpcs.
24
+
25
+ package protobuf.socketrpc;
26
+
27
+ message Request {
28
+
29
+ // RPC service full name
30
+ required string service_name = 1;
31
+
32
+ // RPC method name
33
+ required string method_name = 2;
34
+
35
+ // RPC request proto
36
+ required bytes request_proto = 3;
37
+ }
38
+
39
+ message Response {
40
+
41
+ // RPC response proto
42
+ optional bytes response_proto = 1;
43
+
44
+ // Error, if any
45
+ optional string error = 2;
46
+
47
+ // Was callback invoked
48
+ optional bool callback = 3 [default = false];
49
+
50
+ // Error Reason
51
+ optional ErrorReason error_reason = 4;
52
+ }
53
+
54
+ // Possible error reasons
55
+ // The server-side errors are returned in the response from the server.
56
+ // The client-side errors are returned by the client-side code when it doesn't
57
+ // have a response from the server.
58
+ enum ErrorReason {
59
+
60
+ // Server-side errors
61
+ BAD_REQUEST_DATA = 0; // Server received bad request data
62
+ BAD_REQUEST_PROTO = 1; // Server received bad request proto
63
+ SERVICE_NOT_FOUND = 2; // Service not found on server
64
+ METHOD_NOT_FOUND = 3; // Method not found on server
65
+ RPC_ERROR = 4; // Rpc threw exception on server
66
+ RPC_FAILED = 5; // Rpc failed on server
67
+
68
+ // Client-side errors (these are returned by the client-side code)
69
+ INVALID_REQUEST_PROTO = 6; // Rpc was called with invalid request proto
70
+ BAD_RESPONSE_PROTO = 7; // Server returned a bad response proto
71
+ UNKNOWN_HOST = 8; // Could not find supplied host
72
+ IO_ERROR = 9; // I/O error while communicating with server
73
+ }
data/protobuf.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+ $:.push File.expand_path("./lib", File.dirname(__FILE__))
3
+ require "protobuf/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'protobuf'
7
+ s.version = Protobuf::VERSION
8
+ s.date = Time.now.strftime('%Y-%m-%d')
9
+
10
+ s.authors = ['BJ Neilsen', 'Brandon Dewitt']
11
+ s.email = ["bj.neilsen+protobuf@gmail.com", "brandonsdewitt+protobuf@gmail.com"]
12
+ s.homepage = "https://github.com/localshred/protobuf"
13
+ s.summary = "Google Protocol Buffers v#{Protobuf::PROTOC_VERSION} Serialization and RPC implementation for Ruby."
14
+ s.description = s.summary
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ unless ENV['WITHOUT_PROTO_COMPILER']
22
+ if defined?(JRUBY_VERSION)
23
+ s.platform = 'java'
24
+ else
25
+ s.extensions << File.join('ext', 'ruby_generator', 'extconf.rb')
26
+ end
27
+ end
28
+
29
+ s.add_dependency 'activesupport'
30
+ s.add_dependency 'ffi'
31
+ s.add_dependency 'multi_json'
32
+ s.add_dependency 'thor'
33
+
34
+ s.add_development_dependency 'eventmachine'
35
+ s.add_development_dependency 'ffi-rzmq'
36
+ # s.add_development_dependency 'perftools.rb'
37
+ s.add_development_dependency 'pry'
38
+ s.add_development_dependency 'pry-nav'
39
+ s.add_development_dependency 'rake'
40
+ s.add_development_dependency 'rake-compiler'
41
+ s.add_development_dependency 'rspec'
42
+ s.add_development_dependency 'simplecov'
43
+ s.add_development_dependency 'yard'
44
+ end
@@ -0,0 +1,179 @@
1
+ require 'benchmark'
2
+ require 'support/all'
3
+ require 'support/test/resource_service'
4
+
5
+ begin
6
+ require 'perftools'
7
+ rescue LoadError
8
+ $stderr.puts 'perftools must be uncommented in the gemspec before you can run this benchmark task'
9
+ exit(1)
10
+ end
11
+
12
+ # Including a way to turn on debug logger for spec runs
13
+ if ENV["DEBUG"]
14
+ puts 'debugging'
15
+ debug_log = File.expand_path('../debug_bench.log', File.dirname(__FILE__) )
16
+ Protobuf::Logger.configure(:file => debug_log, :level => ::Logger::DEBUG)
17
+ end
18
+
19
+ namespace :benchmark do
20
+
21
+ def benchmark_wrapper(global_bench = nil)
22
+ if global_bench
23
+ yield global_bench
24
+ else
25
+ Benchmark.bm(10) do |bench|
26
+ yield bench
27
+ end
28
+ end
29
+ end
30
+
31
+ def em_client_em_server(number_tests, test_length, global_bench = nil)
32
+ EM.stop if EM.reactor_running?
33
+
34
+ EventMachine.fiber_run do
35
+ StubServer.new do |server|
36
+ client = ::Test::ResourceService.client
37
+
38
+ benchmark_wrapper(global_bench) do |bench|
39
+ bench.report("ES / EC") do
40
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
41
+ end
42
+ end
43
+ end
44
+
45
+ EM.stop
46
+ end
47
+ end
48
+
49
+ def em_client_sock_server(number_tests, test_length, global_bench = nil)
50
+ EM.stop if EM.reactor_running?
51
+
52
+ EventMachine.fiber_run do
53
+ StubServer.new(:server => Protobuf::Rpc::Socket::Server, :port => 9399) do |server|
54
+ client = ::Test::ResourceService.client(:port => 9399)
55
+
56
+ benchmark_wrapper(global_bench) do |bench|
57
+ bench.report("SS / EC") do
58
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
59
+ end
60
+ end
61
+ end
62
+
63
+ EM.stop
64
+ end
65
+ end
66
+
67
+ def sock_client_sock_server(number_tests, test_length, global_bench = nil)
68
+ load "protobuf/socket.rb"
69
+ EM.stop if EM.reactor_running?
70
+
71
+ StubServer.new(:server => Protobuf::Rpc::Socket::Server, :port => 9399) do |server|
72
+ client = ::Test::ResourceService.client(:port => 9399)
73
+
74
+ benchmark_wrapper(global_bench) do |bench|
75
+ bench.report("SS / SC") do
76
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ def sock_client_em_server(number_tests, test_length, global_bench = nil)
83
+ load "protobuf/socket.rb"
84
+ EM.stop if EM.reactor_running?
85
+ em_thread = Thread.new { EM.run }
86
+ Thread.pass until EM.reactor_running?
87
+
88
+ StubServer.new(:port => 9399) do |server|
89
+ client = ::Test::ResourceService.client(:port => 9399)
90
+
91
+ benchmark_wrapper(global_bench) do |bench|
92
+ bench.report("ES / SC") do
93
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
94
+ end
95
+ end
96
+ end
97
+
98
+ EM.stop
99
+ Thread.kill(em_thread) if em_thread
100
+ end
101
+
102
+ def zmq_client_zmq_server(number_tests, test_length, global_bench = nil)
103
+ load "protobuf/zmq.rb"
104
+ StubServer.new(:port => 9399, :server => Protobuf::Rpc::Zmq::Server) do |server|
105
+ client = ::Test::ResourceService.client(:port => 9399)
106
+
107
+ benchmark_wrapper(global_bench) do |bench|
108
+ bench.report("ZS / ZC") do
109
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
110
+ end
111
+ end
112
+ server.stop
113
+ end
114
+ end
115
+
116
+ desc "benchmark ZMQ client with ZMQ server"
117
+ task :zmq_client_zmq_server, [:number, :length] do |t, args|
118
+ args.with_defaults(:number => 1000, :length => 100)
119
+ zmq_client_zmq_server(args[:number], args[:length])
120
+ end
121
+
122
+ desc "benchmark ZMQ client with ZMQ server and profile"
123
+ task :zmq_profile, [:number, :length, :profile_output] do |t, args|
124
+ args.with_defaults(:number => 1000, :length => 100, :profile_output => "/tmp/zmq_profiler_#{Time.now.to_i}")
125
+ ::PerfTools::CpuProfiler.start(args[:profile_output]) do
126
+ zmq_client_zmq_server(args[:number], args[:length])
127
+ end
128
+
129
+ puts args[:profile_output]
130
+ end
131
+
132
+ desc "benchmark Protobuf Message #new"
133
+ task :profile_protobuf_new, [:number, :profile_output] do |t, args|
134
+ args.with_defaults(:number => 1000, :profile_output => "/tmp/profiler_new_#{Time.now.to_i}")
135
+ create_params = { :name => "The name that we set", :date_created => Time.now.to_i, :status => 2 }
136
+ ::PerfTools::CpuProfiler.start(args[:profile_output]) do
137
+ args[:number].to_i.times { Test::Resource.new(create_params) }
138
+ end
139
+
140
+ puts args[:profile_output]
141
+ end
142
+
143
+ desc "benchmark EventMachine client with EventMachine server"
144
+ task :em_client_em_server, [:number, :length] do |t, args|
145
+ args.with_defaults(:number => 1000, :length => 100)
146
+ em_client_em_server(args[:number], args[:length])
147
+ end
148
+
149
+ desc "benchmark EventMachine client with Socket server"
150
+ task :em_client_sock_server, [:number, :length] do |t, args|
151
+ args.with_defaults(:number => 1000, :length => 100)
152
+ em_client_sock_server(args[:number], args[:length])
153
+ end
154
+
155
+ desc "benchmark Socket client with Socket server"
156
+ task :sock_client_sock_server, [:number, :length] do |t, args|
157
+ args.with_defaults(:number => 1000, :length => 100)
158
+ sock_client_sock_server(args[:number], args[:length])
159
+ end
160
+
161
+ desc "benchmark Socket client with EventMachine server"
162
+ task :sock_client_em_server, [:number, :length] do |t, args|
163
+ args.with_defaults(:number => 1000, :length => 100)
164
+ sock_client_em_server(args[:number], args[:length])
165
+ end
166
+
167
+ desc "benchmark server performance"
168
+ task :servers, [:number, :length] do |t, args|
169
+ args.with_defaults(:number => 1000, :length => 100)
170
+
171
+ Benchmark.bm(10) do |bench|
172
+ # zmq_client_zmq_server(args[:number], args[:length], bench)
173
+ # em_client_em_server(args[:number], args[:length], bench)
174
+ # em_client_sock_server(args[:number], args[:length], bench)
175
+ sock_client_sock_server(args[:number], args[:length], bench)
176
+ # sock_client_em_server(args[:number], args[:length], bench)
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'An Embedded Service Call Hierarchy' do
4
+
5
+
6
+
7
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'spec/support/test/resource_service'
3
+
4
+ describe 'Functional EventMachine Client' do
5
+ before(:each) do
6
+ load 'protobuf/evented.rb'
7
+ end
8
+
9
+ it 'runs fine when required fields are set' do
10
+ expect {
11
+ EventMachine.fiber_run do
12
+ StubServer.new do |server|
13
+ client = ::Test::ResourceService.client(:timeout => 5)
14
+
15
+ client.find(:name => 'Test Name', :active => true) do |c|
16
+ c.on_success do |succ|
17
+ succ.name.should eq("Test Name")
18
+ succ.status.should eq(::Test::StatusType::ENABLED)
19
+ end
20
+
21
+ c.on_failure do |err|
22
+ raise err.inspect
23
+ end
24
+ end
25
+ end
26
+ EM.stop
27
+ end
28
+ }.to_not raise_error
29
+ end
30
+
31
+ it 'calls the on_failure callback when a message is malformed' do
32
+ error = nil
33
+ EventMachine.fiber_run do
34
+ StubServer.new do |server|
35
+ request = ::Test::ResourceFindRequest.new(:active => true)
36
+ client = ::Test::ResourceService.client
37
+
38
+ client.find(request) do |c|
39
+ c.on_success { raise "shouldn't pass"}
40
+ c.on_failure {|e| error = e}
41
+ end
42
+ end
43
+ EM.stop
44
+ end
45
+ error.message.should =~ /ResourceFindRequest.*fields.*improperly set/
46
+ end
47
+
48
+ it 'calls the on_failure callback when the request type is wrong' do
49
+ error = nil
50
+ EventMachine.fiber_run do
51
+ StubServer.new do |server|
52
+ request = ::Test::Resource.new(:name => 'Test Name')
53
+ client = ::Test::ResourceService.client
54
+
55
+ client.find(request) do |c|
56
+ c.on_success { raise "shouldn't pass"}
57
+ c.on_failure {|e| error = e}
58
+ end
59
+ end
60
+ EM.stop
61
+ end
62
+ error.message.should =~ /expected request.*ResourceFindRequest.*Resource instead/i
63
+ end
64
+ end