thrift-client 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+
9
+ module Foo
10
+ class Foo
11
+ include ::Thrift::Struct, ::Thrift::Struct_Union
12
+ FIRST = 1
13
+ SECOND = 2
14
+ THIRD = 3
15
+ FOURTH = 4
16
+
17
+ FIELDS = {
18
+ FIRST => {:type => ::Thrift::Types::I32, :name => 'first'},
19
+ SECOND => {:type => ::Thrift::Types::I64, :name => 'second'},
20
+ THIRD => {:type => ::Thrift::Types::DOUBLE, :name => 'third'},
21
+ FOURTH => {:type => ::Thrift::Types::STRING, :name => 'fourth'}
22
+ }
23
+
24
+ def struct_fields; FIELDS; end
25
+
26
+ def validate
27
+ end
28
+
29
+ ::Thrift::Struct.generate_accessors self
30
+ end
31
+
32
+ class Bar
33
+ include ::Thrift::Struct, ::Thrift::Struct_Union
34
+ FOURTH = 1
35
+ THIRD = 2
36
+ SECOND = 3
37
+ FIRST = 4
38
+
39
+ FIELDS = {
40
+ FOURTH => {:type => ::Thrift::Types::STRING, :name => 'fourth'},
41
+ THIRD => {:type => ::Thrift::Types::DOUBLE, :name => 'third'},
42
+ SECOND => {:type => ::Thrift::Types::I64, :name => 'second'},
43
+ FIRST => {:type => ::Thrift::Types::I32, :name => 'first'}
44
+ }
45
+
46
+ def struct_fields; FIELDS; end
47
+
48
+ def validate
49
+ end
50
+
51
+ ::Thrift::Struct.generate_accessors self
52
+ end
53
+
54
+ end
@@ -0,0 +1,53 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "test/unit"
5
+ require "thrift_client"
6
+ require "foobar_service"
7
+ require 'foobar_types'
8
+ require 'thrift'
9
+ # require 'yaml'
10
+
11
+ class ClientTest < Test::Unit::TestCase
12
+ include Test::Unit::Assertions
13
+ def setup
14
+ super
15
+ transport = Thrift::Socket.new('127.0.0.1', 9999)
16
+ transport = Thrift::FramedTransport.new(transport)
17
+ protocol = Thrift::BinaryProtocol.new(transport)
18
+ protocol = Thrift::MultiplexedProtocol.new(protocol,"foo")
19
+ @client = Foo::FoobarService::Client.new(protocol)
20
+ transport.open
21
+ puts "setup"
22
+ end
23
+
24
+ def test_multiprotocol
25
+ puts "test start"
26
+
27
+ @client.ping
28
+
29
+
30
+ assert_nothing_raised do @client.ping
31
+ end
32
+
33
+ bar = @client.getBar
34
+ assert_not_nil(bar)
35
+
36
+ foo = Foo::Foo.new
37
+ foo.first = 1
38
+ foo.second = 2
39
+ foo.third = 3.0
40
+ foo.fourth = "4"
41
+ assert(@client.putFoo(foo))
42
+
43
+ assert_nothing_raised do @client.ping
44
+ end
45
+
46
+ puts "test stop"
47
+ end
48
+
49
+ def teardown
50
+ super
51
+ puts "teardown"
52
+ end
53
+ end
@@ -0,0 +1,102 @@
1
+ $:.unshift File.expand_path("../../test", __FILE__)
2
+ $:.unshift File.expand_path("../../lib", __FILE__)
3
+ require 'thrift'
4
+ require 'foobar_service'
5
+ require 'foobar_types'
6
+ require 'thrift_client'
7
+
8
+ module Server
9
+ include Thrift
10
+
11
+ class FooHandler
12
+
13
+ def ping
14
+ puts "ping"
15
+ end
16
+
17
+ def getBar
18
+ bar = Foo::Bar.new
19
+ bar.fourth = "4"
20
+ bar.third = 3.0
21
+ bar.second = 2
22
+ bar.first = 1
23
+ return bar
24
+ end
25
+
26
+ def putFoo(foo)
27
+ puts foo.inspect
28
+ return true
29
+ end
30
+
31
+ def getTimeout
32
+ sleep(2)
33
+ return false
34
+ end
35
+
36
+ def getError()
37
+ puts "error"
38
+ raise ApplicationException.new("Exception")
39
+ return true
40
+ end
41
+
42
+ end
43
+
44
+ def self.start_server(host, port, serverClass)
45
+ handler = FooHandler.new
46
+ foo_processor = Foo::FoobarService::Processor.new(handler)
47
+ puts foo_processor.class.name.split("::")[-2].downcase.gsub("service","")
48
+ multi_processor = Thrift::MultiplexedProcessor.new
49
+ multi_processor.register(foo_processor.class.name.split("::")[-2], foo_processor)
50
+ transport = ServerSocket.new(host, port)
51
+ # transport = FramedTransport.new(transport)
52
+ transport_factory = FramedTransportFactory.new
53
+ args = [multi_processor, transport, transport_factory]
54
+ if serverClass == NonblockingServer
55
+ # logger = Logger.new(STDOUT)
56
+ # logger.level = Logger::WARN
57
+ # args << logger
58
+ end
59
+ server = serverClass.new(*args)
60
+ @server_thread = Thread.new do
61
+ server.serve
62
+ end
63
+ @server = server
64
+ end
65
+
66
+ def self.shutdown
67
+ return if @server.nil?
68
+ if @server.respond_to? :shutdown
69
+ @server.shutdown
70
+ else
71
+ @server_thread.kill
72
+ end
73
+ end
74
+ end
75
+
76
+ def resolve_const(const)
77
+ const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
78
+ end
79
+
80
+ host = '127.0.0.1'
81
+ port = ARGV[0] || 9999
82
+
83
+ #serverklass = 'Thrift::SimpleServer'
84
+ #serverklass = 'Thrift::ThreadedServer'
85
+ serverklass = 'Thrift::ThreadPoolServer'
86
+ # serverklass = 'Thrift::NonblockingServer'
87
+
88
+ Server.start_server(host, port.to_i, resolve_const(serverklass))
89
+ # let our host know that the interpreter has started
90
+ # ideally we'd wait until the server was serving, but we don't have a hook for that
91
+
92
+ puts "multiplexed server start on #{host}:#{port}"
93
+ puts ""
94
+
95
+ # STDOUT.flush
96
+
97
+ Marshal.load(STDIN) # wait until we're instructed to shut down
98
+
99
+ Server.shutdown
100
+
101
+ puts "server shutdown"
102
+
@@ -0,0 +1,86 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "test/unit"
5
+ require "thrift_client"
6
+ require "thrift"
7
+
8
+
9
+ DISCONNECT_ERRORS = [
10
+ IOError,
11
+ Thrift::ProtocolException,
12
+ Thrift::TransportException
13
+ ]
14
+ APPLICATION_ERRORS =[
15
+ Thrift::ApplicationException
16
+ ]
17
+ DEFAULTS = {
18
+ :protocol => Thrift::CompactProtocol,
19
+ :transport => Thrift::Socket,
20
+ :transport_wrapper => Thrift::FramedTransport,
21
+ :disconnect_exception_classes => nil,
22
+ :application_exception_classes => nil,
23
+ :size => 1,
24
+ :timeout => nil,
25
+ :client_class => nil,
26
+ :test_on_borrow => true,
27
+ :pool_timeout => 5,
28
+ :before_method => [],
29
+ :after_method => [],
30
+ :on_exception => []
31
+ }
32
+ config = {
33
+ "protocol" => "Thrift::CompactProtocol",
34
+ "transport" => "Thrift::Socket",
35
+ "transport_wrapper" => "Thrift::FramedTransport",
36
+ "disconnect_exception_classes" => "NameError,ArgumentError,StandardError,IOError",
37
+ "application_exception_classes" => "IOError,,",
38
+ "size" => nil,
39
+ "timeout" => nil,
40
+ "client_class" => nil,
41
+ "test_on_borrow" => "true",
42
+ "pool_timeout" => "5",
43
+ "before_method" => [],
44
+ "after_method" => [],
45
+ "on_exception" => []
46
+ }
47
+
48
+ user_options = {}
49
+ user_options.store(:protocol,eval(config["protocol"]))
50
+ user_options.store(:transport,eval(config["transport"]))
51
+ user_options.store(:transport_wrapper,eval(config["transport_wrapper"]))
52
+
53
+ user_define_disconnect_exceptions = config["disconnect_exception_classes"].split(",")
54
+ # puts user_define_exceptions
55
+ user_define_disconnect_exceptions.each do |exception|
56
+ DISCONNECT_ERRORS.push(eval(exception))
57
+ end
58
+ DISCONNECT_ERRORS.uniq!
59
+ DISCONNECT_ERRORS.each do |error|
60
+ # puts error.class
61
+ # puts error
62
+ end
63
+ user_options.store(:disconnect_exception_classes, DISCONNECT_ERRORS)
64
+
65
+ user_define_application_exceptions = config["application_exception_classes"].split(",")
66
+ puts user_define_application_exceptions
67
+ user_define_application_exceptions.each do |exception|
68
+ # puts exception
69
+ APPLICATION_ERRORS.push(eval(exception))
70
+ end
71
+ APPLICATION_ERRORS.uniq!
72
+ APPLICATION_ERRORS.each do |error|
73
+ # puts error.class
74
+ # puts error
75
+ end
76
+ user_options.store(:application_exception_classes, APPLICATION_ERRORS)
77
+
78
+ if config["size"] != nil
79
+ user_options.store(:size, config["size"].to_i)
80
+ end
81
+
82
+ servers = "192.168.3.111:8888,192.168.2.111:8888".split(",")
83
+ servers.each do |server|
84
+ puts server
85
+ puts server.class
86
+ end
@@ -0,0 +1,97 @@
1
+ $:.unshift File.expand_path("../../test", __FILE__)
2
+ require 'thrift'
3
+ require 'foobar_service'
4
+ require 'foobar_types'
5
+
6
+ module Server
7
+ include Thrift
8
+
9
+ class FooHandler
10
+
11
+ def ping
12
+ puts "ping"
13
+ end
14
+
15
+ def getBar
16
+ bar = Foo::Bar.new
17
+ bar.fourth = "4"
18
+ bar.third = 3.0
19
+ bar.second = 2
20
+ bar.first = 1
21
+ return bar
22
+ end
23
+
24
+ def putFoo(foo)
25
+ puts foo.inspect
26
+ return true
27
+ end
28
+
29
+ def getTimeout
30
+ sleep(2)
31
+ return false
32
+ end
33
+
34
+ def getError()
35
+ puts "error"
36
+ raise ApplicationException.new("Exception")
37
+ return true
38
+ end
39
+
40
+ end
41
+
42
+ def self.start_server(host, port, serverClass)
43
+ handler = FooHandler.new
44
+ processor = Foo::FoobarService::Processor.new(handler)
45
+ transport = ServerSocket.new(host, port)
46
+ # transport = FramedTransport.new(transport)
47
+ transport_factory = FramedTransportFactory.new
48
+ args = [processor, transport, transport_factory]
49
+ if serverClass == NonblockingServer
50
+ # logger = Logger.new(STDOUT)
51
+ # logger.level = Logger::WARN
52
+ # args << logger
53
+ end
54
+ server = serverClass.new(*args)
55
+ @server_thread = Thread.new do
56
+ server.serve
57
+ end
58
+ @server = server
59
+ end
60
+
61
+ def self.shutdown
62
+ return if @server.nil?
63
+ if @server.respond_to? :shutdown
64
+ @server.shutdown
65
+ else
66
+ @server_thread.kill
67
+ end
68
+ end
69
+ end
70
+
71
+ def resolve_const(const)
72
+ const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
73
+ end
74
+
75
+ host = '127.0.0.1'
76
+ port = ARGV[0] || 9999
77
+
78
+ #serverklass = 'Thrift::SimpleServer'
79
+ #serverklass = 'Thrift::ThreadedServer'
80
+ serverklass = 'Thrift::ThreadPoolServer'
81
+ # serverklass = 'Thrift::NonblockingServer'
82
+
83
+ Server.start_server(host, port.to_i, resolve_const(serverklass))
84
+ # let our host know that the interpreter has started
85
+ # ideally we'd wait until the server was serving, but we don't have a hook for that
86
+
87
+ puts "server start on #{host}:#{port}"
88
+ puts ""
89
+
90
+ # STDOUT.flush
91
+
92
+ Marshal.load(STDIN) # wait until we're instructed to shut down
93
+
94
+ Server.shutdown
95
+
96
+ puts "server shutdown"
97
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrift-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Ted Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thrift
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ description: Thrift client
28
+ email: ted@ximalaya.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/thrift_client/abstract_server.rb
34
+ - lib/thrift_client/abstract_thrift_client.rb
35
+ - lib/thrift_client/multi_client_server.rb
36
+ - lib/thrift_client/single_client_server.rb
37
+ - lib/thrift_client/thrift.rb
38
+ - lib/thrift_client.rb
39
+ - test/client_test.rb
40
+ - test/foobar_constants.rb
41
+ - test/foobar_service.rb
42
+ - test/foobar_types.rb
43
+ - test/multiplexed_protocol_test.rb
44
+ - test/multiplexed_server.rb
45
+ - test/options_config_test.rb
46
+ - test/server.rb
47
+ homepage: http://www.ximalaya.com
48
+ licenses:
49
+ - MIT2.0
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.0.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: A Thrift client wrapper that encapsulates some common behavior.
71
+ test_files:
72
+ - test/client_test.rb
73
+ - test/foobar_constants.rb
74
+ - test/foobar_service.rb
75
+ - test/foobar_types.rb
76
+ - test/multiplexed_protocol_test.rb
77
+ - test/multiplexed_server.rb
78
+ - test/options_config_test.rb
79
+ - test/server.rb