Skip to content

Commit e6ee29e

Browse files
author
Grant Bissett
committed
here you go meftos
0 parents  commit e6ee29e

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MAKE MONEY ONLINE IN YOU'RE

monitor_file.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/ruby -w
2+
require 'inotify'
3+
require 'socket'
4+
include Socket::Constants
5+
6+
# Setup a server
7+
def setup_server(ip, port)
8+
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
9+
sockaddr = Socket.pack_sockaddr_in(port, ip)
10+
puts "Waiting for a connection on " + sockaddr.inspect
11+
socket.bind(sockaddr)
12+
socket.listen(5)
13+
client, client_sockaddr = socket.accept
14+
15+
# Let's wait for someone to connect
16+
read = ''
17+
while data = client.recvfrom(1)
18+
read += data.join
19+
if read == 'START'
20+
client.puts "ACK"
21+
22+
get_forked(client)
23+
read = ''
24+
end
25+
end
26+
end
27+
28+
def get_forked(client)
29+
setup_listener(client)
30+
end
31+
32+
def setup_listener(client)
33+
file = ARGV.first
34+
35+
mon = Inotify.new
36+
mon.add_watch(file, Inotify::MODIFY)
37+
puts client.inspect
38+
mon.each_event do |event|
39+
client.puts "$$$"
40+
end
41+
end
42+
43+
setup_server('0.0.0.0', 1337)

prefork_monitor_file.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/ruby
2+
require 'socket'
3+
require 'inotify'
4+
5+
class Preforker
6+
attr_reader (:child_count)
7+
8+
def initialize(prefork, max_clients_per_child, port, client_handler)
9+
@prefork = prefork
10+
@max_clients_per_child = max_clients_per_child
11+
@port = port
12+
@child_count = 0
13+
14+
@reaper = proc {
15+
trap('CHLD', @reaper)
16+
pid = Process.wait
17+
@child_count -= 1
18+
}
19+
20+
@huntsman = proc {
21+
trap('CHLD', 'IGNORE')
22+
trap('INT', 'IGNORE')
23+
Process.kill('INT', 0)
24+
exit
25+
}
26+
27+
@client_handler=client_handler
28+
end
29+
30+
def child_handler
31+
trap('INT', 'EXIT')
32+
@client_handler.setUp
33+
# wish: sigprocmask UNblock SIGINT
34+
@max_clients_per_child.times {
35+
client = @server.accept or break
36+
@client_handler.handle_request(client)
37+
client.close
38+
}
39+
@client_handler.tearDown
40+
end
41+
42+
def make_new_child
43+
# wish: sigprocmask block SIGINT
44+
@child_count += 1
45+
pid = fork do
46+
child_handler
47+
end
48+
# wish: sigprocmask UNblock SIGINT
49+
end
50+
51+
def run
52+
@server = TCPserver.open(@port)
53+
trap('CHLD', @reaper)
54+
trap('INT', @huntsman)
55+
loop {
56+
(@prefork - @child_count).times { |i|
57+
make_new_child
58+
}
59+
sleep 0.1
60+
}
61+
end
62+
end
63+
64+
class ClientHandler
65+
@monitor
66+
def setUp
67+
file = ARGV.first
68+
@monitor = Inotify.new
69+
@monitor.add_watch(file, Inotify::MODIFY)
70+
end
71+
72+
def tearDown
73+
@monitor.close
74+
end
75+
76+
def handle_request(client)
77+
@monitor.each_event do |event|
78+
client.puts "$$$"
79+
end
80+
end
81+
end
82+
83+
server = Preforker.new(5, 100, 1337, ClientHandler.new)
84+
server.run

0 commit comments

Comments
 (0)