thrift-multiplexer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e28f0b220af33adce4196f6b8c67fb6d10009ac
4
+ data.tar.gz: c0d8c7b89ab7f5babd29287073f503bb4aa26cd9
5
+ SHA512:
6
+ metadata.gz: 668e26f36410e9ba6f7656469ddaab0ff66cd91b607de3efd4278fe3330aa62dc1a661349c36054b399a0293266f0bf503b5590c375e11e6c31c3952f8f99c55
7
+ data.tar.gz: a4ddd40ffb18b65dab3e1f255332e323126c83846f722849e0df258f0b3eb6b9cca04bc11df5e736b395e11ced8639fe745ca6650f256fa2a9666729d52f7dff
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thrift-multiplexer.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 André Aizim Kelmanson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,188 @@
1
+ # Thrift::Multiplexer
2
+
3
+ This gem implements multiplexing for [Apache Thrift](https://thrift.apache.org/).
4
+
5
+ For more information on Multiplexing Services, please access [THRIFT-1915](https://issues.apache.org/jira/browse/THRIFT-1915).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'thrift-multiplexer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install thrift-multiplexer
22
+
23
+ ## Usage
24
+
25
+ ### Example
26
+
27
+ This example is based on the original [Thrift Ruby Tutorial](https://thrift.apache.org/tutorial/rb).
28
+
29
+ #### Thrift files
30
+
31
+ You need to download [tutorial.thrift](https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/tutorial.thrift) and [shared.thrift](https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/shared.thrift).
32
+
33
+ To generate the Ruby code, run:
34
+
35
+ thrift -r --gen rb tutorial.thrift
36
+
37
+ #### Client
38
+
39
+ ```ruby
40
+ $:.push('gen-rb')
41
+ $:.unshift '../../lib/rb/lib'
42
+
43
+ require 'thrift'
44
+ require 'thrift/multiplexer'
45
+
46
+ require 'calculator'
47
+
48
+
49
+ begin
50
+ port = ARGV[0] || 9090
51
+
52
+ transport = Thrift::BufferedTransport.new(Thrift::Socket.new('127.0.0.1', port))
53
+ protocol = Thrift::MultiplexedProtocol.new(Thrift::BinaryProtocol.new(transport), 'Abc')
54
+ client = Calculator::Client.new(protocol)
55
+ protocol2 = Thrift::MultiplexedProtocol.new(Thrift::BinaryProtocol.new(transport), 'Xyz')
56
+ client2 = Calculator::Client.new(protocol)
57
+
58
+ transport.open()
59
+
60
+ client.ping()
61
+ print "ping()\n"
62
+
63
+ sum = client2.add(1,1)
64
+ print "1+1=", sum, "\n"
65
+
66
+ sum = client2.add(1,4)
67
+ print "1+4=", sum, "\n"
68
+
69
+ work = Work.new()
70
+
71
+ work.op = Operation::SUBTRACT
72
+ work.num1 = 15
73
+ work.num2 = 10
74
+ diff = client.calculate(1, work)
75
+ print "15-10=", diff, "\n"
76
+
77
+ log = client.getStruct(1)
78
+ print "Log: ", log.value, "\n"
79
+
80
+ begin
81
+ work.op = Operation::DIVIDE
82
+ work.num1 = 1
83
+ work.num2 = 0
84
+ quot = client.calculate(1, work)
85
+ puts "Whoa, we can divide by 0 now?"
86
+ rescue InvalidOperation => io
87
+ print "InvalidOperation: ", io.why, "\n"
88
+ end
89
+
90
+ client.zip()
91
+ print "zip\n"
92
+
93
+ transport.close()
94
+
95
+ rescue Thrift::Exception => tx
96
+ print 'Thrift::Exception: ', tx.message, "\n"
97
+ end
98
+ ```
99
+
100
+ #### Server
101
+
102
+ ```ruby
103
+ $:.push('gen-rb')
104
+ $:.unshift '../../lib/rb/lib'
105
+
106
+ require 'thrift'
107
+ require 'thrift/multiplexer'
108
+
109
+ require 'calculator'
110
+ require 'shared_types'
111
+
112
+ class CalculatorHandler
113
+ def initialize()
114
+ @log = {}
115
+ end
116
+
117
+ def ping()
118
+ puts "ping()"
119
+ end
120
+
121
+ def add(n1, n2)
122
+ print "add(", n1, ",", n2, ")\n"
123
+ return n1 + n2
124
+ end
125
+
126
+ def calculate(logid, work)
127
+ print "calculate(", logid, ", {", work.op, ",", work.num1, ",", work.num2,"})\n"
128
+ if work.op == Operation::ADD
129
+ val = work.num1 + work.num2
130
+ elsif work.op == Operation::SUBTRACT
131
+ val = work.num1 - work.num2
132
+ elsif work.op == Operation::MULTIPLY
133
+ val = work.num1 * work.num2
134
+ elsif work.op == Operation::DIVIDE
135
+ if work.num2 == 0
136
+ x = InvalidOperation.new()
137
+ x.what = work.op
138
+ x.why = "Cannot divide by 0"
139
+ raise x
140
+ end
141
+ val = work.num1 / work.num2
142
+ else
143
+ x = InvalidOperation.new()
144
+ x.what = work.op
145
+ x.why = "Invalid operation"
146
+ raise x
147
+ end
148
+
149
+ entry = SharedStruct.new()
150
+ entry.key = logid
151
+ entry.value = "#{val}"
152
+ @log[logid] = entry
153
+
154
+ return val
155
+
156
+ end
157
+
158
+ def getStruct(key)
159
+ print "getStruct(", key, ")\n"
160
+ return @log[key]
161
+ end
162
+
163
+ def zip()
164
+ print "zip\n"
165
+ end
166
+
167
+ end
168
+
169
+ handler = CalculatorHandler.new()
170
+ processor = Thrift::MultiplexedProcessor.new
171
+ processor.register_processor('Abc', Calculator::Processor.new(handler))
172
+ processor.register_processor('Xyz', Calculator::Processor.new(handler))
173
+ transport = Thrift::ServerSocket.new(9090)
174
+ transportFactory = Thrift::BufferedTransportFactory.new()
175
+ server = Thrift::SimpleServer.new(processor, transport, transportFactory)
176
+
177
+ puts "Starting the server..."
178
+ server.serve()
179
+ puts "done."
180
+ ```
181
+
182
+ ## Contributing
183
+
184
+ 1. Fork it ( https://github.com/investtools/thrift-multiplexer/fork )
185
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
186
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
187
+ 4. Push to the branch (`git push origin my-new-feature`)
188
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,57 @@
1
+ require 'thrift/protocol/protocol_decorator'
2
+
3
+ module Thrift
4
+ class MultiplexedProcessor
5
+ def initialize
6
+ @actual_processors = {}
7
+ end
8
+
9
+ def register_processor(service_name, processor)
10
+ @actual_processors[service_name] = processor
11
+ end
12
+
13
+ def process(iprot, oprot)
14
+ name, type, seqid = iprot.read_message_begin
15
+ check_type(type)
16
+ check_separator(name)
17
+ service_name, method = name.split(':')
18
+ processor(service_name).process(StoredMessageProtocol.new(iprot, [method, type, seqid]), oprot)
19
+ end
20
+
21
+ protected
22
+
23
+ def processor(service_name)
24
+ if @actual_processors.has_key?(service_name)
25
+ @actual_processors[service_name]
26
+ else
27
+ raise Thrift::Exception.new("Service name not found: #{service_name}. Did you forget to call #{self.class.name}#register_processor?")
28
+ end
29
+ end
30
+
31
+ def check_type(type)
32
+ unless [MessageTypes::CALL, MessageTypes::ONEWAY].include?(type)
33
+ raise Thrift::Exception.new('This should not have happened!?')
34
+ end
35
+ end
36
+
37
+ def check_separator(name)
38
+ if name.count(':') < 1
39
+ raise Thrift::Exception.new("Service name not found in message name: #{name}. Did you forget to use a Thrift::Protocol::MultiplexedProtocol in your client?")
40
+ end
41
+ end
42
+ end
43
+
44
+ class StoredMessageProtocol < BaseProtocol
45
+
46
+ include ProtocolDecorator
47
+
48
+ def initialize(protocol, message_begin)
49
+ super(protocol)
50
+ @message_begin = message_begin
51
+ end
52
+
53
+ def read_message_begin
54
+ @message_begin
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ require 'thrift/multiplexer/version'
2
+ require 'thrift/multiplexed_processor'
3
+ require 'thrift/protocol/multiplexed_protocol'
@@ -0,0 +1,5 @@
1
+ module Thrift
2
+ module Multiplexer
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ require 'thrift/protocol/protocol_decorator'
2
+
3
+ module Thrift
4
+ class MultiplexedProtocol < BaseProtocol
5
+
6
+ include ProtocolDecorator
7
+
8
+ def initialize(protocol, service_name)
9
+ super(protocol)
10
+ @service_name = service_name
11
+ end
12
+
13
+ def write_message_begin(name, type, seqid)
14
+ case type
15
+ when MessageTypes::CALL, MessageTypes::ONEWAY
16
+ @protocol.write_message_begin("#{@service_name}:#{name}", type, seqid)
17
+ else
18
+ @protocol.write_message_begin(name, type, seqid)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,176 @@
1
+ module Thrift
2
+ module ProtocolDecorator
3
+
4
+ def initialize(protocol)
5
+ @protocol = protocol
6
+ end
7
+
8
+ def trans
9
+ @protocol.trans
10
+ end
11
+
12
+ def write_message_begin(name, type, seqid)
13
+ @protocol.write_message_begin
14
+ end
15
+
16
+ def write_message_end
17
+ @protocol.write_message_end
18
+ end
19
+
20
+ def write_struct_begin(name)
21
+ @protocol.write_struct_begin(name)
22
+ end
23
+
24
+ def write_struct_end
25
+ @protocol.write_struct_end
26
+ end
27
+
28
+ def write_field_begin(name, type, id)
29
+ @protocol.write_field_begin(name, type, id)
30
+ end
31
+
32
+ def write_field_end
33
+ @protocol.write_field_end
34
+ end
35
+
36
+ def write_field_stop
37
+ @protocol.write_field_stop
38
+ end
39
+
40
+ def write_map_begin(ktype, vtype, size)
41
+ @protocol.write_map_begin(ktype, vtype, size)
42
+ end
43
+
44
+ def write_map_end
45
+ @protocol.write_map_end
46
+ end
47
+
48
+ def write_list_begin(etype, size)
49
+ @protocol.write_list_begin(etype, size)
50
+ end
51
+
52
+ def write_list_end
53
+ @protocol.write_list_end
54
+ end
55
+
56
+ def write_set_begin(etype, size)
57
+ @protocol.write_set_begin(etype, size)
58
+ end
59
+
60
+ def write_set_end
61
+ @protocol.write_set_end
62
+ end
63
+
64
+ def write_bool(bool)
65
+ @protocol.write_bool(bool)
66
+ end
67
+
68
+ def write_byte(byte)
69
+ @protocol.write_byte(byte)
70
+ end
71
+
72
+ def write_i16(i16)
73
+ @protocol.write_i16(i16)
74
+ end
75
+
76
+ def write_i32(i32)
77
+ @protocol.write_i32(i32)
78
+ end
79
+
80
+ def write_i64(i64)
81
+ @protocol.write_i64(i64)
82
+ end
83
+
84
+ def write_double(dub)
85
+ @protocol.write_double(dub)
86
+ end
87
+
88
+ def write_string(str)
89
+ @protocol.write_string(str)
90
+ end
91
+
92
+ def write_binary(buf)
93
+ @protocol.write_binary(buf)
94
+ end
95
+
96
+ def read_message_begin
97
+ @protocol.read_message_begin
98
+ end
99
+
100
+ def read_message_end
101
+ @protocol.read_message_end
102
+ end
103
+
104
+ def read_struct_begin
105
+ @protocol.read_struct_begin
106
+ end
107
+
108
+ def read_struct_end
109
+ @protocol.read_struct_end
110
+ end
111
+
112
+ def read_field_begin
113
+ @protocol.read_field_begin
114
+ end
115
+
116
+ def read_field_end
117
+ @protocol.read_field_end
118
+ end
119
+
120
+ def read_map_begin
121
+ @protocol.read_map_begin
122
+ end
123
+
124
+ def read_map_end
125
+ @protocol.read_map_end
126
+ end
127
+
128
+ def read_list_begin
129
+ @protocol.read_list_begin
130
+ end
131
+
132
+ def read_list_end
133
+ @protocol.read_list_end
134
+ end
135
+
136
+ def read_set_begin
137
+ @protocol.read_set_begin
138
+ end
139
+
140
+ def read_set_end
141
+ @protocol.read_set_end
142
+ end
143
+
144
+ def read_bool
145
+ @protocol.read_bool
146
+ end
147
+
148
+ def read_byte
149
+ @protocol.read_byte
150
+ end
151
+
152
+ def read_i16
153
+ @protocol.read_i16
154
+ end
155
+
156
+ def read_i32
157
+ @protocol.read_i32
158
+ end
159
+
160
+ def read_i64
161
+ @protocol.read_i64
162
+ end
163
+
164
+ def read_double
165
+ @protocol.read_double
166
+ end
167
+
168
+ def read_string
169
+ @protocol.read_string
170
+ end
171
+
172
+ def read_binary
173
+ @protocol.read_binary
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thrift/Multiplexer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thrift-multiplexer"
8
+ spec.version = Thrift::Multiplexer::VERSION
9
+ spec.authors = ["André Aizim Kelmanson"]
10
+ spec.email = ["akelmanson@gmail.com"]
11
+ spec.summary = %q{Thrift Multiplexer}
12
+ spec.description = %q{Thrift Multiplexer}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency "thrift"
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrift-multiplexer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - André Aizim Kelmanson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thrift
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Thrift Multiplexer
56
+ email:
57
+ - akelmanson@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/thrift/multiplexed_processor.rb
68
+ - lib/thrift/multiplexer.rb
69
+ - lib/thrift/multiplexer/version.rb
70
+ - lib/thrift/protocol/multiplexed_protocol.rb
71
+ - lib/thrift/protocol/protocol_decorator.rb
72
+ - thrift-mutiplexer.gemspec
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Thrift Multiplexer
97
+ test_files: []