woolen_common 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +31 -0
  3. data/ext/woolen_common/extconf.rb +26 -0
  4. data/ext/woolen_common/linux.h +3 -0
  5. data/ext/woolen_common/win.c +18 -0
  6. data/ext/woolen_common/win.h +4 -0
  7. data/ext/woolen_common/win/puts_color.c +139 -0
  8. data/ext/woolen_common/win/puts_color.h +5 -0
  9. data/ext/woolen_common/woolen_common.c +20 -0
  10. data/ext/woolen_common/woolen_common.h +7 -0
  11. data/lib/woolen_common.rb +39 -0
  12. data/lib/woolen_common/abstract_middleware/builder.rb +138 -0
  13. data/lib/woolen_common/abstract_middleware/map_cfg_manager.rb +52 -0
  14. data/lib/woolen_common/abstract_middleware/runner.rb +72 -0
  15. data/lib/woolen_common/action_pool_proxy.rb +28 -0
  16. data/lib/woolen_common/actionpool.rb +10 -0
  17. data/lib/woolen_common/actionpool/pool.rb +295 -0
  18. data/lib/woolen_common/actionpool/queue.rb +41 -0
  19. data/lib/woolen_common/actionpool/thread.rb +181 -0
  20. data/lib/woolen_common/addr_helper.rb +93 -0
  21. data/lib/woolen_common/cache.rb +305 -0
  22. data/lib/woolen_common/common_helper.rb +42 -0
  23. data/lib/woolen_common/config_manager.rb +36 -0
  24. data/lib/woolen_common/drb_helper.rb +125 -0
  25. data/lib/woolen_common/ffi/win32_kernel32.rb +86 -0
  26. data/lib/woolen_common/logger.rb +419 -0
  27. data/lib/woolen_common/pcap/mu/fixnum_ext.rb +8 -0
  28. data/lib/woolen_common/pcap/mu/pcap/ethernet.rb +164 -0
  29. data/lib/woolen_common/pcap/mu/pcap/header.rb +76 -0
  30. data/lib/woolen_common/pcap/mu/pcap/io_pair.rb +68 -0
  31. data/lib/woolen_common/pcap/mu/pcap/io_wrapper.rb +77 -0
  32. data/lib/woolen_common/pcap/mu/pcap/ip.rb +62 -0
  33. data/lib/woolen_common/pcap/mu/pcap/ipv4.rb +274 -0
  34. data/lib/woolen_common/pcap/mu/pcap/ipv6.rb +149 -0
  35. data/lib/woolen_common/pcap/mu/pcap/packet.rb +106 -0
  36. data/lib/woolen_common/pcap/mu/pcap/pkthdr.rb +162 -0
  37. data/lib/woolen_common/pcap/mu/pcap/reader.rb +62 -0
  38. data/lib/woolen_common/pcap/mu/pcap/reader/http_family.rb +175 -0
  39. data/lib/woolen_common/pcap/mu/pcap/sctp.rb +369 -0
  40. data/lib/woolen_common/pcap/mu/pcap/sctp/chunk.rb +124 -0
  41. data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/data.rb +135 -0
  42. data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init.rb +101 -0
  43. data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init_ack.rb +69 -0
  44. data/lib/woolen_common/pcap/mu/pcap/sctp/parameter.rb +111 -0
  45. data/lib/woolen_common/pcap/mu/pcap/sctp/parameter/ip_address.rb +49 -0
  46. data/lib/woolen_common/pcap/mu/pcap/stream_packetizer.rb +74 -0
  47. data/lib/woolen_common/pcap/mu/pcap/tcp.rb +522 -0
  48. data/lib/woolen_common/pcap/mu/pcap/udp.rb +81 -0
  49. data/lib/woolen_common/pcap/mu/scenario/pcap.rb +175 -0
  50. data/lib/woolen_common/pcap/mu/scenario/pcap/fields.rb +51 -0
  51. data/lib/woolen_common/pcap/mu/scenario/pcap/rtp.rb +72 -0
  52. data/lib/woolen_common/pcap/pcap.rb +115 -0
  53. data/lib/woolen_common/pcap/readme.md +72 -0
  54. data/lib/woolen_common/ruby_ext/blank.rb +126 -0
  55. data/lib/woolen_common/ruby_ext/drb_ext.rb +7 -0
  56. data/lib/woolen_common/ruby_ext/string.rb +116 -0
  57. data/lib/woolen_common/ruby_ext/win32_ole.rb +4 -0
  58. data/lib/woolen_common/ruby_proxy.rb +5 -0
  59. data/lib/woolen_common/ruby_proxy/client.rb +305 -0
  60. data/lib/woolen_common/ruby_proxy/config.rb +44 -0
  61. data/lib/woolen_common/ruby_proxy/exceptions.rb +17 -0
  62. data/lib/woolen_common/ruby_proxy/proxy.rb +157 -0
  63. data/lib/woolen_common/ruby_proxy/proxy_global_set.rb +44 -0
  64. data/lib/woolen_common/ruby_proxy/proxy_load.rb +34 -0
  65. data/lib/woolen_common/ruby_proxy/server.rb +53 -0
  66. data/lib/woolen_common/splib.rb +36 -0
  67. data/lib/woolen_common/splib/Array.rb +33 -0
  68. data/lib/woolen_common/splib/CodeReloader.rb +59 -0
  69. data/lib/woolen_common/splib/Constants.rb +44 -0
  70. data/lib/woolen_common/splib/Conversions.rb +47 -0
  71. data/lib/woolen_common/splib/Exec.rb +132 -0
  72. data/lib/woolen_common/splib/Float.rb +13 -0
  73. data/lib/woolen_common/splib/HumanIdealRandomIterator.rb +40 -0
  74. data/lib/woolen_common/splib/Monitor.rb +214 -0
  75. data/lib/woolen_common/splib/PriorityQueue.rb +110 -0
  76. data/lib/woolen_common/splib/Sleep.rb +10 -0
  77. data/lib/woolen_common/splib/UrlShorteners.rb +48 -0
  78. data/lib/woolen_common/ssh_proxy.rb +146 -0
  79. data/lib/woolen_common/system_helper.rb +123 -0
  80. data/lib/woolen_common/system_monitor.rb +23 -0
  81. data/lib/woolen_common/system_monitor/linux_monitor.rb +250 -0
  82. data/lib/woolen_common/system_monitor/windows_monitor.rb +145 -0
  83. data/lib/woolen_common/type_helper.rb +42 -0
  84. data/lib/woolen_common/ver_ctrl_middle_ware.rb +92 -0
  85. data/lib/woolen_common/version.rb +3 -0
  86. metadata +210 -0
