uae-common 0.0.1 → 0.0.2

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 (2) hide show
  1. data/lib/uae/common.rb +119 -111
  2. metadata +2 -2
data/lib/uae/common.rb CHANGED
@@ -1,111 +1,119 @@
1
- require 'fileutils'
2
- require 'socket'
3
-
4
- module UAE
5
-
6
- # 获取本机ip
7
- def self.local_ip(route)
8
- orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
9
- UDPSocket.open {|s| s.connect(route, 1); s.addr.last }
10
- ensure
11
- Socket.do_not_reverse_lookup = orig
12
- end
13
-
14
- # 获取一个uuid
15
- def self.secure_uuid
16
- result = File.open('/dev/urandom') { |x| x.read(16).unpack('H*')[0] }
17
- end
18
-
19
- # 获取一个随机端口
20
- def self.grab_ephemeral_port
21
- socket = TCPServer.new('0.0.0.0', 0)
22
- socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
23
- Socket.do_not_reverse_lookup = true
24
- port = socket.addr[1]
25
- socket.close
26
- return port
27
- end
28
-
29
- def self.process_running?(pid)
30
- return false unless pid && (pid > 0)
31
- output = %x[ps -o rss= -p #{pid}]
32
- return true if ($? == 0 && !output.empty?)
33
- # fail otherwise..
34
- return false
35
- end
36
-
37
- class PidFile
38
- class ProcessRunningError < StandardError
39
- end
40
-
41
- def initialize(pid_file, create_parents=true)
42
- @pid_file = pid_file
43
- @dirty = true
44
- write(create_parents)
45
- end
46
-
47
- # Removes the created pidfile
48
- def unlink()
49
- return unless @dirty
50
-
51
- # Swallowing exception here is fine. Removing the pid files is a courtesy.
52
- begin
53
- File.unlink(@pid_file)
54
- @dirty = false
55
- rescue
56
- end
57
- self
58
- end
59
-
60
- # Removes the created pidfile upon receipt of the supplied signals
61
- def unlink_on_signals(*sigs)
62
- return unless @dirty
63
-
64
- sigs.each do |s|
65
- Signal.trap(s) { unlink() }
66
- end
67
- self
68
- end
69
-
70
- def unlink_at_exit()
71
- at_exit { unlink() }
72
- self
73
- end
74
-
75
- def to_s()
76
- @pid_file
77
- end
78
-
79
- protected
80
-
81
- # Atomically writes the pidfile.
82
- # NB: This throws exceptions if the pidfile contains the pid of another running process.
83
- #
84
- # +create_parents+ If true, all parts of the path up to the file's dirname will be created.
85
- #
86
- def write(create_parents=true)
87
- FileUtils.mkdir_p(File.dirname(@pid_file)) if create_parents
88
-
89
- # Protip from Wilson: binary mode keeps things sane under Windows
90
- # Closing the fd releases our lock
91
- File.open(@pid_file, 'a+b', 0644) do |f|
92
- f.flock(File::LOCK_EX)
93
-
94
- # Check if process is already running
95
- pid = f.read().strip().to_i()
96
- if pid == Process.pid()
97
- return
98
- elsif UAE.process_running?(pid)
99
- raise ProcessRunningError.new("Process already running (pid=%d)." % (pid))
100
- end
101
-
102
- # We're good to go, write our pid
103
- f.truncate(0)
104
- f.rewind()
105
- f.write("%d\n" % (Process.pid()))
106
- f.flush()
107
- end
108
- end
109
- end # class PidFile
110
-
111
- end
1
+ require 'fileutils'
2
+ require 'socket'
3
+
4
+ module UAE
5
+
6
+ # 获取本机ip
7
+ def self.local_ip(route)
8
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
9
+ UDPSocket.open {|s| s.connect(route, 1); s.addr.last }
10
+ ensure
11
+ Socket.do_not_reverse_lookup = orig
12
+ end
13
+
14
+ # 获取一个uuid
15
+ def self.secure_uuid
16
+ result = File.open('/dev/urandom') { |x| x.read(16).unpack('H*')[0] }
17
+ end
18
+
19
+ # 获取一个随机端口
20
+ def self.grab_ephemeral_port(range = (40000..50000))
21
+ range.each do |i|
22
+ socket = nil
23
+ begin
24
+ socket = TCPServer.new('0.0.0.0', i)
25
+ socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
26
+ Socket.do_not_reverse_lookup = true
27
+ port = socket.addr[1]
28
+ socket.close
29
+ return port
30
+ rescue Exception => e
31
+ socket.close if socket
32
+ next
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.process_running?(pid)
38
+ return false unless pid && (pid > 0)
39
+ output = %x[ps -o rss= -p #{pid}]
40
+ return true if ($? == 0 && !output.empty?)
41
+ # fail otherwise..
42
+ return false
43
+ end
44
+
45
+ class PidFile
46
+ class ProcessRunningError < StandardError
47
+ end
48
+
49
+ def initialize(pid_file, create_parents=true)
50
+ @pid_file = pid_file
51
+ @dirty = true
52
+ write(create_parents)
53
+ end
54
+
55
+ # Removes the created pidfile
56
+ def unlink()
57
+ return unless @dirty
58
+
59
+ # Swallowing exception here is fine. Removing the pid files is a courtesy.
60
+ begin
61
+ File.unlink(@pid_file)
62
+ @dirty = false
63
+ rescue
64
+ end
65
+ self
66
+ end
67
+
68
+ # Removes the created pidfile upon receipt of the supplied signals
69
+ def unlink_on_signals(*sigs)
70
+ return unless @dirty
71
+
72
+ sigs.each do |s|
73
+ Signal.trap(s) { unlink() }
74
+ end
75
+ self
76
+ end
77
+
78
+ def unlink_at_exit()
79
+ at_exit { unlink() }
80
+ self
81
+ end
82
+
83
+ def to_s()
84
+ @pid_file
85
+ end
86
+
87
+ protected
88
+
89
+ # Atomically writes the pidfile.
90
+ # NB: This throws exceptions if the pidfile contains the pid of another running process.
91
+ #
92
+ # +create_parents+ If true, all parts of the path up to the file's dirname will be created.
93
+ #
94
+ def write(create_parents=true)
95
+ FileUtils.mkdir_p(File.dirname(@pid_file)) if create_parents
96
+
97
+ # Protip from Wilson: binary mode keeps things sane under Windows
98
+ # Closing the fd releases our lock
99
+ File.open(@pid_file, 'a+b', 0644) do |f|
100
+ f.flock(File::LOCK_EX)
101
+
102
+ # Check if process is already running
103
+ pid = f.read().strip().to_i()
104
+ if pid == Process.pid()
105
+ return
106
+ elsif UAE.process_running?(pid)
107
+ raise ProcessRunningError.new("Process already running (pid=%d)." % (pid))
108
+ end
109
+
110
+ # We're good to go, write our pid
111
+ f.truncate(0)
112
+ f.rewind()
113
+ f.write("%d\n" % (Process.pid()))
114
+ f.flush()
115
+ end
116
+ end
117
+ end # class PidFile
118
+
119
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uae-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-01-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: UAE common library
15
15
  email: