protobuf 1.4.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +4 -0
  2. data/Rakefile +7 -7
  3. data/bin/rpc_server +2 -188
  4. data/bin/rprotoc +44 -38
  5. data/examples/addressbook.pb.rb +2 -2
  6. data/ext/Makefile +11 -0
  7. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  8. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  9. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  10. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  11. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  12. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  13. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  92. data/ext/ruby_generator/RubyGenerator.cpp +435 -0
  93. data/ext/ruby_generator/RubyGenerator.h +180 -0
  94. data/ext/ruby_generator/extconf.rb +24 -0
  95. data/lib/protobuf.rb +55 -33
  96. data/lib/protobuf/cli.rb +176 -0
  97. data/lib/protobuf/common/logger.rb +11 -11
  98. data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
  99. data/lib/protobuf/evented.rb +22 -0
  100. data/lib/protobuf/field.rb +57 -0
  101. data/lib/protobuf/field/base_field.rb +210 -0
  102. data/lib/protobuf/field/bool_field.rb +24 -0
  103. data/lib/protobuf/field/bytes_field.rb +38 -0
  104. data/lib/protobuf/field/double_field.rb +19 -0
  105. data/lib/protobuf/field/enum_field.rb +58 -0
  106. data/lib/protobuf/field/extension_fields.rb +24 -0
  107. data/lib/protobuf/field/field_array.rb +55 -0
  108. data/lib/protobuf/field/fixed32_field.rb +19 -0
  109. data/lib/protobuf/field/fixed64_field.rb +22 -0
  110. data/lib/protobuf/field/float_field.rb +29 -0
  111. data/lib/protobuf/field/int32_field.rb +10 -0
  112. data/lib/protobuf/field/int64_field.rb +10 -0
  113. data/lib/protobuf/field/integer_field.rb +19 -0
  114. data/lib/protobuf/field/message_field.rb +51 -0
  115. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  116. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  117. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  118. data/lib/protobuf/field/sint32_field.rb +10 -0
  119. data/lib/protobuf/field/sint64_field.rb +10 -0
  120. data/lib/protobuf/field/string_field.rb +14 -0
  121. data/lib/protobuf/field/uint32_field.rb +10 -0
  122. data/lib/protobuf/field/uint64_field.rb +10 -0
  123. data/lib/protobuf/field/varint_field.rb +63 -0
  124. data/lib/protobuf/message.rb +376 -0
  125. data/lib/protobuf/message/message.rb +1 -475
  126. data/lib/protobuf/rpc/buffer.rb +1 -1
  127. data/lib/protobuf/rpc/client.rb +26 -26
  128. data/lib/protobuf/rpc/connector.rb +15 -14
  129. data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
  130. data/lib/protobuf/rpc/rpc.pb.rb +39 -28
  131. data/lib/protobuf/rpc/server.rb +7 -28
  132. data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
  133. data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
  134. data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
  135. data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
  136. data/lib/protobuf/socket.rb +22 -0
  137. data/lib/protobuf/version.rb +2 -1
  138. data/lib/protobuf/zmq.rb +21 -0
  139. data/proto/rpc.pb.rb +48 -0
  140. data/protobuf.gemspec +14 -6
  141. data/spec/benchmark/tasks.rb +42 -23
  142. data/spec/functional/evented_server_spec.rb +2 -0
  143. data/spec/functional/socket_server_spec.rb +22 -26
  144. data/spec/functional/zmq_server_spec.rb +22 -26
  145. data/spec/lib/protobuf/cli_spec.rb +182 -0
  146. data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
  147. data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
  148. data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
  149. data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
  150. data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
  151. data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
  152. data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
  153. data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
  154. data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
  155. data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
  156. data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
  157. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
  158. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
  159. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
  160. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
  161. data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
  162. data/spec/proto/test.pb.rb +48 -25
  163. data/spec/spec_helper.rb +9 -15
  164. data/spec/support/all.rb +7 -0
  165. data/spec/{helper → support}/server.rb +5 -7
  166. data/spec/{helper → support}/silent_constants.rb +4 -0
  167. data/spec/support/test_app_file.rb +2 -0
  168. data/spec/{helper → support}/tolerance_matcher.rb +0 -0
  169. data/test/proto/types.pb.rb +1 -1
  170. data/test/test_addressbook.rb +1 -1
  171. data/test/test_enum_value.rb +1 -1
  172. data/test/test_standard_message.rb +1 -1
  173. metadata +243 -107
  174. data/lib/protobuf/common/util.rb +0 -59
  175. data/lib/protobuf/compiler/compiler.rb +0 -52
  176. data/lib/protobuf/compiler/nodes.rb +0 -323
  177. data/lib/protobuf/compiler/proto.y +0 -216
  178. data/lib/protobuf/compiler/proto2.ebnf +0 -79
  179. data/lib/protobuf/compiler/proto_parser.rb +0 -1425
  180. data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
  181. data/lib/protobuf/compiler/visitors.rb +0 -282
  182. data/lib/protobuf/descriptor/descriptor.proto +0 -286
  183. data/lib/protobuf/descriptor/descriptor.rb +0 -55
  184. data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
  185. data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
  186. data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
  187. data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
  188. data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
  189. data/lib/protobuf/message/field.rb +0 -684
  190. data/lib/protobuf/message/protoable.rb +0 -38
  191. data/spec/helper/all.rb +0 -7
  192. data/spec/unit/common/util_spec.rb +0 -17
  193. data/spec/unit/rpc/connector_spec.rb +0 -31
  194. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
  195. data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
  196. data/test/check_unbuild.rb +0 -30
  197. data/test/test_compiler.rb +0 -325
  198. data/test/test_descriptor.rb +0 -122
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'mkmf'
3
+
4
+ include_directory = File.expand_path(File.join(File.dirname(__FILE__), "..", "protobuf-2.4.1", "src"))
5
+
6
+ $CPPFLAGS << " -I#{include_directory}"
7
+ $CPPFLAGS << " -Wall "
8
+
9
+ have_library("pthread")
10
+ have_library("protoc")
11
+ have_library("protobuf")
12
+
13
+ create_makefile('ruby_generator')
14
+ rescue
15
+ $stdout << <<-WARNING
16
+ Cannot compile native extension.
17
+
18
+ If you are running on Jruby or Windows you should compile protocol buffer
19
+ definitions on another computer and then use those definitions locally.
20
+
21
+ Only the compiler is restricted by this. The definitions should work without issue.
22
+ WARNING
23
+ $stdout << $/
24
+ end
@@ -2,43 +2,65 @@ require 'logger'
2
2
  require 'socket'
