-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.rb
40 lines (37 loc) · 1.08 KB
/
mirror.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env ruby
require 'socket'
class Mirror
def initialize(hosts)
@connections=Array.new
hosts.each do |host|
hostparams=host.split(':')
connection={ :host => hostparams[0], :port => hostparams[1] }
connect(connection)
@connections<<connection
end
end
def connect(connection)
begin
@@log.info "Connecting to "+connection[:host]+" "+connection[:port].to_s
connection.store(:socket,TCPSocket.open(connection[:host],connection[:port]))
rescue
@@log.error ("Coudn't connect to "+connection[:host]+":"+connection[:port].to_s)
return false
end
@@log.info "Connected"
return true
end
def send_data( data )
@connections.each do |connection|
begin
connection[:socket].puts(data)
rescue
@@log.error "Socket closed to "+connection[:host]+" "+connection[:port].to_s
if connect(connection)
connection[:socket].puts(data)
@@log.info "Reconnected to "+connection[:host]+" "+connection[:port].to_s
end
end
end
end
end