hprose 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,347 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hprose/service.rb #
14
+ # #
15
+ # hprose service for ruby #
16
+ # #
17
+ # LastModified: Mar 8, 2014 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ require "hprose/common"
23
+ require "hprose/io"
24
+ require "thread"
25
+
26
+ module Hprose
27
+ class Service
28
+ private
29
+ include Tags
30
+ include ResultMode
31
+ public
32
+ attr_accessor :debug, :filter, :simple
33
+ attr_accessor :on_before_invoke, :on_after_invoke
34
+ attr_accessor :on_send_error
35
+ def initialize
36
+ @functions = {}
37
+ @funcNames = {}
38
+ @resultMode = {}
39
+ @simpleMode = {}
40
+ @debug = $DEBUG
41
+ @filter = Filter.new
42
+ @simple = false
43
+ @on_before_invoke = nil
44
+ @on_after_invoke = nil
45
+ @on_send_error = nil
46
+ end
47
+ def add(*args, &block)
48
+ case args.size
49
+ when 1 then
50
+ case args[0]
51
+ when Array then add_functions(args[0])
52
+ when Class then add_class_methods(args[0])
53
+ when String, Symbol then block_given? ? add_block(args[0], &block) : add_function(args[0])
54
+ when Proc, Method then add_function(args[0])
55
+ else add_instance_methods(args[0])
56
+ end
57
+ when 2 then
58
+ case args[0]
59
+ when Array then
60
+ case args[1]
61
+ when Array then add_functions(args[0], args[1])
62
+ else add_methods(args[0], args[1])
63
+ end
64
+ when Class then
65
+ case args[1]
66
+ when Class then add_class_methods(args[0], args[1])
67
+ when String, Symbol then add_class_methods(args[0], args[0], args[1])
68
+ else raise Exception.exception('wrong arguments')
69
+ end
70
+ when String, Symbol then
71
+ case args[1]
72
+ when String, Symbol then add_function(args[0], args[1])
73
+ else add_method(args[0], args[1])
74
+ end
75
+ when Proc, Method then
76
+ case args[1]
77
+ when String, Symbol then add_function(args[0], args[1])
78
+ else raise Exception.exception('wrong arguments')
79
+ end
80
+ else
81
+ case args[1]
82
+ when Class then add_instance_methods(args[0], args[1])
83
+ when String, Symbol then add_instance_methods(args[0], nil, args[1])
84
+ else raise Exception.exception('wrong arguments')
85
+ end
86
+ end
87
+ when 3 then
88
+ case args[0]
89
+ when Array then
90
+ if args[1].nil? then
91
+ case args[2]
92
+ when Array then add_functions(args[0], args[2])
93
+ else raise Exception.exception('wrong arguments')
94
+ end
95
+ else
96
+ case args[2]
97
+ when Array, String, Symbol then add_methods(args[0], args[1], args[2])
98
+ else raise Exception.exception('wrong arguments')
99
+ end
100
+ end
101
+ when Class then
102
+ case args[2]
103
+ when String, Symbol then
104
+ if args[1].is_a?(Class) then
105
+ add_class_methods(args[0], args[1], args[2])
106
+ else
107
+ add_instance_methods(args[1], args[0], args[2])
108
+ end
109
+ else raise Exception.exception('wrong arguments')
110
+ end
111
+ when String, Symbol then
112
+ case args[2]
113
+ when String, Symbol then
114
+ if args[1].nil? then
115
+ add_function(args[0], args[2])
116
+ else
117
+ add_method(args[0], args[1], args[2])
118
+ end
119
+ else raise Exception.exception('wrong arguments')
120
+ end
121
+ when Proc, Method then raise Exception.exception('wrong arguments')
122
+ else
123
+ if args[1].is_a?(Class) and (args[2].is_a?(String) or args[2].is_a?(Symbol)) then
124
+ add_instance_methods(args[0], args[1], args[2])
125
+ else
126
+ raise Exception.exception('wrong arguments')
127
+ end
128
+ end
129
+ else raise Exception.exception('wrong arguments')
130
+ end
131
+ end
132
+ def add_missing_function(function, resultMode = Normal, simple = nil)
133
+ add_function(function, '*', resultMode, simple)
134
+ end
135
+ def add_block(methodname, resultMode = Normal, simple = nil, &block)
136
+ if block_given? then
137
+ methodname = methodname.to_s if methodname.is_a?(Symbol)
138
+ aliasname = methodname.downcase
139
+ @functions[aliasname] = block
140
+ @funcNames[aliasname] = methodname
141
+ @resultMode[aliasname] = resultMode
142
+ @simpleMode[aliasname] = simple
143
+ else
144
+ raise Exception.exception('block must be given')
145
+ end
146
+ end
147
+ def add_function(function, aliasname = nil, resultMode = Normal, simple = nil)
148
+ function = function.to_s if function.is_a?(Symbol)
149
+ aliasname = aliasname.to_s if aliasname.is_a?(Symbol)
150
+ if function.is_a?(String) then
151
+ aliasname = function if aliasname.nil?
152
+ function = Object.method(function)
153
+ end
154
+ unless function.is_a?(Proc) or function.is_a?(Method) or function.respond_to?(:call) then
155
+ raise Exception.exception('function must be callable')
156
+ end
157
+ if aliasname.nil? then
158
+ if function.is_a?(Method) then
159
+ aliasname = function.inspect
160
+ aliasname[/#(.*?)#/] = ''
161
+ aliasname[/>$/] = ''
162
+ aliasname[/<(.*?)>\./] = '' if !aliasname[/<(.*?)>\./].nil?
163
+ else
164
+ raise Exception.exception('need a alias name for function')
165
+ end
166
+ end
167
+ name = aliasname.downcase
168
+ @functions[name] = function
169
+ @funcNames[name] = aliasname
170
+ @resultMode[name] = resultMode
171
+ @simpleMode[name] = simple
172
+ end
173
+ def add_functions(functions, aliases = nil, resultMode = Normal, simple = nil)
174
+ unless functions.is_a?(Array) then
175
+ raise Exception.exception('argument functions is not an array')
176
+ end
177
+ count = functions.size
178
+ unless aliases.nil? or aliases.is_a?(Array) and count == aliases.size then
179
+ raise Exception.exception('the count of functions is not matched with aliases')
180
+ end
181
+ count.times do |i|
182
+ function = functions[i]
183
+ if aliases.nil? then
184
+ add_function(function, nil, resultMode, simple)
185
+ else
186
+ add_function(function, aliases[i], resultMode, simple)
187
+ end
188
+ end
189
+ end
190
+ def add_method(methodname, belongto, aliasname = nil, resultMode = Normal, simple = nil)
191
+ function = belongto.method(methodname)
192
+ add_function(function, (aliasname.nil? ? methodname : aliasname), resultMode, simple)
193
+ end
194
+ def add_methods(methods, belongto, aliases = nil, resultMode = Normal, simple = nil)
195
+ unless methods.is_a?(Array) then
196
+ raise Exception.exception('argument methods is not an array')
197
+ end
198
+ aliases = aliases.to_s if aliases.is_a?(Symbol)
199
+ count = methods.size
200
+ if aliases.is_a?(String) then
201
+ alias_prefix = aliases
202
+ aliases = Array.new(count) { |i| alias_prefix + '_' + methods[i].to_s }
203
+ end
204
+ if not aliases.nil? and count != aliases.size then
205
+ raise Exception.exception('The count of methods is not matched with aliases')
206
+ end
207
+ count.times do |i|
208
+ method = methods[i]
209
+ function = belongto.method(method)
210
+ add_function(function, (aliases.nil? ? method : aliases[i]), resultMode, simple)
211
+ end
212
+ end
213
+ def add_instance_methods(obj, cls = nil, alias_prefix = nil, resultMode = Normal, simple = nil)
214
+ alias_prefix = alias_prefix.to_s if alias_prefix.is_a?(Symbol)
215
+ cls = obj.class if cls.nil?
216
+ methods = cls.public_instance_methods(false)
217
+ aliases = Array.new(methods.size) do |i|
218
+ if alias_prefix.nil? then
219
+ methods[i].to_s
220
+ else
221
+ alias_prefix + '_' + methods[i].to_s
222
+ end
223
+ end
224
+ methods.map! { |method| cls.instance_method(method).bind(obj) }
225
+ add_functions(methods, aliases, resultMode, simple)
226
+ end
227
+ def add_class_methods(cls, execcls = nil, alias_prefix = nil, resultMode = Normal, simple = nil)
228
+ alias_prefix = alias_prefix.to_s if alias_prefix.is_a?(Symbol)
229
+ execcls = cls if execcls.nil?
230
+ methods = cls.singleton_methods(false)
231
+ aliases = Array.new(methods.size) do |i|
232
+ if alias_prefix.nil? then
233
+ methods[i].to_s
234
+ else
235
+ alias_prefix + '_' + methods[i].to_s
236
+ end
237
+ end
238
+ methods.map! { |method| execcls.method(method) }
239
+ add_functions(methods, aliases, resultMode, simple)
240
+ end
241
+ protected
242
+ def response_end(ostream)
243
+ data = @filter.output_filter(ostream.string)
244
+ ostream.close
245
+ return data
246
+ end
247
+ def fix_args(args, arity, context)
248
+ args
249
+ end
250
+ def fire_error_event(e)
251
+ @on_send_error.call(e) unless @on_send_error.nil?
252
+ end
253
+ def do_error(e)
254
+ fire_error_event(e)
255
+ error = @debug ? e.backtrace.unshift(e.message).join("\r\n") : e.message
256
+ ostream = StringIO.new
257
+ writer = Writer.new(ostream, true)
258
+ ostream.putc(TagError)
259
+ writer.write_string(error)
260
+ ostream.putc(TagEnd)
261
+ return response_end(ostream)
262
+ end
263
+ def do_invoke(istream, context)
264
+ simpleReader = Reader.new(istream, true)
265
+ begin
266
+ name = simpleReader.read_string
267
+ aliasname = name.downcase
268
+ args = []
269
+ byref = false
270
+ result = nil
271
+ tag = simpleReader.check_tags([TagList, TagCall, TagEnd])
272
+ if tag == TagList then
273
+ reader = Reader.new(istream)
274
+ args = reader.read_list_without_tag
275
+ tag = reader.check_tags([TagTrue, TagCall, TagEnd])
276
+ if tag == TagTrue then
277
+ byref = true
278
+ tag = reader.check_tags([TagCall, TagEnd])
279
+ end
280
+ end
281
+ @on_before_invoke.call(name, args, byref) unless @on_before_invoke.nil?
282
+ result = nil
283
+ if @functions.has_key?(aliasname) then
284
+ function = @functions[aliasname]
285
+ resultMode = @resultMode[aliasname]
286
+ simple = @simpleMode[aliasname]
287
+ result = function.call(*fix_args(args, function.arity, context))
288
+ elsif @functions.has_key?('*') then
289
+ function = @functions['*']
290
+ resultMode = @resultMode['*']
291
+ simple = @simpleMode[aliasname]
292
+ result = function.call(name, args)
293
+ else
294
+ raise Exception.exception("Can't find this function " << name)
295
+ end
296
+ @on_after_invoke.call(name, args, byref, result) unless @on_after_invoke.nil?
297
+ ostream = StringIO.new
298
+ if resultMode == RawWithEndTag then
299
+ ostream.write(result)
300
+ return response_end(ostream)
301
+ elsif resultMode == Raw then
302
+ ostream.write(result)
303
+ else
304
+ ostream.putc(TagResult)
305
+ if resultMode == Serialized then
306
+ ostream.write(result)
307
+ else
308
+ simple = @simple if simple.nil?
309
+ writer = Writer.new(ostream, simple)
310
+ writer.serialize(result)
311
+ if byref then
312
+ writer.stream.putc(TagArgument)
313
+ writer.reset
314
+ writer.write_list(args)
315
+ end
316
+ end
317
+ end
318
+ end while tag == TagCall
319
+ writer.stream.putc(TagEnd)
320
+ return response_end(ostream)
321
+ end
322
+ def do_function_list()
323
+ ostream = StringIO.new
324
+ writer = Writer.new(ostream, true)
325
+ ostream.putc(TagFunctions)
326
+ writer.write_list(@funcNames.values)
327
+ ostream.putc(TagEnd)
328
+ return response_end(ostream)
329
+ end
330
+ def handle(data, context)
331
+ begin
332
+ data = @filter.input_filter(data)
333
+ istream = StringIO.new(data, 'rb')
334
+ tag = istream.getbyte
335
+ case tag
336
+ when TagCall then return do_invoke(istream, context)
337
+ when TagEnd then return do_function_list
338
+ else raise Exception.exception("Wrong Request: \r\n#{data}")
339
+ end
340
+ rescue ::Exception => e
341
+ return do_error(e)
342
+ ensure
343
+ istream.close unless istream.nil?
344
+ end
345
+ end
346
+ end # class Service
347
+ end # module Hprose
data/lib/hprose.rb ADDED
@@ -0,0 +1,50 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hprose.rb #
14
+ # #
15
+ # hprose for ruby #
16
+ # #
17
+ # LastModified: Mar 8, 2014 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ module Hprose
23
+ autoload :Exception, 'hprose/common'
24
+ autoload :ResultMode, 'hprose/common'
25
+ autoload :Filter, 'hprose/common'
26
+ autoload :Tags, 'hprose/io'
27
+ autoload :ClassManager, 'hprose/io'
28
+ autoload :RawReader, 'hprose/io'
29
+ autoload :Reader, 'hprose/io'
30
+ autoload :Writer, 'hprose/io'
31
+ autoload :Formatter, 'hprose/io'
32
+ autoload :Client, 'hprose/client'
33
+ autoload :HttpClient, 'hprose/httpclient'
34
+ autoload :Service, 'hprose/service'
35
+ autoload :HttpService, 'hprose/httpservice'
36
+ end
37
+
38
+ Object.const_set(:HproseException, Hprose.const_get(:Exception))
39
+ Object.const_set(:HproseResultMode, Hprose.const_get(:ResultMode))
40
+ Object.const_set(:HproseFilter, Hprose.const_get(:Filter))
41
+ Object.const_set(:HproseTags, Hprose.const_get(:Tags))
42
+ Object.const_set(:HproseClassManager, Hprose.const_get(:ClassManager))
43
+ Object.const_set(:HproseRawReader, Hprose.const_get(:RawReader))
44
+ Object.const_set(:HproseReader, Hprose.const_get(:Reader))
45
+ Object.const_set(:HproseWriter, Hprose.const_get(:Writer))
46
+ Object.const_set(:HproseFormatter, Hprose.const_get(:Formatter))
47
+ Object.const_set(:HproseClient, Hprose.const_get(:Client))
48
+ Object.const_set(:HproseHttpClient, Hprose.const_get(:HttpClient))
49
+ Object.const_set(:HproseService, Hprose.const_get(:Service))
50
+ Object.const_set(:HproseHttpService, Hprose.const_get(:HttpService))
@@ -0,0 +1,28 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hproseclient.rb #
14
+ # #
15
+ # hprose client for ruby #
16
+ # #
17
+ # LastModified: May 19, 2010 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ module Hprose
23
+ autoload :Client, 'hprose/client'
24
+ autoload :HttpClient, 'hprose/httpclient'
25
+ end
26
+
27
+ Object.const_set(:HproseClient, Hprose.const_get(:Client))
28
+ Object.const_set(:HproseHttpClient, Hprose.const_get(:HttpClient))
@@ -0,0 +1,30 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hproseio.rb #
14
+ # #
15
+ # hprose io for ruby #
16
+ # #
17
+ # LastModified: Dec 1, 2012 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ module Hprose
23
+ autoload :Exception, 'hprose/common'
24
+ autoload :ResultMode, 'hprose/common'
25
+ autoload :Filter, 'hprose/common'
26
+ end
27
+
28
+ Object.const_set(:HproseException, Hprose.const_get(:Exception))
29
+ Object.const_set(:HproseResultMode, Hprose.const_get(:ResultMode))
30
+ Object.const_set(:HproseFilter, Hprose.const_get(:Filter))
data/lib/hproseio.rb ADDED
@@ -0,0 +1,36 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hproseio.rb #
14
+ # #
15
+ # hprose io for ruby #
16
+ # #
17
+ # LastModified: Mar 8, 2014 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ module Hprose
23
+ autoload :Tags, 'hprose/io'
24
+ autoload :ClassManager, 'hprose/io'
25
+ autoload :RawReader, 'hprose/io'
26
+ autoload :Reader, 'hprose/io'
27
+ autoload :Writer, 'hprose/io'
28
+ autoload :Formatter, 'hprose/io'
29
+ end
30
+
31
+ Object.const_set(:HproseTags, Hprose.const_get(:Tags))
32
+ Object.const_set(:HproseClassManager, Hprose.const_get(:ClassManager))
33
+ Object.const_set(:HproseRawReader, Hprose.const_get(:RawReader))
34
+ Object.const_set(:HproseReader, Hprose.const_get(:Reader))
35
+ Object.const_set(:HproseWriter, Hprose.const_get(:Writer))
36
+ Object.const_set(:HproseFormatter, Hprose.const_get(:Formatter))
@@ -0,0 +1,28 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hproseserver.rb #
14
+ # #
15
+ # hprose server for ruby #
16
+ # #
17
+ # LastModified: May 19, 2010 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ module Hprose
23
+ autoload :Service, 'hprose/service'
24
+ autoload :HttpService, 'hprose/httpservice'
25
+ end
26
+
27
+ Object.const_set(:HproseService, Hprose.const_get(:Service))
28
+ Object.const_set(:HproseHttpService, Hprose.const_get(:HttpService))
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hprose
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ma Bingyao ( andot )
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: andot@hprose.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - examples/client.rb
21
+ - examples/server.rb
22
+ - lib/hprose/client.rb
23
+ - lib/hprose/common.rb
24
+ - lib/hprose/httpclient.rb
25
+ - lib/hprose/httpservice.rb
26
+ - lib/hprose/io.rb
27
+ - lib/hprose/service.rb
28
+ - lib/hprose.rb
29
+ - lib/hproseclient.rb
30
+ - lib/hprosecommon.rb
31
+ - lib/hproseio.rb
32
+ - lib/hproseserver.rb
33
+ - README.md
34
+ homepage: http://www.hprose.com/
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Hprose is a lightweight, secure, cross-domain, platform-independent, language-independent,
58
+ envirment-independent, complex object supported, reference parameters supported,
59
+ session supported, service-oriented, high performance remote object service engine.
60
+ This project is the client and server implementations of the Hprose for Ruby.
61
+ test_files: []