3
3
  require 'pp'
4
4
  require 'stringio'
5
+ require 'active_support/all'
5
6
 
6
7
  module Protobuf
7
- end
8
8
 
9
- Protobuf::ClientType = ENV['PB_CLIENT_TYPE'] if ENV['PB_CLIENT_TYPE']
10
- Protobuf::ServerType = ENV['PB_SERVER_TYPE'] if ENV['PB_SERVER_TYPE']
9
+ # See Protobuf#connector_type documentation.
10
+ VALID_CONNECTOR_TYPES = [ :socket, :zmq, :evented ].freeze
11
11
 
12
- # Socket Client/Server loaded by default as it has no impact on cross-platform issues
13
- require 'protobuf/rpc/servers/socket/server'
14
- require 'protobuf/rpc/connectors/socket'
15
- require 'protobuf/rpc/client'
16
- require 'protobuf/rpc/service'
12
+ # Default is Socket as it has no external dependencies.
13
+ DEFAULT_CONNECTOR_TYPE = :socket
17
14
 
18
- # When setting up a client
19
- case
20
- when defined?(Protobuf::ClientType) && Protobuf::ClientType =~ /\Asocket\Z/i then
21
- #no-op
22
- when defined?(Protobuf::ClientType) && Protobuf::ClientType =~ /\Azmq\Z/i then
23
- require 'ffi-rzmq'
24
- require 'protobuf/rpc/connectors/zmq'
25
- else
26
- Protobuf::ClientType = "EventMachine"
27
- require 'eventmachine'
28
- require 'protobuf/ext/eventmachine'
29
- require 'protobuf/rpc/connectors/eventmachine'
30
- end
15
+ module_function
16
+
17
+ # GC Pause during server requests
18
+ #
19
+ # Boolean value to tell the server to disable
20
+ # the Garbage Collector when handling an rpc request.
21
+ # Once the request is completed, the GC is enabled again.
22
+ # This optomization provides a huge boost in speed to rpc requests.
23
+ def self.gc_pause_server_request
24
+ return @_gc_pause_server_request unless @_gc_pause_server_request.nil?
25
+ gc_pause_server_request = false
26
+ end
27
+
28
+ def self.gc_pause_server_request=(value)
29
+ @_gc_pause_server_request = !!value
30
+ end
31
+
32
+ # GC Pause during serializations (server-side only)
33
+ #
34
+ # Boolean value to tell the server to disable
35
+ # the Garbage Collector when serializing a response proto.
36
+ # Once the serialization is completed, the GC is enabled again.
37
+ # This optomization provides a large boost in speed to rpc requests.
38
+ # Note that this option is ignored if gc_pause_server_request is
39
+ # enabled since serialization is during the request cycle.
40
+ def self.gc_pause_server_serialization
41
+ return @_gc_pause_server_serialization unless @_gc_pause_server_serialization.nil?
42
+ gc_pause_server_serialization = false
43
+ end
44
+
45
+ def self.gc_pause_server_serialization=(value)
46
+ @_gc_pause_server_serialization = !!value
47
+ end
48
+
49
+ # Connector Type
50
+ #
51
+ # Symbol value which denotes the type of connector to use
52
+ # during client requests to an RPC server.
53
+ def self.connector_type
54
+ @_connector_type ||= DEFAULT_CONNECTOR_TYPE
55
+ end
56
+
57
+ def self.connector_type=(type)
58
+ raise 'Invalid connector type given' unless VALID_CONNECTOR_TYPES.include?(type)
59
+ @_connector_type = type
60
+ end
31
61
 
32
- # For running the rpc_server
33
- case
34
- when defined?(Protobuf::ServerType) && Protobuf::ServerType =~ /\Asocket\Z/i then
35
- #no-op
36
- when defined?(Protobuf::ServerType) && Protobuf::ServerType =~ /\Azmq\Z/i then
37
- require 'ffi-rzmq'
38
- require 'protobuf/rpc/servers/zmq/server'
39
- else
40
- Protobuf::ServerType = "Evented::Server"
41
- require 'eventmachine'
42
- require 'protobuf/ext/eventmachine'
43
- require 'protobuf/rpc/servers/evented/server'
44
62
  end
63
+
64
+ require 'protobuf/rpc/client'
65
+ require 'protobuf/rpc/service'
66
+ require 'protobuf/socket'
@@ -0,0 +1,176 @@
1
+ require 'thor'
2
+ require 'pry'
3
+ require 'protobuf/version'
4
+ require 'protobuf/common/logger'
5
+ require 'protobuf/rpc/servers/evented_runner'
6
+ require 'protobuf/rpc/servers/socket_runner'
7
+ require 'protobuf/rpc/servers/zmq_runner'
8
+
9
+ module Protobuf
10
+ class CLI < ::Thor
11
+ include ::Thor::Actions
12
+
13
+ attr_accessor :runner, :mode, :start_aborted
14
+
15
+ default_task :start
16
+
17
+ desc 'start APP_FILE', 'Run the RPC server in the given mode, preloading the given APP_FILE. This is the default task.'
18
+
19
+ option :host, :type => :string, :default => '127.0.0.1', :aliases => %w(-o), :desc => 'Host to bind.'
20
+ option :port, :type => :numeric, :default => 9595, :aliases => %w(-p), :desc => 'Port to bind.'
21
+
22
+ option :backlog, :type => :numeric, :default => 100, :aliases => %w(-b), :desc => 'Backlog for listening socket when using Socket Server.'
23
+ option :threshold, :type => :numeric, :default => 100, :aliases => %w(-t), :desc => 'Multi-threaded Socket Server cleanup threshold.'
24
+
25
+ option :log, :type => :string, :aliases => %w(-l), :desc => 'Log file or device. Default is STDOUT.'
26
+ option :level, :type => :numeric, :default => ::Logger::INFO, :aliases => %w(-v), :desc => 'Log level to use, 0-5 (see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/)'
27
+
28
+ option :socket, :type => :boolean, :aliases => %w(-s), :desc => 'Socket Mode for server and client connections.'
29
+ option :evented, :type => :boolean, :aliases => %w(-m), :desc => 'Evented Mode for server and client connections (uses EventMachine).'
30
+ option :zmq, :type => :boolean, :aliases => %w(-z), :desc => 'ZeroMQ Socket Mode for server and client connections.'
31
+
32
+ option :debug, :type => :boolean, :default => false, :aliases => %w(-d), :desc => 'Debug Mode. Override log level to DEBUG.'
33
+ option :gc_pause_request, :type => :boolean, :default => false, :desc => 'Enable/Disable GC pause during request.'
34
+ option :gc_pause_serialization, :type => :boolean, :default => false, :desc => 'Enable/Disable GC pause during serialization.'
35
+
36
+ def start(app_file)
37
+ debug_say 'Configuring the rpc_server process'
38
+ @start_aborted = false
39
+
40
+ configure_logger
41
+ configure_traps
42
+ configure_server_mode
43
+ require_protobuf!
44
+ configure_gc
45
+
46
+ run_if_no_abort { require_application!(app_file) }
47
+ run_if_no_abort { configure_process_name(app_file) }
48
+ run_if_no_abort { start_server! }
49
+
50
+ rescue => e
51
+ say_and_exit!('ERROR: RPC Server failed to start.', e)
52
+ end
53
+
54
+ desc 'version', 'Print ruby and protoc versions and exit.'
55
+ def version
56
+ say "Ruby Protobuf v#{::Protobuf::VERSION}, protoc v#{::Protobuf::PROTOC_VERSION}"
57
+ end
58
+
59
+ no_tasks do
60
+
61
+ # If we pause during request we don't need to pause in serialization
62
+ def configure_gc
63
+ debug_say 'Configuring gc'
64
+ ::Protobuf.gc_pause_server_request = options.gc_pause_request?
65
+ ::Protobuf.gc_pause_server_serialization = (!options.gc_pause_request? && options.gc_pause_serialization?)
66
+ end
67
+
68
+ # Setup the protobuf logger.
69
+ def configure_logger
70
+ debug_say 'Configuring logger'
71
+ ::Protobuf::Logger.configure({ :file => options.log || STDOUT,
72
+ :level => options.debug? ? ::Logger::DEBUG : options.level })
73
+
74
+ # Debug output the server options to the log file.
75
+ ::Protobuf::Logger.debug { 'Debugging options:' }
76
+ ::Protobuf::Logger.debug { options.inspect }
77
+ end
78
+
79
+ # Re-write the $0 var to have a nice process name in ps.
80
+ def configure_process_name(app_file)
81
+ debug_say 'Configuring process name'
82
+ $0 = "rpc_server --#{@mode} #{options.host}:#{options.port} #{app_file}"
83
+ end
84
+
85
+ # Configure the mode of the server and the runner class.
86
+ def configure_server_mode
87
+ debug_say 'Configuring runner mode'
88
+ if options.zmq? && ! options.evented? && ! options.socket?
89
+ @mode = :zmq
90
+ @runner = ::Protobuf::Rpc::ZmqRunner
91
+ elsif options.evented? && ! options.zmq? && ! options.socket?
92
+ @mode = :evented
93
+ @runner = ::Protobuf::Rpc::EventedRunner
94
+ else
95
+ say 'WARNING: You have provided multiple mode options. Defaulting to socket mode.', :yellow if multi_mode?
96
+ @mode = :socket
97
+ @runner = ::Protobuf::Rpc::SocketRunner
98
+ end
99
+ end
100
+
101
+ # Configure signal traps.
102
+ # TODO add signal handling for hot-reloading the application.
103
+ def configure_traps
104
+ debug_say 'Configuring traps'
105
+ trap_block = proc {
106
+ ::Protobuf::Logger.info { 'RPC Server shutting down...' }
107
+ @start_aborted = true
108
+ @runner.stop
109
+ ::Protobuf::Logger.info { 'Shutdown complete' }
110
+ }
111
+
112
+ debug_say 'Registering INT', :blue
113
+ trap(:INT, &trap_block)
114
+ debug_say 'Registering QUIT', :blue
115
+ trap(:QUIT, &trap_block)
116
+ debug_say 'Registering TERM', :blue
117
+ trap(:TERM, &trap_block)
118
+ end
119
+
120
+ # Say something if we're in debug mode.
121
+ def debug_say(message, color = :yellow)
122
+ say(message, color) if options.debug?
123
+ end
124
+
125
+ # Internal helper to determine if the modes are multi-set which is not valid.
126
+ def multi_mode?
127
+ (options.zmq? && (options.evented? || options.socket?)) \
128
+ && (options.evented? && (options.evented? || options.socket?)) \
129
+ && (options.zmq? && (options.evented? || options.socket?)) \
130
+ end
131
+
132
+ # Require the application file given, exiting if the file doesn't exist.
133
+ def require_application!(app_file)
134
+ debug_say 'Requiring app file'
135
+ require app_file
136
+ rescue LoadError => e
137
+ say_and_exit!("Failed to load application file #{app_file}", e)
138
+ end
139
+
140
+ # Loads protobuf in the given mode, exiting if somehow the mode is wrong.
141
+ def require_protobuf!
142
+ require "protobuf/#{@mode}.rb"
143
+ rescue LoadError => e
144
+ puts e.message, *(e.backtrace)
145
+ say_and_exit!("Failed to load protobuf runner #{@mode}", e)
146
+ end
147
+
148
+ def run_if_no_abort
149
+ yield unless @start_aborted
150
+ end
151
+
152
+ def say_and_exit!(message, exception = nil)
153
+ message = set_color(message, :red) if ::Protobuf::Logger.file == STDOUT
154
+
155
+ ::Protobuf::Logger.error { message }
156
+ if exception
157
+ ::Protobuf::Logger.error { "[#{exception.class.name}] #{exception.message}" }
158
+ ::Protobuf::Logger.debug { exception.backtrace.join("\n") }
159
+ end
160
+
161
+ exit(1)
162
+ end
163
+
164
+ # Start the runner and log the relevant options.
165
+ def start_server!
166
+ debug_say 'Invoking server start'
167
+ @runner.run(options.dup) do
168
+ Protobuf::Logger.info { "pid #{::Process.pid} -- #{@mode} RPC Server listening at #{options.host}:#{options.port}" }
169
+ end
170
+ end
171
+
172
+ end
173
+
174
+ end
175
+ end
176
+
@@ -2,7 +2,7 @@ require 'logger'
2
2
 
