protobuf 3.3.6 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +48 -0
- data/.rubocop_todo.yml +79 -0
- data/.travis.yml +12 -4
- data/Rakefile +14 -11
- data/bin/protoc-gen-ruby +0 -1
- data/bin/rpc_server +1 -0
- data/install-protobuf.sh +8 -0
- data/lib/protobuf.rb +30 -24
- data/lib/protobuf/cli.rb +35 -35
- data/lib/protobuf/code_generator.rb +11 -8
- data/lib/protobuf/decoder.rb +4 -5
- data/lib/protobuf/deprecation.rb +20 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +2 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +2 -0
- data/lib/protobuf/encoder.rb +9 -4
- data/lib/protobuf/enum.rb +38 -54
- data/lib/protobuf/field.rb +2 -2
- data/lib/protobuf/field/base_field.rb +28 -32
- data/lib/protobuf/field/bool_field.rb +4 -4
- data/lib/protobuf/field/bytes_field.rb +5 -4
- data/lib/protobuf/field/double_field.rb +0 -1
- data/lib/protobuf/field/enum_field.rb +4 -7
- data/lib/protobuf/field/field_array.rb +3 -4
- data/lib/protobuf/field/fixed32_field.rb +1 -1
- data/lib/protobuf/field/fixed64_field.rb +0 -1
- data/lib/protobuf/field/float_field.rb +0 -1
- data/lib/protobuf/field/int32_field.rb +0 -1
- data/lib/protobuf/field/int64_field.rb +0 -1
- data/lib/protobuf/field/integer_field.rb +0 -1
- data/lib/protobuf/field/message_field.rb +2 -3
- data/lib/protobuf/field/sfixed32_field.rb +0 -1
- data/lib/protobuf/field/sfixed64_field.rb +0 -1
- data/lib/protobuf/field/signed_integer_field.rb +0 -1
- data/lib/protobuf/field/sint32_field.rb +0 -1
- data/lib/protobuf/field/sint64_field.rb +0 -1
- data/lib/protobuf/field/string_field.rb +0 -1
- data/lib/protobuf/field/uint32_field.rb +0 -1
- data/lib/protobuf/field/uint64_field.rb +0 -1
- data/lib/protobuf/field/varint_field.rb +0 -1
- data/lib/protobuf/generators/base.rb +1 -2
- data/lib/protobuf/generators/enum_generator.rb +1 -2
- data/lib/protobuf/generators/extension_generator.rb +1 -2
- data/lib/protobuf/generators/field_generator.rb +4 -5
- data/lib/protobuf/generators/file_generator.rb +22 -27
- data/lib/protobuf/generators/group_generator.rb +15 -16
- data/lib/protobuf/generators/message_generator.rb +13 -14
- data/lib/protobuf/generators/printable.rb +9 -10
- data/lib/protobuf/generators/service_generator.rb +1 -2
- data/lib/protobuf/lifecycle.rb +20 -33
- data/lib/protobuf/logging.rb +4 -6
- data/lib/protobuf/message.rb +22 -16
- data/lib/protobuf/message/fields.rb +14 -17
- data/lib/protobuf/message/serialization.rb +6 -5
- data/lib/protobuf/rpc/buffer.rb +10 -12
- data/lib/protobuf/rpc/client.rb +12 -12
- data/lib/protobuf/rpc/connectors/base.rb +4 -3
- data/lib/protobuf/rpc/connectors/common.rb +15 -17
- data/lib/protobuf/rpc/connectors/socket.rb +2 -2
- data/lib/protobuf/rpc/connectors/zmq.rb +118 -108
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +2 -0
- data/lib/protobuf/rpc/env.rb +12 -12
- data/lib/protobuf/rpc/error.rb +1 -1
- data/lib/protobuf/rpc/error/client_error.rb +4 -4
- data/lib/protobuf/rpc/error/server_error.rb +6 -6
- data/lib/protobuf/rpc/middleware/exception_handler.rb +1 -1
- data/lib/protobuf/rpc/middleware/logger.rb +3 -3
- data/lib/protobuf/rpc/middleware/request_decoder.rb +5 -5
- data/lib/protobuf/rpc/middleware/response_encoder.rb +3 -3
- data/lib/protobuf/rpc/rpc.pb.rb +2 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +75 -65
- data/lib/protobuf/rpc/servers/socket/worker.rb +2 -2
- data/lib/protobuf/rpc/servers/socket_runner.rb +12 -6
- data/lib/protobuf/rpc/servers/zmq/broker.rb +10 -6
- data/lib/protobuf/rpc/servers/zmq/server.rb +20 -26
- data/lib/protobuf/rpc/servers/zmq/util.rb +7 -7
- data/lib/protobuf/rpc/servers/zmq/worker.rb +5 -7
- data/lib/protobuf/rpc/servers/zmq_runner.rb +14 -3
- data/lib/protobuf/rpc/service.rb +15 -15
- data/lib/protobuf/rpc/service_directory.rb +7 -11
- data/lib/protobuf/rpc/service_dispatcher.rb +3 -3
- data/lib/protobuf/rpc/service_filters.rb +27 -28
- data/lib/protobuf/rpc/stat.rb +4 -7
- data/lib/protobuf/socket.rb +0 -1
- data/lib/protobuf/tasks/compile.rake +2 -2
- data/lib/protobuf/version.rb +1 -1
- data/protobuf.gemspec +20 -4
- data/spec/benchmark/tasks.rb +49 -23
- data/spec/bin/protoc-gen-ruby_spec.rb +11 -6
- data/spec/encoding/all_types_spec.rb +91 -77
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/socket_server_spec.rb +9 -10
- data/spec/functional/zmq_server_spec.rb +21 -19
- data/spec/lib/protobuf/cli_spec.rb +20 -20
- data/spec/lib/protobuf/code_generator_spec.rb +6 -6
- data/spec/lib/protobuf/enum_spec.rb +57 -31
- data/spec/lib/protobuf/field/float_field_spec.rb +2 -2
- data/spec/lib/protobuf/field/int32_field_spec.rb +1 -1
- data/spec/lib/protobuf/field/string_field_spec.rb +7 -8
- data/spec/lib/protobuf/field_spec.rb +3 -6
- data/spec/lib/protobuf/generators/base_spec.rb +6 -6
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +22 -17
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +8 -9
- data/spec/lib/protobuf/generators/field_generator_spec.rb +14 -11
- data/spec/lib/protobuf/generators/file_generator_spec.rb +7 -4
- data/spec/lib/protobuf/generators/service_generator_spec.rb +14 -11
- data/spec/lib/protobuf/lifecycle_spec.rb +9 -4
- data/spec/lib/protobuf/message_spec.rb +63 -35
- data/spec/lib/protobuf/optionable_spec.rb +3 -3
- data/spec/lib/protobuf/rpc/client_spec.rb +2 -2
- data/spec/lib/protobuf/rpc/connector_spec.rb +1 -1
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +6 -6
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +26 -18
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +4 -4
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +11 -9
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +2 -2
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +7 -7
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +14 -14
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +5 -5
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +1 -1
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +9 -7
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +9 -9
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +2 -2
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +24 -23
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +4 -4
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +84 -51
- data/spec/lib/protobuf/rpc/service_spec.rb +15 -14
- data/spec/lib/protobuf/rpc/stat_spec.rb +1 -1
- data/spec/lib/protobuf_spec.rb +9 -9
- data/spec/spec_helper.rb +7 -19
- data/spec/support/server.rb +29 -59
- data/spec/support/test/defaults.pb.rb +2 -0
- data/spec/support/test/enum.pb.rb +2 -0
- data/spec/support/test/extended.pb.rb +2 -0
- data/spec/support/test/google_unittest_import.pb.rb +2 -0
- data/spec/support/test/google_unittest_import_public.pb.rb +2 -0
- data/spec/support/test/multi_field_extensions.pb.rb +2 -0
- data/spec/support/test/resource.pb.rb +2 -0
- data/spec/support/test/resource_service.rb +17 -20
- metadata +153 -112
- data/lib/protobuf/deprecator.rb +0 -42
data/lib/protobuf/rpc/env.rb
CHANGED
@@ -16,19 +16,19 @@ module Protobuf
|
|
16
16
|
#
|
17
17
|
def self.hash_accessor(*names) #:nodoc:
|
18
18
|
names.each do |name|
|
19
|
-
|
20
|
-
def #{name}
|
21
|
-
self['#{name}']
|
22
|
-
end
|
19
|
+
name_str = name.to_s.freeze
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
define_method name do
|
22
|
+
self[name_str]
|
23
|
+
end
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
define_method "#{name}=" do |value|
|
26
|
+
self[name_str] = value
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method "#{name}?" do
|
30
|
+
!self[name_str].nil?
|
31
|
+
end
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -48,7 +48,7 @@ module Protobuf
|
|
48
48
|
:service_name,
|
49
49
|
:worker_id
|
50
50
|
|
51
|
-
def initialize(options={})
|
51
|
+
def initialize(options = {})
|
52
52
|
merge!(options)
|
53
53
|
|
54
54
|
self['worker_id'] = ::Thread.current.object_id.to_s(16)
|
data/lib/protobuf/rpc/error.rb
CHANGED
@@ -8,7 +8,7 @@ module Protobuf
|
|
8
8
|
class PbError < StandardError
|
9
9
|
attr_reader :error_type
|
10
10
|
|
11
|
-
def initialize(message='An unknown RpcError occurred', error_type='RPC_ERROR')
|
11
|
+
def initialize(message = 'An unknown RpcError occurred', error_type = 'RPC_ERROR')
|
12
12
|
@error_type = error_type.is_a?(String) ? Socketrpc::ErrorReason.const_get(error_type) : error_type
|
13
13
|
super message
|
14
14
|
end
|
@@ -4,25 +4,25 @@ module Protobuf
|
|
4
4
|
module Rpc
|
5
5
|
|
6
6
|
class InvalidRequestProto < PbError
|
7
|
-
def initialize(message='Invalid request type given')
|
7
|
+
def initialize(message = 'Invalid request type given')
|
8
8
|
super message, 'INVALID_REQUEST_PROTO'
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
class BadResponseProto < PbError
|
13
|
-
def initialize(message='Bad response type from server')
|
13
|
+
def initialize(message = 'Bad response type from server')
|
14
14
|
super message, 'BAD_RESPONSE_PROTO'
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
class UnkownHost < PbError
|
19
|
-
def initialize(message='Unknown host or port')
|
19
|
+
def initialize(message = 'Unknown host or port')
|
20
20
|
super message, 'UNKNOWN_HOST'
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
class IOError < PbError
|
25
|
-
def initialize(message='IO Error occurred')
|
25
|
+
def initialize(message = 'IO Error occurred')
|
26
26
|
super message, 'IO_ERROR'
|
27
27
|
end
|
28
28
|
end
|
@@ -4,37 +4,37 @@ module Protobuf
|
|
4
4
|
module Rpc
|
5
5
|
|
6
6
|
class BadRequestData < PbError
|
7
|
-
def initialize
|
7
|
+
def initialize(message = 'Unable to parse request')
|
8
8
|
super message, 'BAD_REQUEST_DATA'
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
class BadRequestProto < PbError
|
13
|
-
def initialize
|
13
|
+
def initialize(message = 'Request is of wrong type')
|
14
14
|
super message, 'BAD_REQUEST_PROTO'
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
class ServiceNotFound < PbError
|
19
|
-
def initialize
|
19
|
+
def initialize(message = 'Service class not found')
|
20
20
|
super message, 'SERVICE_NOT_FOUND'
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
class MethodNotFound < PbError
|
25
|
-
def initialize
|
25
|
+
def initialize(message = 'Service method not found')
|
26
26
|
super message, 'METHOD_NOT_FOUND'
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
class RpcError < PbError
|
31
|
-
def initialize
|
31
|
+
def initialize(message = 'RPC exception occurred')
|
32
32
|
super message, 'RPC_ERROR'
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
class RpcFailed < PbError
|
37
|
-
def initialize
|
37
|
+
def initialize(message = 'RPC failed')
|
38
38
|
super message, 'RPC_FAILED'
|
39
39
|
end
|
40
40
|
end
|
@@ -19,7 +19,7 @@ module Protobuf
|
|
19
19
|
env
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
private
|
23
23
|
|
24
24
|
def instrumenter
|
25
25
|
@instrumenter ||= Instrumenter.new
|
@@ -63,11 +63,11 @@ module Protobuf
|
|
63
63
|
rpc,
|
64
64
|
sizes,
|
65
65
|
elapsed_time,
|
66
|
-
@end_time.try(:iso8601)
|
66
|
+
@end_time.try(:iso8601),
|
67
67
|
].compact.join(' - ')
|
68
68
|
end
|
69
69
|
|
70
|
-
|
70
|
+
private
|
71
71
|
|
72
72
|
def elapsed_time
|
73
73
|
(@start_time && @end_time ? "#{(@end_time - @start_time).round(4)}s" : nil)
|
@@ -30,14 +30,14 @@ module Protobuf
|
|
30
30
|
env.log_signature || super
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
private
|
34
34
|
|
35
35
|
def method_name
|
36
36
|
@method_name ||= begin
|
37
37
|
method_name = request_wrapper.method_name.underscore.to_sym
|
38
38
|
|
39
39
|
unless service.rpc_method?(method_name)
|
40
|
-
|
40
|
+
fail MethodNotFound, "#{service.name}##{method_name} is not a defined RPC method."
|
41
41
|
end
|
42
42
|
|
43
43
|
method_name
|
@@ -50,7 +50,7 @@ module Protobuf
|
|
50
50
|
rpc_method.request_type.decode(data)
|
51
51
|
end
|
52
52
|
rescue => exception
|
53
|
-
raise BadRequestData
|
53
|
+
raise BadRequestData, "Unable to decode request: #{exception.message}"
|
54
54
|
end
|
55
55
|
|
56
56
|
# Decode the incoming request object into our expected request object
|
@@ -61,7 +61,7 @@ module Protobuf
|
|
61
61
|
Socketrpc::Request.decode(env.encoded_request)
|
62
62
|
end
|
63
63
|
rescue => exception
|
64
|
-
raise BadRequestData
|
64
|
+
raise BadRequestData, "Unable to decode request: #{exception.message}"
|
65
65
|
end
|
66
66
|
|
67
67
|
def rpc_method
|
@@ -71,7 +71,7 @@ module Protobuf
|
|
71
71
|
def service
|
72
72
|
@service ||= service_name.constantize
|
73
73
|
rescue NameError
|
74
|
-
raise ServiceNotFound
|
74
|
+
raise ServiceNotFound, "Service class #{service_name} is not defined."
|
75
75
|
end
|
76
76
|
|
77
77
|
def service_name
|
@@ -22,7 +22,7 @@ module Protobuf
|
|
22
22
|
env.log_signature || super
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
private
|
26
26
|
|
27
27
|
# Encode the response wrapper to return to the client
|
28
28
|
#
|
@@ -35,7 +35,7 @@ module Protobuf
|
|
35
35
|
|
36
36
|
# Rescue encoding exceptions, re-wrap them as generic protobuf errors,
|
37
37
|
# and re-raise them
|
38
|
-
raise PbError
|
38
|
+
raise PbError, exception.message
|
39
39
|
end
|
40
40
|
|
41
41
|
# Prod the object to see if we can produce a proto object as a response
|
@@ -66,7 +66,7 @@ module Protobuf
|
|
66
66
|
expected = env.response_type
|
67
67
|
|
68
68
|
if expected != actual
|
69
|
-
|
69
|
+
fail BadResponseProto, "Expected response to be of type #{expected.name} but was #{actual.name}"
|
70
70
|
end
|
71
71
|
|
72
72
|
candidate
|
data/lib/protobuf/rpc/rpc.pb.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require '
|
1
|
+
require 'set'
|
2
|
+
|
2
3
|
require 'protobuf/rpc/servers/socket/worker'
|
3
4
|
|
4
5
|
module Protobuf
|
@@ -9,33 +10,52 @@ module Protobuf
|
|
9
10
|
|
10
11
|
AUTO_COLLECT_TIMEOUT = 5 # seconds
|
11
12
|
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_accessor :threshold, :host, :port, :backlog
|
16
|
+
attr_writer :running
|
17
|
+
|
18
|
+
public
|
19
|
+
|
20
|
+
attr_reader :running
|
21
|
+
alias_method :running?, :running
|
22
|
+
|
12
23
|
def initialize(options)
|
13
|
-
|
24
|
+
self.running = false
|
25
|
+
self.host = options.fetch(:host)
|
26
|
+
self.port = options.fetch(:port)
|
27
|
+
self.backlog = options.fetch(:backlog, 100)
|
28
|
+
self.threshold = options.fetch(:threshold, 100)
|
29
|
+
end
|
30
|
+
|
31
|
+
def threads
|
32
|
+
@threads ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def working
|
36
|
+
@working ||= Set.new
|
14
37
|
end
|
15
38
|
|
16
39
|
def cleanup?
|
17
|
-
# every
|
18
|
-
|
40
|
+
# every `threshold` connections run a cleanup routine after closing the response
|
41
|
+
threads.size > 0 && threads.size % threshold == 0
|
19
42
|
end
|
20
43
|
|
21
44
|
def cleanup_threads
|
22
|
-
logger.debug { sign_message("Thread cleanup - #{
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
t[:thread].join
|
29
|
-
@working.delete(t[:socket])
|
30
|
-
false
|
45
|
+
logger.debug { sign_message("Thread cleanup - #{threads.size} - start") }
|
46
|
+
|
47
|
+
threads.delete_if do |hash|
|
48
|
+
unless (thread = hash.fetch(:thread)).alive?
|
49
|
+
thread.join
|
50
|
+
working.delete(hash.fetch(:socket))
|
31
51
|
end
|
32
52
|
end
|
33
53
|
|
34
|
-
logger.debug { sign_message("Thread cleanup - #{
|
54
|
+
logger.debug { sign_message("Thread cleanup - #{threads.size} - complete") }
|
35
55
|
end
|
36
56
|
|
37
57
|
def log_signature
|
38
|
-
@_log_signature ||= "server-#{self.class.name}"
|
58
|
+
@_log_signature ||= "[server-#{self.class.name}]"
|
39
59
|
end
|
40
60
|
|
41
61
|
def new_worker(socket)
|
@@ -48,64 +68,54 @@ module Protobuf
|
|
48
68
|
|
49
69
|
def run
|
50
70
|
logger.debug { sign_message("Run") }
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
cleanup_threads if cleanup?
|
71
|
+
|
72
|
+
server = ::TCPServer.new(host, port)
|
73
|
+
fail "The server was unable to start properly." if server.closed?
|
74
|
+
|
75
|
+
begin
|
76
|
+
server.listen(backlog)
|
77
|
+
listen_fds = [server]
|
78
|
+
self.running = true
|
79
|
+
|
80
|
+
while running?
|
81
|
+
logger.debug { sign_message("Waiting for connections") }
|
82
|
+
ready_cnxns = begin
|
83
|
+
IO.select(listen_fds, [], [], AUTO_COLLECT_TIMEOUT)
|
84
|
+
rescue IOError
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
if ready_cnxns
|
89
|
+
ready_cnxns.first.each do |client|
|
90
|
+
case
|
91
|
+
when !running?
|
92
|
+
# no-op
|
93
|
+
when client == server
|
94
|
+
logger.debug { sign_message("Accepted new connection") }
|
95
|
+
client, _sockaddr = server.accept
|
96
|
+
listen_fds << client
|
97
|
+
else
|
98
|
+
unless working.include?(client)
|
99
|
+
working << listen_fds.delete(client)
|
100
|
+
logger.debug { sign_message("Working") }
|
101
|
+
threads << { :thread => new_worker(client), :socket => client }
|
102
|
+
|
103
|
+
cleanup_threads if cleanup?
|
104
|
+
end
|
86
105
|
end
|
87
106
|
end
|
107
|
+
else
|
108
|
+
# Run a cleanup if select times out while waiting
|
109
|
+
cleanup_threads if threads.size > 1
|
88
110
|
end
|
89
|
-
else
|
90
|
-
# Run a cleanup if select times out while waiting
|
91
|
-
cleanup_threads if @threads.size > 1
|
92
111
|
end
|
112
|
+
ensure
|
113
|
+
server.close
|
93
114
|
end
|
94
|
-
|
95
|
-
rescue Errno::EADDRINUSE
|
96
|
-
raise
|
97
|
-
rescue
|
98
|
-
# Closing the server causes the loop to raise an exception here
|
99
|
-
raise #if running?
|
100
|
-
end
|
101
|
-
|
102
|
-
def running?
|
103
|
-
!!@running
|
104
115
|
end
|
105
116
|
|
106
117
|
def stop
|
107
|
-
|
108
|
-
@server.try(:close)
|
118
|
+
self.running = false
|
109
119
|
end
|
110
120
|
end
|
111
121
|
end
|
@@ -33,7 +33,7 @@ module Protobuf
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def send_data(data)
|
36
|
-
|
36
|
+
fail 'Socket closed unexpectedly' unless socket_writable?
|
37
37
|
response_buffer = Protobuf::Rpc::Buffer.new(:write)
|
38
38
|
response_buffer.set_data(data)
|
39
39
|
|
@@ -44,7 +44,7 @@ module Protobuf
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def log_signature
|
47
|
-
@_log_signature ||= "server-#{self.class}-#{object_id}"
|
47
|
+
@_log_signature ||= "[server-#{self.class}-#{object_id}]"
|
48
48
|
end
|
49
49
|
|
50
50
|
def socket_writable?
|
@@ -2,8 +2,14 @@ module Protobuf
|
|
2
2
|
module Rpc
|
3
3
|
class SocketRunner
|
4
4
|
|
5
|
+
private
|
6
|
+
|
7
|
+
attr_accessor :server
|
8
|
+
|
9
|
+
public
|
10
|
+
|
5
11
|
def initialize(options)
|
6
|
-
|
12
|
+
options = case
|
7
13
|
when options.is_a?(OpenStruct) then
|
8
14
|
options.marshal_dump
|
9
15
|
when options.is_a?(Hash) then
|
@@ -11,23 +17,23 @@ module Protobuf
|
|
11
17
|
when options.respond_to?(:to_hash) then
|
12
18
|
options.to_hash
|
13
19
|
else
|
14
|
-
|
20
|
+
fail "Cannot parser Socket Server - server options"
|
15
21
|
end
|
16
22
|
|
17
|
-
|
23
|
+
self.server = ::Protobuf::Rpc::Socket::Server.new(options)
|
18
24
|
end
|
19
25
|
|
20
26
|
def run
|
21
27
|
yield if block_given?
|
22
|
-
|
28
|
+
server.run
|
23
29
|
end
|
24
30
|
|
25
31
|
def running?
|
26
|
-
|
32
|
+
server.running?
|
27
33
|
end
|
28
34
|
|
29
35
|
def stop
|
30
|
-
|
36
|
+
server.stop
|
31
37
|
end
|
32
38
|
end
|
33
39
|
end
|