uae-common 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/uae/common.rb +119 -111
- 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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
f.
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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.
|
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-
|
12
|
+
date: 2013-01-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: UAE common library
|
15
15
|
email:
|