3
3
  module Protobuf
4
4
  class Logger < ::Logger
5
-
5
+
6
6
  class << self
7
7
  attr_accessor :file, :level
8
8
 
@@ -13,43 +13,43 @@ module Protobuf
13
13
  end
14
14
  end
15
15
  end
16
-
16
+
17
17
  # One-line file/level configuration
18
18
  def self.configure(options)
19
19
  self.file = options.fetch(:file, false)
20
20
  self.level = options.fetch(:level, false)
21
21
  end
22
-
22
+
23
23
  # Use to reset the instance
24
24
  def self.reset_device!
25
25
  self.file = self.level = @__instance = nil
26
26
  end
27
-
27
+
28
28
  # Singleton instance
29
29
  def self.instance
30
30
  @__instance ||= begin
31
31
  log = nil
32
-
32
+
33
33
  if @file && @level
34
34
  log = new(self.file)
35
35
  log.level = self.level
36
36
  end
37
-
37
+
38
38
  log
39
39
  end
40
40
  end
41
-
42
- #
41
+
42
+ #
43
43
  # LogMethods module for log method including, e.g.:
44
- #
44
+ #
45
45
  # class MyClass
46
46
  # include Protobuf::Logger::LogMethods
47
47
  # ...
48
48
  # end
49
- #
49
+ #
50
50
  # Produce a module to allow "include" in other classes to avoid
