Class: Hprose::Service

Inherits:
Object
  • Object
show all
Includes:
ResultMode, Tags
Defined in:
lib/hprose/service.rb

Direct Known Subclasses

HttpService, TcpServer

Constant Summary

Constants included from ResultMode

ResultMode::Normal, ResultMode::Raw, ResultMode::RawWithEndTag, ResultMode::Serialized

Constants included from Tags

Tags::TagArgument, Tags::TagBytes, Tags::TagCall, Tags::TagClass, Tags::TagClosebrace, Tags::TagDate, Tags::TagDouble, Tags::TagEmpty, Tags::TagEnd, Tags::TagError, Tags::TagFalse, Tags::TagFunctions, Tags::TagGuid, Tags::TagInfinity, Tags::TagInteger, Tags::TagList, Tags::TagLong, Tags::TagMap, Tags::TagNaN, Tags::TagNeg, Tags::TagNine, Tags::TagNull, Tags::TagObject, Tags::TagOpenbrace, Tags::TagPoint, Tags::TagPos, Tags::TagQuote, Tags::TagRef, Tags::TagResult, Tags::TagSemicolon, Tags::TagString, Tags::TagTime, Tags::TagTrue, Tags::TagUTC, Tags::TagUTF8Char, Tags::TagZero

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hprose/service.rb', line 34

def initialize
  @functions = {}
  @funcNames = {}
  @resultMode = {}
  @simpleMode = {}
  @debug = $DEBUG
  @filters = []
  @simple = false
  @on_before_invoke = nil
  @on_after_invoke = nil
  @on_send_error = nil
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



31
32
33
# File 'lib/hprose/service.rb', line 31

def debug
  @debug
end

#on_after_invokeObject

Returns the value of attribute on_after_invoke.



32
33
34
# File 'lib/hprose/service.rb', line 32

def on_after_invoke
  @on_after_invoke
end

#on_before_invokeObject

Returns the value of attribute on_before_invoke.



32
33
34
# File 'lib/hprose/service.rb', line 32

def on_before_invoke
  @on_before_invoke
end

#on_send_errorObject

Returns the value of attribute on_send_error.



33
34
35
# File 'lib/hprose/service.rb', line 33

def on_send_error
  @on_send_error
end

#simpleObject

Returns the value of attribute simple.



31
32
33
# File 'lib/hprose/service.rb', line 31

def simple
  @simple
end

Instance Method Details

#add(*args, &block) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hprose/service.rb', line 60

def add(*args, &block)
  case args.size
  when 1 then
    case args[0]
    when Array then add_functions(args[0])
    when Class then add_class_methods(args[0])
    when String, Symbol then block_given? ? add_block(args[0], &block) : add_function(args[0])
    when Proc, Method then add_function(args[0])
    else add_instance_methods(args[0])
    end
  when 2 then
    case args[0]
    when Array then
      case args[1]
      when Array then add_functions(args[0], args[1])
      else add_methods(args[0], args[1])
      end
    when Class then
      case args[1]
      when Class then add_class_methods(args[0], args[1])
      when String, Symbol then add_class_methods(args[0], args[0], args[1])
      else raise Exception.exception('wrong arguments')
      end
    when String, Symbol then
      case args[1]
      when String, Symbol then add_function(args[0], args[1])
      else add_method(args[0], args[1])
      end
    when Proc, Method then
      case args[1]
      when String, Symbol then add_function(args[0], args[1])
      else raise Exception.exception('wrong arguments')
      end
    else
      case args[1]
      when Class then add_instance_methods(args[0], args[1])
      when String, Symbol then add_instance_methods(args[0], nil, args[1])
      else raise Exception.exception('wrong arguments')
      end
    end
  when 3 then
    case args[0]
    when Array then
      if args[1].nil? then
        case args[2]
        when Array then add_functions(args[0], args[2])
        else raise Exception.exception('wrong arguments')
        end
      else
        case args[2]
        when Array, String, Symbol then add_methods(args[0], args[1], args[2])
        else raise Exception.exception('wrong arguments')
        end
      end
    when Class then
      case args[2]
      when String, Symbol then
        if args[1].is_a?(Class) then
          add_class_methods(args[0], args[1], args[2])
        else
          add_instance_methods(args[1], args[0], args[2])
        end
      else raise Exception.exception('wrong arguments')
      end
    when String, Symbol then
      case args[2]
      when String, Symbol then
        if args[1].nil? then
          add_function(args[0], args[2])
        else
          add_method(args[0], args[1], args[2])
        end
      else raise Exception.exception('wrong arguments')
      end
    when Proc, Method then raise Exception.exception('wrong arguments')
    else
      if args[1].is_a?(Class) and (args[2].is_a?(String) or args[2].is_a?(Symbol)) then
        add_instance_methods(args[0], args[1], args[2])
      else
        raise Exception.exception('wrong arguments')
      end
    end
  else raise Exception.exception('wrong arguments')
  end
end

#add_block(methodname, resultMode = Normal, simple = nil, &block) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hprose/service.rb', line 148

def add_block(methodname, resultMode = Normal, simple = nil, &block)
  if block_given? then
    methodname = methodname.to_s if methodname.is_a?(Symbol)
    aliasname = methodname.downcase
    @functions[aliasname] = block
    @funcNames[aliasname] = methodname
    @resultMode[aliasname] = resultMode
    @simpleMode[aliasname] = simple
  else
    raise Exception.exception('block must be given')
  end
