protobuf 2.2.5-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/.yardopts +5 -0
- data/Gemfile +3 -0
- data/README.md +316 -0
- data/Rakefile +29 -0
- data/UPGRADING.md +60 -0
- data/bin/rpc_server +5 -0
- data/bin/rprotoc +62 -0
- data/examples/addressbook.pb.rb +55 -0
- data/examples/addressbook.proto +24 -0
- data/examples/reading_a_message.rb +32 -0
- data/examples/writing_a_message.rb +46 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
- data/ext/ruby_generator/Makefile +10 -0
- data/ext/ruby_generator/RubyGenerator.cpp +450 -0
- data/ext/ruby_generator/RubyGenerator.h +199 -0
- data/ext/ruby_generator/extconf.rb +36 -0
- data/ext/ruby_generator/protoc-ruby +0 -0
- data/lib/protobuf/cli.rb +188 -0
- data/lib/protobuf/enum.rb +58 -0
- data/lib/protobuf/enum_value.rb +59 -0
- data/lib/protobuf/evented.rb +22 -0
- data/lib/protobuf/exceptions.rb +11 -0
- data/lib/protobuf/ext/eventmachine.rb +14 -0
- data/lib/protobuf/field/base_field.rb +240 -0
- data/lib/protobuf/field/bool_field.rb +36 -0
- data/lib/protobuf/field/bytes_field.rb +38 -0
- data/lib/protobuf/field/double_field.rb +19 -0
- data/lib/protobuf/field/enum_field.rb +50 -0
- data/lib/protobuf/field/extension_fields.rb +32 -0
- data/lib/protobuf/field/field_array.rb +65 -0
- data/lib/protobuf/field/fixed32_field.rb +19 -0
- data/lib/protobuf/field/fixed64_field.rb +22 -0
- data/lib/protobuf/field/float_field.rb +31 -0
- data/lib/protobuf/field/int32_field.rb +12 -0
- data/lib/protobuf/field/int64_field.rb +12 -0
- data/lib/protobuf/field/integer_field.rb +19 -0
- data/lib/protobuf/field/message_field.rb +53 -0
- data/lib/protobuf/field/sfixed32_field.rb +21 -0
- data/lib/protobuf/field/sfixed64_field.rb +24 -0
- data/lib/protobuf/field/signed_integer_field.rb +23 -0
- data/lib/protobuf/field/sint32_field.rb +12 -0
- data/lib/protobuf/field/sint64_field.rb +12 -0
- data/lib/protobuf/field/string_field.rb +14 -0
- data/lib/protobuf/field/uint32_field.rb +12 -0
- data/lib/protobuf/field/uint64_field.rb +12 -0
- data/lib/protobuf/field/varint_field.rb +61 -0
- data/lib/protobuf/field.rb +57 -0
- data/lib/protobuf/logger.rb +86 -0
- data/lib/protobuf/message/decoder.rb +83 -0
- data/lib/protobuf/message/encoder.rb +48 -0
- data/lib/protobuf/message/extend.rb +8 -0
- data/lib/protobuf/message/message.rb +1 -0
- data/lib/protobuf/message.rb +320 -0
- data/lib/protobuf/rpc/buffer.rb +79 -0
- data/lib/protobuf/rpc/client.rb +166 -0
- data/lib/protobuf/rpc/connector.rb +19 -0
- data/lib/protobuf/rpc/connectors/base.rb +38 -0
- data/lib/protobuf/rpc/connectors/common.rb +156 -0
- data/lib/protobuf/rpc/connectors/em_client.rb +84 -0
- data/lib/protobuf/rpc/connectors/eventmachine.rb +87 -0
- data/lib/protobuf/rpc/connectors/socket.rb +73 -0
- data/lib/protobuf/rpc/connectors/zmq.rb +69 -0
- data/lib/protobuf/rpc/error/client_error.rb +31 -0
- data/lib/protobuf/rpc/error/server_error.rb +43 -0
- data/lib/protobuf/rpc/error.rb +25 -0
- data/lib/protobuf/rpc/rpc.pb.rb +118 -0
- data/lib/protobuf/rpc/server.rb +89 -0
- data/lib/protobuf/rpc/servers/evented/server.rb +41 -0
- data/lib/protobuf/rpc/servers/evented_runner.rb +21 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +111 -0
- data/lib/protobuf/rpc/servers/socket/worker.rb +66 -0
- data/lib/protobuf/rpc/servers/socket_runner.rb +27 -0
- data/lib/protobuf/rpc/servers/zmq/broker.rb +87 -0
- data/lib/protobuf/rpc/servers/zmq/server.rb +50 -0
- data/lib/protobuf/rpc/servers/zmq/util.rb +27 -0
- data/lib/protobuf/rpc/servers/zmq/worker.rb +60 -0
- data/lib/protobuf/rpc/servers/zmq_runner.rb +25 -0
- data/lib/protobuf/rpc/service.rb +173 -0
- data/lib/protobuf/rpc/service_dispatcher.rb +130 -0
- data/lib/protobuf/rpc/service_filters.rb +267 -0
- data/lib/protobuf/rpc/stat.rb +83 -0
- data/lib/protobuf/socket.rb +22 -0
- data/lib/protobuf/version.rb +4 -0
- data/lib/protobuf/wire_type.rb +10 -0
- data/lib/protobuf/zmq.rb +21 -0
- data/lib/protobuf.rb +86 -0
- data/proto/rpc.pb.rb +48 -0
- data/proto/rpc.proto +73 -0
- data/protobuf.gemspec +44 -0
- data/spec/benchmark/tasks.rb +179 -0
- data/spec/functional/embedded_service_spec.rb +7 -0
- data/spec/functional/evented_server_spec.rb +64 -0
- data/spec/functional/socket_server_spec.rb +58 -0
- data/spec/functional/zmq_server_spec.rb +58 -0
- data/spec/lib/protobuf/cli_spec.rb +212 -0
- data/spec/lib/protobuf/enum_spec.rb +98 -0
- data/spec/lib/protobuf/enum_value_spec.rb +15 -0
- data/spec/lib/protobuf/logger_spec.rb +131 -0
- data/spec/lib/protobuf/message/encoder_spec.rb +19 -0
- data/spec/lib/protobuf/message_spec.rb +209 -0
- data/spec/lib/protobuf/rpc/client_spec.rb +158 -0
- data/spec/lib/protobuf/rpc/connector_spec.rb +32 -0
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +50 -0
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +128 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +36 -0
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +22 -0
- data/spec/lib/protobuf/rpc/servers/evented_server_spec.rb +18 -0
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +43 -0
- data/spec/lib/protobuf/rpc/servers/zmq/broker_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +41 -0
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +45 -0
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +44 -0
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +116 -0
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +451 -0
- data/spec/lib/protobuf/rpc/service_spec.rb +165 -0
- data/spec/lib/protobuf_spec.rb +62 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/support/all.rb +6 -0
- data/spec/support/server.rb +101 -0
- data/spec/support/test/enum.pb.rb +34 -0
- data/spec/support/test/enum.proto +12 -0
- data/spec/support/test/resource.pb.rb +58 -0
- data/spec/support/test/resource.proto +31 -0
- data/spec/support/test/resource_service.rb +14 -0
- data/spec/support/test_app_file.rb +2 -0
- data/spec/support/tolerance_matcher.rb +40 -0
- data/test/data/data.bin +3 -0
- data/test/data/data_source.py +14 -0
- data/test/data/types.bin +0 -0
- data/test/data/types_source.py +22 -0
- data/test/data/unk.png +0 -0
- data/test/proto/addressbook.pb.rb +66 -0
- data/test/proto/addressbook.proto +33 -0
- data/test/proto/addressbook_base.pb.rb +58 -0
- data/test/proto/addressbook_base.proto +26 -0
- data/test/proto/addressbook_ext.pb.rb +20 -0
- data/test/proto/addressbook_ext.proto +6 -0
- data/test/proto/collision.pb.rb +17 -0
- data/test/proto/collision.proto +5 -0
- data/test/proto/ext_collision.pb.rb +24 -0
- data/test/proto/ext_collision.proto +8 -0
- data/test/proto/ext_range.pb.rb +22 -0
- data/test/proto/ext_range.proto +7 -0
- data/test/proto/float_default.proto +10 -0
- data/test/proto/lowercase.pb.rb +30 -0
- data/test/proto/lowercase.proto +9 -0
- data/test/proto/merge.pb.rb +39 -0
- data/test/proto/merge.proto +15 -0
- data/test/proto/nested.pb.rb +30 -0
- data/test/proto/nested.proto +9 -0
- data/test/proto/optional_field.pb.rb +35 -0
- data/test/proto/optional_field.proto +12 -0
- data/test/proto/packed.pb.rb +22 -0
- data/test/proto/packed.proto +6 -0
- data/test/proto/rpc.proto +6 -0
- data/test/proto/types.pb.rb +84 -0
- data/test/proto/types.proto +37 -0
- data/test/test_addressbook.rb +56 -0
- data/test/test_enum_value.rb +41 -0
- data/test/test_extension.rb +36 -0
- data/test/test_lowercase.rb +11 -0
- data/test/test_message.rb +128 -0
- data/test/test_optional_field.rb +103 -0
- data/test/test_packed_field.rb +40 -0
- data/test/test_parse.rb +15 -0
- data/test/test_repeated_types.rb +132 -0
- data/test/test_serialize.rb +61 -0
- data/test/test_standard_message.rb +96 -0
- data/test/test_types.rb +226 -0
- metadata +461 -0
data/lib/protobuf/cli.rb
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
require 'thor'
|
|
2
|
+
require 'protobuf/version'
|
|
3
|
+
require 'protobuf/logger'
|
|
4
|
+
require 'protobuf/rpc/servers/evented_runner'
|
|
5
|
+
require 'protobuf/rpc/servers/socket_runner'
|
|
6
|
+
require 'protobuf/rpc/servers/zmq_runner'
|
|
7
|
+
|
|
8
|
+
module Protobuf
|
|
9
|
+
class CLI < ::Thor
|
|
10
|
+
include ::Thor::Actions
|
|
11
|
+
|
|
12
|
+
attr_accessor :runner, :mode, :start_aborted
|
|
13
|
+
|
|
14
|
+
default_task :start
|
|
15
|
+
|
|
16
|
+
desc 'start APP_FILE', 'Run the RPC server in the given mode, preloading the given APP_FILE. This is the default task.'
|
|
17
|
+
|
|
18
|
+
option :host, :type => :string, :default => '127.0.0.1', :aliases => %w(-o), :desc => 'Host to bind.'
|
|
19
|
+
option :port, :type => :numeric, :default => 9399, :aliases => %w(-p), :desc => 'Master Port to bind.'
|
|
20
|
+
|
|
21
|
+
option :backlog, :type => :numeric, :default => 100, :aliases => %w(-b), :desc => 'Backlog for listening socket when using Socket Server.'
|
|
22
|
+
option :threshold, :type => :numeric, :default => 100, :aliases => %w(-t), :desc => 'Multi-threaded Socket Server cleanup threshold.'
|
|
23
|
+
option :threads, :type => :numeric, :default => 5, :aliases => %w(-r), :desc => 'Number of worker threads to run. Only applicable in --zmq mode.'
|
|
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 :print_deprecation_warnings, :type => :boolean, :default => true, :desc => 'Cause use of deprecated fields to be printed or ignored.'
|
|
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
|
+
configure_deprecation_warnings
|
|
46
|
+
|
|
47
|
+
run_if_no_abort { require_application!(app_file) }
|
|
48
|
+
run_if_no_abort { configure_process_name(app_file) }
|
|
49
|
+
run_if_no_abort { start_server! }
|
|
50
|
+
|
|
51
|
+
rescue => e
|
|
52
|
+
say_and_exit!('ERROR: RPC Server failed to start.', e)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
desc 'version', 'Print ruby and protoc versions and exit.'
|
|
56
|
+
def version
|
|
57
|
+
say "Ruby Protobuf v#{::Protobuf::VERSION}, protoc v#{::Protobuf::PROTOC_VERSION}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
no_tasks do
|
|
61
|
+
|
|
62
|
+
# Tell protobuf how to handle the printing of deprecated field usage.
|
|
63
|
+
def configure_deprecation_warnings
|
|
64
|
+
::Protobuf.print_deprecation_warnings = options.print_deprecation_warnings?
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# If we pause during request we don't need to pause in serialization
|
|
68
|
+
def configure_gc
|
|
69
|
+
debug_say 'Configuring gc'
|
|
70
|
+
::Protobuf.gc_pause_server_request = options.gc_pause_request?
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Setup the protobuf logger.
|
|
74
|
+
def configure_logger
|
|
75
|
+
debug_say 'Configuring logger'
|
|
76
|
+
::Protobuf::Logger.configure({ :file => options.log || STDOUT,
|
|
77
|
+
:level => options.debug? ? ::Logger::DEBUG : options.level })
|
|
78
|
+
|
|
79
|
+
# Debug output the server options to the log file.
|
|
80
|
+
::Protobuf::Logger.debug { 'Debugging options:' }
|
|
81
|
+
::Protobuf::Logger.debug { options.inspect }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Re-write the $0 var to have a nice process name in ps.
|
|
85
|
+
def configure_process_name(app_file)
|
|
86
|
+
debug_say 'Configuring process name'
|
|
87
|
+
$0 = "rpc_server --#{@mode} #{options.host}:#{options.port} #{app_file}"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Configure the mode of the server and the runner class.
|
|
91
|
+
def configure_server_mode
|
|
92
|
+
debug_say 'Configuring runner mode'
|
|
93
|
+
if options.zmq? && ! options.evented? && ! options.socket?
|
|
94
|
+
@mode = :zmq
|
|
95
|
+
@runner = ::Protobuf::Rpc::ZmqRunner
|
|
96
|
+
elsif options.evented? && ! options.zmq? && ! options.socket?
|
|
97
|
+
@mode = :evented
|
|
98
|
+
@runner = ::Protobuf::Rpc::EventedRunner
|
|
99
|
+
else
|
|
100
|
+
say 'WARNING: You have provided multiple mode options. Defaulting to socket mode.', :yellow if multi_mode?
|
|
101
|
+
@mode = :socket
|
|
102
|
+
@runner = ::Protobuf::Rpc::SocketRunner
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Configure signal traps.
|
|
107
|
+
# TODO add signal handling for hot-reloading the application.
|
|
108
|
+
def configure_traps
|
|
109
|
+
debug_say 'Configuring traps'
|
|
110
|
+
[:INT, :QUIT, :TERM].each do |signal|
|
|
111
|
+
debug_say "Registering signal trap for #{signal}", :blue
|
|
112
|
+
trap(signal) do
|
|
113
|
+
::Protobuf::Logger.info { 'RPC Server shutting down...' }
|
|
114
|
+
@start_aborted = true
|
|
115
|
+
@runner.stop
|
|
116
|
+
::Protobuf::Logger.info { 'Shutdown complete' }
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Say something if we're in debug mode.
|
|
122
|
+
def debug_say(message, color = :yellow)
|
|
123
|
+
say(message, color) if options.debug?
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Internal helper to determine if the modes are multi-set which is not valid.
|
|
127
|
+
def multi_mode?
|
|
128
|
+
(options.zmq? && (options.evented? || options.socket?)) \
|
|
129
|
+
&& (options.evented? && (options.evented? || options.socket?)) \
|
|
130
|
+
&& (options.zmq? && (options.evented? || options.socket?)) \
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Require the application file given, exiting if the file doesn't exist.
|
|
134
|
+
def require_application!(app_file)
|
|
135
|
+
debug_say 'Requiring app file'
|
|
136
|
+
require app_file
|
|
137
|
+
rescue LoadError => e
|
|
138
|
+
say_and_exit!("Failed to load application file #{app_file}", e)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Loads protobuf in the given mode, exiting if somehow the mode is wrong.
|
|
142
|
+
def require_protobuf!
|
|
143
|
+
require "protobuf/#{@mode}.rb"
|
|
144
|
+
rescue LoadError => e
|
|
145
|
+
puts e.message, *(e.backtrace)
|
|
146
|
+
say_and_exit!("Failed to load protobuf runner #{@mode}", e)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def run_if_no_abort
|
|
150
|
+
yield unless @start_aborted
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def runner_options
|
|
154
|
+
{ :host => options.host,
|
|
155
|
+
:port => options.port,
|
|
156
|
+
:backlog => options.backlog,
|
|
157
|
+
:threshold => options.threshold,
|
|
158
|
+
:threads => options.threads }
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def say_and_exit!(message, exception = nil)
|
|
162
|
+
message = set_color(message, :red) if ::Protobuf::Logger.file == STDOUT
|
|
163
|
+
|
|
164
|
+
::Protobuf::Logger.error { message }
|
|
165
|
+
if exception
|
|
166
|
+
$stderr.puts "[#{exception.class.name}] #{exception.message}"
|
|
167
|
+
$stderr.puts exception.backtrace.join("\n")
|
|
168
|
+
|
|
169
|
+
::Protobuf::Logger.error { "[#{exception.class.name}] #{exception.message}" }
|
|
170
|
+
::Protobuf::Logger.debug { exception.backtrace.join("\n") }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
exit(1)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Start the runner and log the relevant options.
|
|
177
|
+
def start_server!
|
|
178
|
+
debug_say 'Invoking server start'
|
|
179
|
+
@runner.run(runner_options) do
|
|
180
|
+
Protobuf::Logger.info { "pid #{::Process.pid} -- #{@mode} RPC Server listening at #{options.host}:#{options.port}" }
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require 'protobuf/enum_value'
|
|
2
|
+
|
|
3
|
+
module Protobuf
|
|
4
|
+
class Enum
|
|
5
|
+
|
|
6
|
+
def self.define(name, value)
|
|
7
|
+
enum_value = ::Protobuf::EnumValue.new(self, name, value)
|
|
8
|
+
const_set(name, enum_value)
|
|
9
|
+
@values ||= {}
|
|
10
|
+
@names ||= []
|
|
11
|
+
@values[name] = enum_value
|
|
12
|
+
@names[value] = name
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Fetch the given enum by a variety of type-checking
|
|
16
|
+
# mechanisms. This is useful for the enum field setters
|
|
17
|
+
# as well as repeated enum field construction.
|
|
18
|
+
def self.fetch(value)
|
|
19
|
+
case value
|
|
20
|
+
when ::Protobuf::EnumValue then
|
|
21
|
+
value
|
|
22
|
+
when Numeric then
|
|
23
|
+
enum_by_value(value.to_i)
|
|
24
|
+
when String, Symbol then
|
|
25
|
+
value_by_name(value.to_sym)
|
|
26
|
+
else
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.enum_by_value(value)
|
|
32
|
+
value_by_name(name_by_value(value))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.name_by_value(value)
|
|
36
|
+
value.nil? ? nil : @names[value]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.valid_tag?(tag)
|
|
40
|
+
!! name_by_value(tag)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.value_by_name(name)
|
|
44
|
+
@values[name]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.values
|
|
48
|
+
@values
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
##
|
|
52
|
+
# Class Aliases
|
|
53
|
+
#
|
|
54
|
+
class << self
|
|
55
|
+
alias_method :get_name_by_tag, :name_by_value
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require 'protobuf/enum'
|
|
2
|
+
|
|
3
|
+
module Protobuf
|
|
4
|
+
class EnumValue
|
|
5
|
+
include Comparable
|
|
6
|
+
|
|
7
|
+
attr_reader :parent_class, :name, :value
|
|
8
|
+
|
|
9
|
+
##
|
|
10
|
+
# Constructor
|
|
11
|
+
#
|
|
12
|
+
def initialize(parent_class, name, value)
|
|
13
|
+
@parent_class = parent_class
|
|
14
|
+
@name = name
|
|
15
|
+
@value = value
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
##
|
|
19
|
+
# Public Instance Methods
|
|
20
|
+
#
|
|
21
|
+
def <=>(compared_value)
|
|
22
|
+
case compared_value
|
|
23
|
+
when ::Protobuf::EnumValue then
|
|
24
|
+
value <=> compared_value.value
|
|
25
|
+
when Numeric then
|
|
26
|
+
value <=> compared_value.to_i
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Overriding the class so ActiveRecord/Arel visitor will visit the enum as a Fixnum
|
|
31
|
+
def class
|
|
32
|
+
Fixnum
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def inspect
|
|
36
|
+
"\#<Protobuf::EnumValue #{@parent_class}::#{@name}=#{@value}>"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def to_hash_value
|
|
40
|
+
self.to_i
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def to_i
|
|
44
|
+
@value.to_i
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_s(format = :value_string)
|
|
48
|
+
case format
|
|
49
|
+
when :value_string then
|
|
50
|
+
self.to_i.to_s
|
|
51
|
+
when :name then
|
|
52
|
+
name.to_s
|
|
53
|
+
else
|
|
54
|
+
self.to_i.to_s
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
@@ -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,14 @@
|
|
|
1
|
+
require 'fiber'
|
|
2
|
+
|
|
3
|
+
# Method from em-synchrony
|
|
4
|
+
# https://github.com/igrigorik/em-synchrony
|
|
5
|
+
#
|
|
6
|
+
# A convenience method for wrapping EM.run body within
|
|
7
|
+
# a Ruby Fiber such that async operations can be transparently
|
|
8
|
+
# paused and resumed based on IO scheduling
|
|
9
|
+
module EventMachine
|
|
10
|
+
def self.fiber_run(blk=nil, tail=nil, &block)
|
|
11
|
+
context = Proc.new{ Fiber.new{ (b = blk || block) and b.call }.resume }
|
|
12
|
+
self.run(context, tail)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
require 'protobuf/wire_type'
|
|
2
|
+
require 'protobuf/field/field_array'
|
|
3
|
+
|
|
4
|
+
module Protobuf
|
|
5
|
+
module Field
|
|
6
|
+
class BaseField
|
|
7
|
+
##
|
|
8
|
+
# Attributes
|
|
9
|
+
#
|
|
10
|
+
attr_reader :message_class, :rule, :type, :name, :tag, :default, :default_value, :setter_method_name, :getter_method_name
|
|
11
|
+
|
|
12
|
+
##
|
|
13
|
+
# Class Methods
|
|
14
|
+
#
|
|
15
|
+
def self.default
|
|
16
|
+
nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
##
|
|
20
|
+
# Constructor
|
|
21
|
+
#
|
|
22
|
+
def initialize(message_class, rule, type, name, tag, options)
|
|
23
|
+
@message_class, @rule, @type, @name, @tag = \
|
|
24
|
+
message_class, rule, type, name, tag
|
|
25
|
+
|
|
26
|
+
@getter_method_name = name
|
|
27
|
+
@setter_method_name = "#{name}=".to_sym
|
|
28
|
+
@default = options.delete(:default)
|
|
29
|
+
@extension = options.delete(:extension)
|
|
30
|
+
@packed = repeated? && options.delete(:packed)
|
|
31
|
+
@deprecated = options.delete(:deprecated)
|
|
32
|
+
unless options.empty?
|
|
33
|
+
warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type)
|
|
37
|
+
raise "Can't use packed encoding for `#{@type}' type"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
@default_value = \
|
|
41
|
+
case @rule
|
|
42
|
+
when :repeated then
|
|
43
|
+
FieldArray.new(self).freeze
|
|
44
|
+
when :required then
|
|
45
|
+
nil
|
|
46
|
+
when :optional then
|
|
47
|
+
typed_default_value
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
define_accessor
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
##
|
|
54
|
+
# Public Instance Methods
|
|
55
|
+
#
|
|
56
|
+
def acceptable?(value)
|
|
57
|
+
true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def enum?
|
|
61
|
+
return @_enum unless @_enum.nil?
|
|
62
|
+
@_enum = (self.class < ::Protobuf::Field::EnumField)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def message?
|
|
66
|
+
return @_message unless @_message.nil?
|
|
67
|
+
@_message = (self.class < ::Protobuf::Field::MessageField)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def initialized?(message_instance)
|
|
71
|
+
value = message_instance.__send__(getter_method_name)
|
|
72
|
+
case @rule
|
|
73
|
+
when :required then
|
|
74
|
+
! value.nil? && (! kind_of?(MessageField) || value.initialized?)
|
|
75
|
+
when :repeated then
|
|
76
|
+
value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? }
|
|
77
|
+
when :optional then
|
|
78
|
+
value.nil? || ! kind_of?(MessageField) || value.initialized?
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Decode +bytes+ and pass to +message_instance+.
|
|
83
|
+
def set(message_instance, bytes)
|
|
84
|
+
if packed?
|
|
85
|
+
array = message_instance.__send__(getter_method_name)
|
|
86
|
+
method = \
|
|
87
|
+
case wire_type
|
|
88
|
+
when WireType::FIXED32 then :read_fixed32
|
|
89
|
+
when WireType::FIXED64 then :read_fixed64
|
|
90
|
+
when WireType::VARINT then :read_varint
|
|
91
|
+
end
|
|
92
|
+
stream = StringIO.new(bytes)
|
|
93
|
+
until stream.eof?
|
|
94
|
+
array << decode(Decoder.__send__(method, stream))
|
|
95
|
+
end
|
|
96
|
+
else
|
|
97
|
+
value = decode(bytes)
|
|
98
|
+
if repeated?
|
|
99
|
+
message_instance.__send__(getter_method_name) << value
|
|
100
|
+
else
|
|
101
|
+
message_instance.__send__(setter_method_name, value)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Decode +bytes+ and return a field value.
|
|
107
|
+
def decode(bytes)
|
|
108
|
+
raise NotImplementedError, "#{self.class.name}\#decode"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Encode +value+ and return a byte string.
|
|
112
|
+
def encode(value)
|
|
113
|
+
raise NotImplementedError, "#{self.class.name}\#encode"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def extension?
|
|
117
|
+
!!@extension
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Is this a repeated field?
|
|
121
|
+
def repeated?
|
|
122
|
+
return @_repeated unless @_repeated.nil?
|
|
123
|
+
@_repeated = (@rule == :repeated)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Is this a required field?
|
|
127
|
+
def required?
|
|
128
|
+
return @_required unless @_required.nil?
|
|
129
|
+
@_required = (@rule == :required)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Is this a optional field?
|
|
133
|
+
def optional?
|
|
134
|
+
return @_optional unless @_optional.nil?
|
|
135
|
+
@_optional = (@rule == :optional)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Is this a deprecated field?
|
|
139
|
+
def deprecated?
|
|
140
|
+
!! @deprecated
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Is this a packed repeated field?
|
|
144
|
+
def packed?
|
|
145
|
+
!! @packed
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Upper limit for this field.
|
|
149
|
+
def max
|
|
150
|
+
self.class.max
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Lower limit for this field.
|
|
154
|
+
def min
|
|
155
|
+
self.class.min
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def to_s
|
|
159
|
+
"#{@rule} #{@type} #{@name} = #{@tag} #{@default ? "[default=#{@default.inspect}]" : ''}"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def warn_if_deprecated
|
|
163
|
+
if ::Protobuf.print_deprecation_warnings? && deprecated?
|
|
164
|
+
$stderr.puts("[WARNING] #{@message_class.name}##{@name} field usage is deprecated.")
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private
|
|
169
|
+
|
|
170
|
+
##
|
|
171
|
+
# Private Instance Methods
|
|
172
|
+
#
|
|
173
|
+
def define_accessor
|
|
174
|
+
if repeated?
|
|
175
|
+
define_array_getter
|
|
176
|
+
define_array_setter
|
|
177
|
+
else
|
|
178
|
+
define_getter
|
|
179
|
+
define_setter
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def define_array_getter
|
|
184
|
+
field = self
|
|
185
|
+
@message_class.class_eval do
|
|
186
|
+
define_method(field.getter_method_name) do
|
|
187
|
+
field.warn_if_deprecated
|
|
188
|
+
@values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def define_array_setter
|
|
194
|
+
field = self
|
|
195
|
+
@message_class.class_eval do
|
|
196
|
+
define_method(field.setter_method_name) do |val|
|
|
197
|
+
field.warn_if_deprecated
|
|
198
|
+
@values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
|
|
199
|
+
@values[field.name].replace(val)
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def define_getter
|
|
205
|
+
field = self
|
|
206
|
+
@message_class.class_eval do
|
|
207
|
+
define_method(field.getter_method_name) do
|
|
208
|
+
field.warn_if_deprecated
|
|
209
|
+
@values.fetch(field.name, field.default_value)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def define_setter
|
|
215
|
+
field = self
|
|
216
|
+
@message_class.class_eval do
|
|
217
|
+
define_method(field.setter_method_name) do |val|
|
|
218
|
+
field.warn_if_deprecated
|
|
219
|
+
if val.nil?
|
|
220
|
+
@values.delete(field.name)
|
|
221
|
+
elsif field.acceptable?(val)
|
|
222
|
+
@values[field.name] = val
|
|
223
|
+
else
|
|
224
|
+
raise TypeError, "unacceptable value #{val} for type #{field.type}"
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def typed_default_value
|
|
231
|
+
if @default.nil?
|
|
232
|
+
self.class.default
|
|
233
|
+
else
|
|
234
|
+
@default
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'protobuf/field/varint_field'
|
|
2
|
+
|
|
3
|
+
module Protobuf
|
|
4
|
+
module Field
|
|
5
|
+
class BoolField < VarintField
|
|
6
|
+
def self.default
|
|
7
|
+
false
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def acceptable?(val)
|
|
11
|
+
raise TypeError unless [true, false].include?(val)
|
|
12
|
+
true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def decode(value)
|
|
16
|
+
value == 1
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def define_getter
|
|
20
|
+
super
|
|
21
|
+
|
|
22
|
+
field = self
|
|
23
|
+
@message_class.class_eval do
|
|
24
|
+
define_method("#{field.getter_method_name}?") do
|
|
25
|
+
field.warn_if_deprecated
|
|
26
|
+
@values.fetch(field.name, field.default_value)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def encode(value)
|
|
32
|
+
[value ? 1 : 0].pack('C')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module Protobuf
|
|
2
|
+
module Field
|
|
3
|
+
class BytesField < BaseField
|
|
4
|
+
def self.default
|
|
5
|
+
''
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def wire_type
|
|
9
|
+
WireType::LENGTH_DELIMITED
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def acceptable?(val)
|
|
13
|
+
if val.is_a?(::Protobuf::Message) || val.instance_of?(String)
|
|
14
|
+
return true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
raise TypeError
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def decode(bytes)
|
|
21
|
+
bytes.force_encoding(::Protobuf::Message::STRING_ENCODING) if bytes.respond_to?(:force_encoding)
|
|
22
|
+
bytes
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def encode(value)
|
|
26
|
+
if value.is_a?(::Protobuf::Message)
|
|
27
|
+
value = value.serialize_to_string
|
|
28
|
+
else
|
|
29
|
+
value = value.dup
|
|
30
|
+
value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
string_size = VarintField.encode(value.size)
|
|
34
|
+
string_size << value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'protobuf/field/float_field'
|
|
2
|
+
|
|
3
|
+
module Protobuf
|
|
4
|
+
module Field
|
|
5
|
+
class DoubleField < FloatField
|
|
6
|
+
def wire_type
|
|
7
|
+
WireType::FIXED64
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def decode(bytes)
|
|
11
|
+
bytes.unpack('E').first
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def encode(value)
|
|
15
|
+
[value].pack('E')
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|