51
51
  # cluttering the namespace of the including class with the other methods defined above
52
- #
52
+ #
53
53
  module LogMethods
54
54
  [:debug, :info, :warn, :error, :fatal, :any, :add, :log].each do |m|
55
55
  define_method("log_#{m}") do |*params, &block|
@@ -1,12 +1,8 @@
1
1
  require 'delegate'
2
- require 'protobuf/descriptor/enum_descriptor'
3
- require 'protobuf/message/protoable'
4
2
 
5
3
  module Protobuf
6
4
  class Enum
7
5
  class << self
8
- include Protoable
9
-
10
6
  attr_reader :values
11
7
 
12
8
  def name_by_value(value)
@@ -24,12 +20,6 @@ module Protobuf
24
20
  !! name_by_value(tag)
25
21
  end
26
22
 
27
- def descriptor
28
- @descriptor ||= Descriptor::EnumDescriptor.new(self)
29
- end
30
-
31
- private
32
-
33
23
  def define(name, value)
34
24
  enum_value = EnumValue.new(self, name, value)
35
25
  const_set(name, enum_value)
@@ -0,0 +1,22 @@
1
+ ##
2
+ ## Evented Mode
3
+ ##
4
+ #
5
+ # Require this file if you wish to run your server and/or client RPC
6
+ # with the evented handlers via EventMachine.
7
+ #
8
+ # To run with rpc_server specify the switch `evented`:
9
+ #
10
+ # rpc_server --evented myapp.rb
11
+ #
12
+ # To run for client-side only override the require in your Gemfile:
13
+ #
14
+ # gem 'protobuf', :require => 'protobuf/evented'
15
+ #
16
+ require 'protobuf'
17
+ Protobuf.connector_type = :evented
18
+
19
+ require 'eventmachine'
20
+ require 'protobuf/ext/eventmachine'
21
+ require 'protobuf/rpc/servers/evented/server'
22
+ require 'protobuf/rpc/connectors/eventmachine'
@@ -0,0 +1,57 @@
1
+ require 'protobuf/field/base_field'
2
+ require 'protobuf/field/bytes_field'
3
+ require 'protobuf/field/float_field'
4
+ require 'protobuf/field/message_field'
5
+ require 'protobuf/field/varint_field'
6
+ require 'protobuf/field/string_field'
7
+ require 'protobuf/field/double_field'
8
+ require 'protobuf/field/enum_field'
9
+ require 'protobuf/field/integer_field'
10
+ require 'protobuf/field/signed_integer_field'
11
+ require 'protobuf/field/uint32_field'
12
+ require 'protobuf/field/uint64_field'
13
+ require 'protobuf/field/int32_field'
14
+ require 'protobuf/field/int64_field'
15
+ require 'protobuf/field/sint32_field'
16
+ require 'protobuf/field/sint64_field'
17
+ require 'protobuf/field/bool_field'
18
+ require 'protobuf/field/sfixed32_field'
19
+ require 'protobuf/field/sfixed64_field'
20
+ require 'protobuf/field/fixed32_field'
21
+ require 'protobuf/field/fixed64_field'
22
+ require 'protobuf/field/extension_fields'
23
+
24
+ module Protobuf
25
+ module Field
26
+ PREDEFINED_TYPES = [
27
+ ::Protobuf::Field::DoubleField,
28
+ ::Protobuf::Field::FloatField,
29
+ ::Protobuf::Field::Int32Field,
30
+ ::Protobuf::Field::Int64Field,
31
+ ::Protobuf::Field::Uint32Field,
32
+ ::Protobuf::Field::Uint64Field,
33
+ ::Protobuf::Field::Sint32Field,
34
+ ::Protobuf::Field::Sint64Field,
35
+ ::Protobuf::Field::Fixed32Field,
36
+ ::Protobuf::Field::Fixed64Field,
37
+ ::Protobuf::Field::Sfixed32Field,
38
+ ::Protobuf::Field::Sfixed64Field,
39
+ ::Protobuf::Field::StringField,
40
+ ::Protobuf::Field::BytesField,
41
+ ::Protobuf::Field::BoolField
42
+ ].freeze
43
+
44
+ def self.build(message_class, rule, type, name, tag, options={})
45
+ field_class = type_message_or_enum(type)
46
+ field_class.new(message_class, rule, type, name, tag, options)
47
+ end
48
+
49
+ def self.type_message_or_enum(defined_type)
50
+ return defined_type if ::Protobuf::Field::PREDEFINED_TYPES.include?(defined_type)
51
+ return EnumField if defined_type < Enum
52
+ return MessageField if defined_type < Message
53
+ raise "lost in the wilderness #{defined_type}"
54
+ end
55
+
56
+ end
57
+ end