end

#add_class_methods(cls, execcls = nil, alias_prefix = nil, resultMode = Normal, simple = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/hprose/service.rb', line 240

def add_class_methods(cls, execcls = nil, alias_prefix = nil, resultMode = Normal, simple = nil)
  alias_prefix = alias_prefix.to_s if alias_prefix.is_a?(Symbol)
  execcls = cls if execcls.nil?
  methods = cls.singleton_methods(false)
  aliases = Array.new(methods.size) do |i|
    if alias_prefix.nil? then
      methods[i].to_s
    else
      alias_prefix + '_' + methods[i].to_s
    end
  end
  methods.map! { |method| execcls.method(method) }
  add_functions(methods, aliases, resultMode, simple)
end

#add_filter(filter) ⇒ Object



54
55
56
# File 'lib/hprose/service.rb', line 54

def add_filter(filter)
  @filters << filter
end

#add_function(function, aliasname = nil, resultMode = Normal, simple = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hprose/service.rb', line 160

def add_function(function, aliasname = nil, resultMode = Normal, simple = nil)
  function = function.to_s if function.is_a?(Symbol)
  aliasname = aliasname.to_s if aliasname.is_a?(Symbol)
  if function.is_a?(String) then
    aliasname = function if aliasname.nil?
    function = Object.method(function)
  end
  unless function.is_a?(Proc) or function.is_a?(Method) or function.respond_to?(:call) then
    raise Exception.exception('function must be callable')
  end
  if aliasname.nil? then
    if function.is_a?(Method) then
      aliasname = function.inspect
      aliasname[/#(.*?)#/] = ''
      aliasname[/>$/] = ''
      aliasname[/<(.*?)>\./] = '' if !aliasname[/<(.*?)>\./].nil?
    else
      raise Exception.exception('need a alias name for function')
    end
  end
  name = aliasname.downcase
  @functions[name] = function
  @funcNames[name] = aliasname
  @resultMode[name] = resultMode
  @simpleMode[name] = simple
end

#add_functions(functions, aliases = nil, resultMode = Normal, simple = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/hprose/service.rb', line 186

def add_functions(functions, aliases = nil, resultMode = Normal, simple = nil)
  unless functions.is_a?(Array) then
    raise Exception.exception('argument functions is not an array')
  end
  count = functions.size
  unless aliases.nil? or aliases.is_a?(Array) and count == aliases.size then
    raise Exception.exception('the count of functions is not matched with aliases')
  end
  count.times do |i|
    function = functions[i]
    if aliases.nil? then
      add_function(function, nil, resultMode, simple)
    else
      add_function(function, aliases[i], resultMode, simple)
    end
  end
end

#add_instance_methods(obj, cls = nil, alias_prefix = nil, resultMode = Normal, simple = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/hprose/service.rb', line 226

def add_instance_methods(obj, cls = nil, alias_prefix = nil, resultMode = Normal, simple = nil)
  alias_prefix = alias_prefix.to_s if alias_prefix.is_a?(Symbol)
  cls = obj.class if cls.nil?
  methods = cls.public_instance_methods(false)
  aliases = Array.new(methods.size) do |i|
    if alias_prefix.nil? then
      methods[i].to_s
    else
      alias_prefix + '_' + methods[i].to_s
    end
  end
  methods.map! { |method| cls.instance_method(method).bind(obj) }
  add_functions(methods, aliases, resultMode, simple)
end

#add_method(methodname, belongto, aliasname = nil, resultMode = Normal, simple = nil) ⇒ Object



203
204
205
206
# File 'lib/hprose/service.rb', line 203

def add_method(methodname, belongto, aliasname = nil, resultMode = Normal, simple = nil)
  function = belongto.method(methodname)
  add_function(function, (aliasname.nil? ? methodname : aliasname), resultMode, simple)
end

#add_methods(methods, belongto, aliases = nil, resultMode = Normal, simple = nil) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hprose/service.rb', line 207

def add_methods(methods, belongto, aliases = nil, resultMode = Normal, simple = nil)
  unless methods.is_a?(Array) then
    raise Exception.exception('argument methods is not an array')
  end
  aliases = aliases.to_s if aliases.is_a?(Symbol)
  count = methods.size
  if aliases.is_a?(String) then
    alias_prefix = aliases
    aliases = Array.new(count) { |i| alias_prefix + '_' + methods[i].to_s }
  end
  if not aliases.nil? and count != aliases.size then
    raise Exception.exception('The count of methods is not matched with aliases')
  end
  count.times do |i|
    method = methods[i]
    function = belongto.method(method)
    add_function(function, (aliases.nil? ? method : aliases[i]), resultMode, simple)
  end
end

#add_missing_function(function, resultMode = Normal, simple = nil) ⇒ Object



145
146
147
# File 'lib/hprose/service.rb', line 145

def add_missing_function(function, resultMode = Normal, simple = nil)
  add_function(function, '*', resultMode, simple)
end

#filterObject



46
47
48
49
# File 'lib/hprose/service.rb', line 46

def filter
  return nil if @filters.empty?
  return @filters[0]
end

#filter=(filter) ⇒ Object



50
51
52
53
# File 'lib/hprose/service.rb', line 50

def filter=(filter)
  @filters.clear
  @filters << filter unless (filter.nil?)
end

#remove_filter(filter) ⇒ Object



57
58
59
# File 'lib/hprose/service.rb', line 57

def remove_filter(filter)
  @filters.delete(filter)
end