@@ -0,0 +1,72 @@
1
+ PCAP parser
2
+ =========
3
+
4
+ ## 使用方法
5
+
6
+ ### 解析报文
7
+
8
+
9
+
10
+ ether = Mu::Pcap::Ethernet.from_bytes pkt_str
11
+
12
+ puts ether.payload
13
+
14
+ 即: 一个报文( 必须是纯字符串 ), 被递归解析, 每个 `payload` 是它上一层的协议类( 比如 `Ethernet`, `IPv4`, `UDP` ), 下面是一些常用的方法:
15
+
16
+ * Ethernet
17
+
18
+ * `from_bytes(str)`
19
+
20
+ 解析报文, 并返回特定的类(公共)
21
+
22
+ * `to_bytes`
23
+
24
+ 重新生成报文, 会重新计算checksum(公共)
25
+
26
+ * `payload`
27
+
28
+ 负荷, 通俗叫报文内容, 如果还有被支持的上层协议, 则返回相对应的类. 则已经没有, 则返回真正的负荷.(公共)
29
+
30
+ * `src(=)`, `dst(=)`, `type(=)`
31
+
32
+ 源物理地址, 目的物理地址, 以及上层负载类型( IP, IP6, ARP, PPPOE, 802_1Q )
33
+
34
+
35
+ * IPv4
36
+
37
+ * `:ip_id`, `:offset`, `:ttl`, `:proto`, `:src`, `:dst`, `:dscp`
38
+
39
+ 标志, 段偏移, 生存期, 上层协议(TCP,UDP, SCTP), 源IP, 目的IP, TOS标记
40
+
41
+ * v4?
42
+
43
+ 返回 true
44
+
45
+ * fragment?
46
+
47
+ 检查是否分片
48
+
49
+ * IPv6
50
+
51
+ * hop_limit
52
+
53
+ * next_header
54
+
55
+ * TCP
56
+
57
+ * `src_port`, `dst_port`, `seq`, `ack`, `flags`, `window`, `urgent`, `mss`, `proto_family`
58
+
59
+ 不一一说明, `flags` 可以直接与 ( `TH_FIN`, `TH_SYN`, `TH_RST`, `TH_PUSH`, `TH_ACK`, `TH_URG`, `TH_ECE`, `TH_CWR`) 相与(&), 以判断标识.
60
+
61
+ * UDP
62
+
63
+ * `dst_port`, `src_port`
64
+
65
+ 更详细的指导, 请查看 `rdoc`:
66
+
67
+ 在命令行输入 `gem server`, 打开浏览器, 输入 `http://localhost:8808`.
68
+
69
+ ## Thanks to:
70
+ Fork from : <https://github.com/mudynamics/pcapr-local/tree/master/lib/mu>
71
+
72
+ Thanks to mudynamics, github is <https://github.com/mudynamics>
@@ -0,0 +1,126 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ class Object
4
+ # An object is blank if it's false, empty, or a whitespace string.
5
+ # For example, "", " ", +nil+, [], and {} are all blank.
6
+ #
7
+ # This simplifies:
8
+ #
9
+ # if address.nil? || address.empty?
10
+ #
11
+ # ...to:
12
+ #
13
+ # if address.blank?
14
+ def blank?
15
+ respond_to?(:empty?) ? empty? : !self
16
+ end
17
+
18
+ # An object is present if it's not <tt>blank?</tt>.
19
+ def present?
20
+ !blank?
21
+ end
22
+
23
+ # Returns object if it's <tt>present?</tt> otherwise returns +nil+.
24
+ # <tt>object.presence</tt> is equivalent to <tt>object.present? ? object : nil</tt>.
25
+ #
26
+ # This is handy for any representation of objects where blank is the same
27
+ # as not present at all. For example, this simplifies a common check for
28
+ # HTTP POST/query parameters:
29
+ #
30
+ # state = params[:state] if params[:state].present?
31
+ # country = params[:country] if params[:country].present?
32
+ # region = state || country || 'US'
33
+ #
34
+ # ...becomes:
35
+ #
36
+ # region = params[:state].presence || params[:country].presence || 'US'
37
+ def presence
38
+ self if present?
39
+ end
40
+ end
41
+
42
+ class NilClass
43
+ # +nil+ is blank:
44
+ #
45
+ # nil.blank? # => true
46
+ #
47
+ def blank?
48
+ true
49
+ end
50
+ end
51
+
52
+ class FalseClass
53
+ # +false+ is blank:
54
+ #
55
+ # false.blank? # => true
56
+ #
57
+ def blank?
58
+ true
59
+ end
60
+ end
61
+
62
+ class TrueClass
63
+ # +true+ is not blank:
64
+ #
65
+ # true.blank? # => false
66
+ #
67
+ def blank?
68
+ false
69
+ end
70
+ end
71
+
72
+ class Array
73
+ # An array is blank if it's empty:
74
+ #
75
+ # [].blank? # => true
76
+ # [1,2,3].blank? # => false
77
+ #
78
+ alias_method :blank?, :empty?
79
+ end
80
+
81
+ class Hash
82
+ # A hash is blank if it's empty:
83
+ #
84
+ # {}.blank? # => true
85
+ # {:key => 'value'}.blank? # => false
86
+ #
87
+ alias_method :blank?, :empty?
88
+ end
89
+
90
+ class String
91
+ # 0x3000: fullwidth whitespace
92
+ NON_WHITESPACE_REGEXP = %r![^\s#{[0x3000].pack("U")}]! unless defined? NON_WHITESPACE_REGEXP
93
+
94
+ # A string is blank if it's empty or contains whitespaces only:
95
+ #
96
+ # "".blank? # => true
97
+ # " ".blank? # => true
98
+ # " ".blank? # => true
99
+ # " something here ".blank? # => false
100
+ #
101
+ def blank?
102
+ if self == '[BLANK]'
103
+ return true
104
+ end
105
+ # 1.8 does not takes [:space:] properly
106
+ if encoding_aware?
107
+ self !~ /[^[:space:]]/
108
+ else
109
+ self !~ NON_WHITESPACE_REGEXP
110
+ end
111
+ end
112
+ def is_na?
113
+ self.strip.upcase.include? 'NA'
114
+ end
115
+ end
116
+
117
+ class Numeric #:nodoc:
118
+ # No number is blank:
119
+ #
120
+ # 1.blank? # => false
121
+ # 0.blank? # => false
122
+ #
123
+ def blank?
124
+ false
125
+ end
126
+ end
@@ -0,0 +1,7 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'drb'
3
+ module DRb
4
+ class DRbObject
5
+ undef :type rescue nil
6
+ end
7
+ end
@@ -0,0 +1,116 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
3
+ class String # :nodoc: all
4
+
5
+ if defined?(Encoding) && ''.respond_to?(:encode)
6
+ def encoding_aware?
7
+ true
8
+ end
9
+ else
10
+ def encoding_aware?
11
+ false
12
+ end
13
+ end
14
+
15
+ #判断是否为utf8
16
+ def utf8?
17
+ if String.method_defined?(:encode)
18
+ return true if self.force_encoding("UTF-8").valid_encoding?
19
+ return false
20
+ else
21
+ unpack('U*') rescue return false
22
+ return true
23
+ end
24
+ end
25
+
26
+ #TODO:参数功能目前没有用
27
+ def to_utf8(enc='GBK')
28
+ if self.respond_to?(:encode)
29
+ return self if utf8?
30
+ self.encode('UTF-8', enc)
31
+ else
32
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
33
+ return self if utf8?
34
+ begin
35
+ Iconv.iconv('UTF-8', enc, self).at(0).to_s
36
+ rescue
37
+ self
38
+ end
39
+ end
40
+ end
41
+
42
+ #TODO:参数功能目前没有用
43
+ def to_gbk(enc='UTF-8')
44
+ if self.respond_to?(:encode)
45
+ return self unless utf8?
46
+ self.encode('GBK', enc)
47
+ else
48
+ return self unless utf8?
49
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
50
+ begin
51
+ Iconv.iconv('GBK', enc, self).at(0).to_s
52
+ rescue
53
+ self
54
+ end
55
+ end
56
+ end
57
+
58
+ def to_gb2312(enc = 'UTF-8')
59
+ # require 'iconv'
60
+ # Iconv.iconv('GB2312','UTF-8',self).at(0).to_s
61
+ if self.respond_to?(:encode)
62
+ self.encode('GB2312', enc)
63
+ else
64
+ begin
65
+ self.unpack("U*")
66
+ require 'iconv' if RUBY_VERSION.to_f < 1.9
67
+ Iconv.iconv('GB2312', enc, self).at(0).to_s
68
+ rescue ::ArgumentError
69
+ self
70
+ end
71
+ end
72
+ end
73
+
74
+ def to_arr()
75
+ self.split('&') #暂时未考虑&在\之后的问题,fix this bug
76
+ end
77
+
78
+ def __camelize(first_letter_in_uppercase=true)
79
+ if first_letter_in_uppercase
80
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
81
+ else
82
+ self.to_s[0].chr.downcase + camelize(self)[1..-1]
83
+ end
84
+ end
85
+
86
+ alias camelize __camelize
87
+
88
+ def __underscore
89
+ word = self.to_s.dup
90
+ word.gsub!(/::/, '/')
91
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
92
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
93
+ word.tr!("-", "_")
94
+ word.downcase!
95
+ word
96
+ end
97
+
98
+ alias underscore __underscore
99
+
100
+ end
101
+
102
+ class << nil
103
+ def to_utf8
104
+ ''
105
+ end
106
+ end
107
+
108
+
109
+ if $0 == __FILE__
110
+ # require 'string'
111
+ puts "中文123中文".to_gbk.dump
112
+ puts "中文123中文".to_gb2312.dump
113
+ puts File.join("中文abc/abc", "中文").to_gbk.dump
114
+ puts File.join("中文abc/abc", "中文").to_gb2312.dump
115
+ puts 'ab_c'.camelize
116
+ end
@@ -0,0 +1,4 @@
1
+ require 'drb'
2
+ class WIN32OLE
3
+ include DRb::DRbUndumped
4
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "#{File.join(File.dirname(__FILE__), 'ruby_proxy/exceptions')}"
3
+ require "#{File.join(File.dirname(__FILE__), 'ruby_proxy/config')}"
4
+ require "#{File.join(File.dirname(__FILE__), 'ruby_proxy/client')}"
5
+ require "#{File.join(File.dirname(__FILE__), 'ruby_proxy/server')}"
@@ -0,0 +1,305 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'drb'
3
+ require "#{File.join(File.dirname(__FILE__), '..', 'ruby_ext/drb_ext')}"
4
+ require "#{File.join(File.dirname(__FILE__), 'exceptions')}"
5
+ require 'timeout'
6
+ require 'socket'
7
+
8
+ module ATU
9
+ class ATUConfig
10
+ class << self
11
+ attr_accessor :timeout
12
+ end
13
+ end
14
+ ATUConfig.timeout = 900
15
+ class ClientRet
16
+ include WoolenCommon::ToolLogger
17
+ class << self
18
+ attr_accessor :job_id
19
+ def check_job_done
20
+ # 60 * 10 / 0.5 10分钟内跑不完认为任务结束
21
+ start_time = Time.now.to_i
22
+ cnt = 0
23
+ loop do
24
+ if RubyProxy::DRbClient.client.is_job_done?(self.job_id)
25
+ debug "job[#{job_id}] done"
26
+ return true
27
+ else
28
+ cnt += 1
29
+ if cnt % 20 == 1
30
+ debug "job[#{job_id}] is still running"
31
+ end
32
+ sleep 0.5
33
+ if Time.now.to_i - start_time > ATUConfig.timeout
34
+ warn "the time to check job done is over #{ATUConfig.timeout} sec"
35
+ return false
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def get_job_ret
42
+ if check_job_done
43
+ return RubyProxy::DRbClient.client.get_the_job_ret(self.job_id)
44
+ end
45
+ nil
46
+ end
47
+ end
48
+ end
49
+
50
+ class << self
51
+ alias require_old require
52
+
53
+ def require(arg)
54
+ #TODO: path maybe confict
55
+ #~ path= File.expand_path File.join(Dir.pwd,arg)
56
+ #~ path += '.rb'
57
+ RubyProxy::DRbClient.proxy_load(arg)
58
+ end
59
+
60
+ def [](arg)
61
+ RubyProxy::DRbClient.proxy_global_get(arg)
62
+ end
63
+
64
+ def []=(arg, var)
65
+ RubyProxy::DRbClient.proxy_global_set(arg, var)
66
+ end
67
+
68
+ #load path
69
+ def <<(path)
70
+ RubyProxy::DRbClient.add_load_path(path)
71
+ end
72
+ end
73
+
74
+ #entry
75
+ def self.const_missing(name)
76
+ name = self.name.to_s + '::' + name.to_s
77
+ type = RubyProxy::DRbClient.client.proxy_type(name)
78
+ make_kclass(name, type)
79
+ end
80
+
81
+ def self.make_kclass(name, type)
82
+ #puts "make_kclass: #{name}, #{type}"
83
+ case type
84
+ when 'Class'
85
+ RubyProxy::KlassFactory.make_class(name) do |klass|
86
+ klass.class_eval do
87
+ def initialize(*arg)
88
+ @proxy = RubyProxy::DRbClient.client.proxy(self.class.to_s, "new", *arg)
89
+ end
90
+
91
+ # I think all methods should be proxyed by remote
92
+ undef :type rescue nil
93
+ undef :to_s
94
+ undef :to_a if respond_to?(:to_a)
95
+ undef :methods
96
+
97
+ def __proxy
98
+ @proxy
99
+ end
100
+
101
+ def method_missing(name, *arg, &block)
102
+ #return if @proxy.nil?
103
+ #puts "#{@proxy.methods(false)}"
104
+ #puts "proxy = #{@proxy} method=#{name} arg=#{arg.join(',')}"
105
+ @proxy.__send__(name, *arg, &block)
106
+ end
107
+
108
+ def self.const_missing(name)
109
+ name = self.name.to_s + "::" + name.to_s
110
+ type = RubyProxy::DRbClient.client.proxy_type(name.to_s)
111
+ ret = ATU::make_kclass(name, type)
112
+ case type
113
+ when 'Module', 'Class'
114
+ return ret
115
+ end
116
+ RubyProxy::DRbClient.client.proxy(name.to_s)
117
+ end
118
+
119
+ # TODO: block not support now
120
+ def self.method_missing(name, *arg, &block)
121
+ # puts "need to do method_missing :#{name}"
122
+ RubyProxy::DRbClient.client.copy_env(ENV.to_hash)
123
+ ClientRet.job_id = RubyProxy::DRbClient.client.proxy(self.name.to_s, name.to_s, *arg, &block)
124
+ ClientRet.get_job_ret
125
+ end
126
+
127
+ end
128
+ end
129
+ when 'Module'
130
+ RubyProxy::KlassFactory.make_module(name) do |mod|
131
+ mod.class_eval do
132
+ def self.method_missing(name, *arg)
133
+ RubyProxy::DRbClient.client.proxy(self.name.to_s, name.to_s, *arg)
134
+ end
135
+
136
+ def self.const_missing(name)
137
+ name = self.name.to_s + "::" + name.to_s
138
+ #puts "in module const_missing : #{name}"
139
+ type = RubyProxy::DRbClient.client.proxy_type(name.to_s)
140
+ ret = ATU::make_kclass(name, type)
141
+ case type
142
+ when 'Module', 'Class'
143
+ return ret
144
+ end
145
+ RubyProxy::DRbClient.client.proxy(name.to_s)
146
+ end
147
+ end
148
+ end
149
+ else
150
+
151
+ end
152
+
153
+ end
154
+
155
+ end
156
+
157
+
158
+ module RubyProxy
159
+
160
+ class KlassFactory
161
+ def self.make_klass(klass_name, type)
162
+ case type
163
+ when 'Class'
164
+ make_class(klass_name)
165
+ when 'Module'
166
+ make_module(klass_name)
167
+ else
168
+ raise TypeError, "wrong make_klass type: #{type}"
169
+ end
170
+ end
171
+
172
+ # generate proxy class
173
+ def self.make_class(klass_name)
174
+ klass_name = klass_name.to_s
175
+ name_ok?(klass_name)
176
+ eval <<-EOF
177
+ class #{klass_name}
178
+ end
179
+ EOF
180
+ klass = eval(klass_name)
181
+ yield klass
182
+ klass
183
+ end
184
+
185
+ def self.make_module(klass_name)
186
+ klass_name = klass_name.to_s
187
+ name_ok?(klass_name)
188
+ eval <<-EOF
189
+ module #{klass_name}
190
+ end
191
+ EOF
192
+ klass = eval(klass_name)
193
+ yield klass
194
+ klass
195
+ end
196
+
197
+ def self.name_ok?(name)
198
+ raise TypeError, " name #{name} can't be class or module name." unless name =~ /^[A-Z][a-zA-Z_0-9]*/
199
+ end
200
+ end
201
+
202
+ # Get DRbClient
203
+ class DRbClient
204
+ @client = nil
205
+ include WoolenCommon::ToolLogger
206
+ class <<self
207
+ def client
208
+ begin
209
+ stop_service if @client.nil? and Config.autostart
210
+ start_service if @client.nil? and Config.autostart
211
+ connect_addr = "druby://#{Config.ip}:#{Config.port}"
212
+ @client ||= DRbObject.new(nil, connect_addr)
213
+ rescue DRb::DRbConnError
214
+ raise RubyProxy::NotConnError, "can connect to druby server: #{connect_addr}"
215
+ end
216
+ end
217
+
218
+ def alive?
219
+ @client.respond_to?("any_thing")
220
+ true
221
+ rescue DRb::DRbConnError
222
+ false
223
+ end
224
+
225
+ attr_accessor :ip, :port
226
+
227
+ def proxy_load(file)
228
+ client.proxy_load(file)
229
+ end
230
+
231
+ def proxy_global_get(arg)
232
+ client.proxy_global_get(arg)
233
+ end
234
+
235
+ def proxy_global_set(arg, var)
236
+ client.proxy_global_set(arg, var)
237
+ end
238
+
239
+ def add_load_path(path)
240
+ #path = File.expand_path(path)
241
+ client.add_load_path(path)
242
+ end
243
+
244
+ # not use it later
245
+ def start_service(t=10)
246
+ message = nil
247
+ @service_log = nil
248
+ info 'start ruby proxy server...'
249
+ info start_command
250
+ #~ @server_thread = Thread.new do |t|
251
+ #~ t.abort_on_exception = true
252
+ @service_log = IO.popen(start_command)
253
+ #~ end
254
+ #~ @server_thread.abort_on_exception = true
255
+ wait_until_server_start_time(t)
256
+ do_at_exit if Config.autostart
257
+ end
258
+
259
+ def start_command
260
+ #raise RubyProxy::CommandNotFoundError, "ruby command can not be found: #{Config.command}" unless File.file?(Config.command)
261
+ server_file = File.expand_path File.join(File.dirname(__FILE__), 'server.rb')
262
+ Config.command + " " + server_file
263
+ end
264
+
265
+ def stop_service(t=5)
266
+ #TCPSocket.new(Config.ip,Config.port)
267
+ @client ||= DRbObject.new(nil, "druby://#{Config.ip}:#{Config.port}")
268
+ @client.stop_proxy
269
+ sleep 1
270
+ rescue
271
+ debug "service not start,stop fail!"
272
+ debug "#{$!}"
273
+ ensure
274
+ @client = nil
275
+ end
276
+
277
+ def wait_until_server_start_time(t)
278
+ t.times do |tt|
279
+ begin
280
+ #~ raise CannotStartServer, "" unless @server_thread.alive?
281
+ TCPSocket.new(Config.ip, Config.port)
282
+ info 'server is starting'
283
+ return true
284
+ rescue Exception
285
+ sleep 1
286
+ end
287
+ end
288
+ raise RuntimeError, 'start drbserver fail'
289
+ end
290
+
291
+ def do_at_exit
292
+ at_exit do
293
+ info 'try to stop service'
294
+ stop_service
295
+ end
296
+ end
297
+
298
+ end
299
+ end
300
+ end
301
+
302
+ if $0 == __FILE__
303
+ a = ATU::Hello.new
304
+ puts a.return_1
305
+ end