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,110 @@
1
+ Splib.load :Monitor
2
+
3
+ module Splib
4
+ # Exception raised when queue is empty
5
+ class EmptyQueue < RuntimeError
6
+ end
7
+ # This class provides some simple logic for item output. It is
8
+ # basically a priority based queue with some round robin thrown
9
+ # in to keep things interesting. This queue provides an easy way
10
+ # for many threads to populate it without drowning out each other.
11
+ # NOTE: Design help from the great Ryan "pizza_" Flynn
12
+ class PriorityQueue
13
+
14
+ # args:: config arguments
15
+ # :raise_on_empty
16
+ # whocares:: lambda{|target| true||false}
17
+ # Create a priority queue
18
+ def initialize(*args, &whocares)
19
+ @raise = args.include?(:raise_on_empty)
20
+ @whocares = whocares
21
+ @target_queues = {}
22
+ @queues = {:PRIORITY => [], :NEW => [], :NORMAL => [], :WHOCARES => []}
23
+ @lock = Splib::Monitor.new
24
+ end
25
+
26
+ # target:: target queue
27
+ # item:: item to queue
28
+ # This prioritizes output to help reduce lag when lots of output
29
+ # is being sent to another target. This will automatically decide
30
+ # how to queue the item based on the target
31
+ def prioritized_queue(target, item)
32
+ raise NameError.new('The target :internal_prio is a restricted target') if target == :internal_prio
33
+ @lock.synchronize do
34
+ @target_queues[target] = [] unless @target_queues[target]
35
+ if(@whocares && @whocares.call(target))
36
+ @target_queues[target] << item
37
+ add_queue(:WHOCARES, @target_queues[target])
38
+ else
39
+ @target_queues[target] << item
40
+ if(@target_queues[target].size < 2)
41
+ add_queue(:NEW, @target_queues[target])
42
+ else
43
+ add_queue(:NORMAL, @target_queues[target])
44
+ end
45
+ end
46
+ @lock.signal
47
+ end
48
+ item
49
+ end
50
+
51
+ # item:: item to queue
52
+ # This will add item to the PRIORITY queue which gets
53
+ # sent before all other items.
54
+ def direct_queue(message)
55
+ @lock.synchronize do
56
+ @target_queues[:internal_prio] = [] unless @target_queues[:internal_prio]
57
+ @target_queues[:internal_prio] << message
58
+ add_queue(:PRIORITY, @target_queues[:internal_prio])
59
+ @lock.signal
60
+ end
61
+ message
62
+ end
63
+
64
+ # raise_e:: raise an exception on empty
65
+ # Returns the next message to send. This method decides what
66
+ # message to send based on the priority of the message. It
67
+ # will throw an Exceptions::EmptyQueue when there are no messages
68
+ # left.
69
+ def pop
70
+ m = nil
71
+ @lock.synchronize do
72
+ [:PRIORITY, :NEW, :NORMAL, :WHOCARES].each do |k|
73
+ unless(@queues[k].empty?)
74
+ q = @queues[k].shift
75
+ unless(q.empty?)
76
+ m = q.shift
77
+ add_queue(k, q) unless(q.empty?)
78
+ break
79
+ end
80
+ end
81
+ end
82
+ end
83
+ unless(m)
84
+ if(@raise)
85
+ raise EmptyQueue.new('Queue is currently empty')
86
+ else
87
+ @lock.wait_while{ empty? }
88
+ m = pop
89
+ end
90
+ end
91
+ m
92
+ end
93
+
94
+ # Returns true if queue is empty
95
+ def empty?
96
+ @lock.synchronize{@target_queues.values.find{|n|!n.empty?}.nil?}
97
+ end
98
+
99
+ alias :push :prioritized_queue
100
+ alias :<< :direct_queue
101
+
102
+ private
103
+
104
+ def add_queue(name, queue)
105
+ unless(@queues[name].include?(queue))
106
+ @queues[name] << queue
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,10 @@
1
+ module Splib
2
+ class << self
3
+ # secs:: Number of seconds to sleep (Use float to provide better actual sleep time)
4
+ def sleep(secs=nil)
5
+ start = Time.now.to_f
6
+ secs.nil? ? Kernel.sleep : Kernel.sleep(secs)
7
+ Time.now.to_f - start
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,48 @@
1
+ require 'net/http'
2
+
3
+ module Splib
4
+ # url:: URL to shorten
5
+ # Gets a tinyurl for given URL
6
+ def self.tiny_url(url)
7
+ connection = Net::HTTP.new('tinyurl.com', 80)
8
+ resp, data = connection.get("/api-create.php?url=#{url}")
9
+ if(resp.code !~ /^200$/)
10
+ raise "Failed to make the URL small."
11
+ end
12
+ return data.strip
13
+ end
14
+ # url:: URL to shorten
15
+ # Gets a tr.im for given URL
16
+ def self.trim_url(url)
17
+ connection = Net::HTTP.new('api.tr.im', 80)
18
+ resp, data = connection.get("/v1/trim_simple?url=#{url}")
19
+ if(resp.code !~ /^200$/)
20
+ raise "Failed to make the URL small."
21
+ end
22
+ return data.strip
23
+ end
24
+ # url:: URL to shorten
25
+ # Gets a is.gd for given URL
26
+ def self.isgd_url(url)
27
+ connection = Net::HTTP.new('is.gd', 80)
28
+ resp, data = connection.get("/api.php?longurl=#{url}")
29
+ if(resp.code !~ /^200$/)
30
+ raise "Failed to make the URL small."
31
+ end
32
+ return data.strip
33
+ end
34
+ # url:: URL to shorten
35
+ # Get shortest for given url
36
+ def self.shortest_url(url)
37
+ results = []
38
+ [:tiny_url, :isgd_url, :trim_url].each do |service|
39
+ begin
40
+ results << self.send(service, url)
41
+ rescue
42
+ #ignore#
43
+ end
44
+ end
45
+ raise 'Failed to make URL small' if results.empty?
46
+ results.sort{|a,b| a.length <=> b.length}[0]
47
+ end
48
+ end
@@ -0,0 +1,146 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'net/ssh'
3
+ require "#{File.join(File.dirname(__FILE__), 'logger')}"
4
+ module WoolenCommon
5
+ class SshProxy
6
+ include ToolLogger
7
+ class << self
8
+ attr_accessor :the_ssh_instances
9
+ def get_ssh_proxy(ip,port,user,passwd)
10
+ options = {:port => port,:password => passwd}
11
+ @the_ssh_instances ||= {}
12
+ @the_ssh_instances[ip] ||= {}
13
+ @the_ssh_instances[ip][port] ||= {}
14
+ @the_ssh_instances[ip][port][user] ||= {}
15
+ @the_ssh_instances[ip][port][user][passwd] ||= SshProxy.new(ip, user, options)
16
+ @the_ssh_instances[ip][port][user][passwd]
17
+ end
18
+ end
19
+
20
+ def initialize(host, user, options={})
21
+ @host = host
22
+ @user = user
23
+ @options = options
24
+ @conn_retry = options[:proxy_conn_retry] || 5
25
+ options.delete :proxy_conn_retry if options[:proxy_conn_retry]
26
+ # 超时时间设置30秒太长了,不是很合理,实际上5秒没有回复,那就是出问题了
27
+ @conn_timeout = options[:proxy_conn_timeout] || 5
28
+ options.delete :proxy_conn_timeout if options[:proxy_conn_timeout]
29
+ proxy_reset_conn
30
+ end
31
+
32
+ def proxy_reset_conn
33
+ @conn_retry.times do
34
+ begin
35
+ Timeout.timeout(@conn_timeout) do
36
+ @ssh_conn = Net::SSH.start(@host, @user, @options)
37
+ if check_connector_close
38
+ debug 'reconnect ssh ok'
39
+ return
40
+ end
41
+ end
42
+ rescue Exception => e
43
+ error "连接ssh服务器出错~!信息是:#{e.message},用户信息:@host:#{@host},@user:#{@user},@options:#{@options}"
44
+ end
45
+ end
46
+ end
47
+
48
+ def method_missing(name, *args, &block)
49
+ if check_connector_close
50
+ @ssh_conn.close rescue nil
51
+ proxy_reset_conn
52
+ end
53
+ #debug "SshProxy need to invoke methdo ::#{name} "
54
+ #debug "params::#{args}"
55
+ Timeout.timeout(@conn_timeout) do
56
+ return_result = ''
57
+ if @ssh_conn
58
+ return_result = @ssh_conn.send(name, *args, &block)
59
+ #debug "SshProxy invoke result ::#{return_result}"
60
+ else
61
+ error 'ssh链接建立不起来!'
62
+ end
63
+ return return_result
64
+ end
65
+ end
66
+
67
+ def exec!(command, &block)
68
+ if check_connector_close
69
+ @ssh_conn.close rescue nil
70
+ proxy_reset_conn
71
+ end
72
+ Timeout.timeout(@conn_timeout) do
73
+ return @ssh_conn.exec!(command.unpack('C*').pack('C*'), &block)
74
+ end
75
+ end
76
+
77
+ def exec(command, &block)
78
+ if check_connector_close
79
+ @ssh_conn.close rescue nil
80
+ proxy_reset_conn
81
+ end
82
+ Timeout.timeout(@conn_timeout) do
83
+ return @ssh_conn.exec(command.unpack('C*').pack('C*'), &block)
84
+ end
85
+ end
86
+
87
+ def check_connector_close
88
+ begin
89
+ if @ssh_conn.nil? or @ssh_conn.closed?
90
+ return true
91
+ end
92
+ Timeout.timeout(@conn_timeout) do
93
+ if @ssh_conn.exec!('echo hello').include? 'hello'
94
+ return false
95
+ end
96
+ end
97
+ rescue Exception => e
98
+ error "检查连接出错,错误信息是::#{e.message}"
99
+ return true
100
+ end
101
+ true
102
+ end
103
+
104
+ # 阻塞性下载
105
+ def sftp_download!(remote_path, local_path)
106
+ if check_connector_close
107
+ @ssh_conn.close rescue nil
108
+ proxy_reset_conn
109
+ end
110
+ @ssh_conn.sftp.connect! do |sftp_session|
111
+ return sftp_session.download!(remote_path, local_path)
112
+ end
113
+ end
114
+
115
+ # 非塞性下载
116
+ def sftp_download(remote_path, local_path)
117
+ if check_connector_close
118
+ @ssh_conn.close rescue nil
119
+ proxy_reset_conn
120
+ end
121
+ @ssh_conn.sftp.connect do |sftp_session|
122
+ return sftp_session.download!(remote_path, local_path)
123
+ end
124
+ end
125
+
126
+ def sftp_upload!(remote_path, local_path)
127
+ if check_connector_close
128
+ @ssh_conn.close rescue nil
129
+ proxy_reset_conn
130
+ end
131
+ @ssh_conn.sftp.connect! do |sftp_session|
132
+ return sftp_session.upload!(local_path,remote_path)
133
+ end
134
+ end
135
+
136
+ def sftp_upload(remote_path, local_path)
137
+ if check_connector_close
138
+ @ssh_conn.close rescue nil
139
+ proxy_reset_conn
140
+ end
141
+ @ssh_conn.sftp.connect do |sftp_session|
142
+ return sftp_session.upload(local_path,remote_path)
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,123 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module WoolenCommon
3
+ module SystemHelper
4
+ IPV4_REGEX = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
5
+
6
+ def ruby18?
7
+ RUBY_VERSION =~ /^1.8/ ? true : false
8
+ end
9
+
10
+ module_function :ruby18?
11
+
12
+ def ruby19?
13
+ RUBY_VERSION =~ /^1.9/ ? true : false
14
+ end
15
+
16
+ module_function :ruby19?
17
+
18
+ def platform
19
+ case RUBY_PLATFORM
20
+ when /w32/, /mswin32/
21
+ "windows"
22
+ when /linux/
23
+ "linux"
24
+ else
25
+ "mac"
26
+ end
27
+ end
28
+
29
+ module_function :platform
30
+
31
+ def windows?
32
+ case RUBY_PLATFORM
33
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
34
+ return true
35
+ when /java/
36
+ begin
37
+ require 'Win32API'
38
+ return true
39
+ rescue Exception
40
+ return false
41
+ end
42
+ else
43
+ return false
44
+ end
45
+ end
46
+
47
+ module_function :windows?
48
+
49
+ def is_x64?
50
+ case RUBY_PLATFORM
51
+ when /x86_64/
52
+ return true
53
+ else
54
+ return false
55
+ end
56
+ end
57
+
58
+ module_function :is_x64?
59
+
60
+
61
+ def get_local_ip_addrs
62
+ result_array = []
63
+ if windows?
64
+ ip_addr_arrays = TCPSocket.gethostbyname(Socket.gethostname)
65
+ ip_addr_arrays.each do |one_addr|
66
+ if one_addr.is_a? String
67
+ if one_addr =~ IPV4_REGEX
68
+ result_array << one_addr
69
+ end
70
+ end
71
+ end
72
+ else
73
+ output = %x{ip addr list}
74
+ output.split(/\n/).each { |str|
75
+ if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
76
+ tmp = $1
77
+ unless tmp =~ /127\./
78
+ result_array << tmp
79
+ end
80
+ end
81
+ }
82
+ end
83
+ result_array
84
+ end
85
+
86
+ module_function :get_local_ip_addrs
87
+
88
+
89
+ def get_same_subnet_ip(check_ip)
90
+ same_count = 0
91
+ ip_size = 32
92
+ ctrl_ip_num = IPAddr.new(check_ip).to_i
93
+ result_ip = ''
94
+ get_local_ip_addrs.each do |one_ip|
95
+ cnt = 0
96
+ one_ip_num = IPAddr.new(one_ip).to_i
97
+ ip_size.times do |count|
98
+ mask = 1 << (ip_size - count)
99
+ if ctrl_ip_num & mask == one_ip_num & mask
100
+ cnt += 1
101
+ next if cnt < ip_size
102
+ end
103
+ if cnt > same_count
104
+ result_ip = one_ip
105
+ same_count = cnt
106
+ end
107
+ break
108
+ end
109
+ end
110
+ result_ip
111
+ end
112
+ module_function :get_same_subnet_ip
113
+
114
+ def get_platform_path(*args, &block)
115
+ real_path = CommonHelper.get_real_path(*args, &block)
116
+ if windows?
117
+ real_path.gsub!('/','\\')
118
+ end
119
+ real_path
120
+ end
121
+ module_function :get_platform_path
122
+ end
123
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "#{File.join(File.dirname(__FILE__), 'logger')}"
3
+ require "#{File.join(File.dirname(__FILE__), 'system_helper')}"
4
+ module WoolenCommon
5
+ MONITOR_DEFAULT_CFG={
6
+ 'net_if_ids' => [0]
7
+ }
8
+ class SystemMonitor
9
+ if SystemHelper.windows?
10
+ require "#{File.join(File.dirname(__FILE__), 'system_monitor', 'windows_monitor')}"
11
+ include WindowsMonitor
12
+ else
13
+ require "#{File.join(File.dirname(__FILE__), 'system_monitor', 'linux_monitor')}"
14
+ include LinuxMonitor
15
+ end
16
+
17
+ class << self
18
+ def run_monitor(monitor_cfg=MONITOR_DEFAULT_CFG)
19
+ get_common_performance monitor_cfg
20
+ end
21
+ end
22
+ end
23
+ end