Class: LocalSocket
- Inherits:
-
Object
- Object
- LocalSocket
- Defined in:
- lib/local_socket.rb
Constant Summary collapse
- CONNECTION_TEST =
"connection_test"
Instance Attribute Summary collapse
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
Instance Method Summary collapse
- #bind ⇒ Object
- #connection_changed ⇒ Object
-
#initialize(receive_channel, send_channel, max_message_size = 5000, thread_sleep = 0.1, socket_timeout = 60) ⇒ LocalSocket
constructor
A new instance of LocalSocket.
- #join ⇒ Object
- #message_received ⇒ Object
- #send_msg(msg) ⇒ Object
Constructor Details
#initialize(receive_channel, send_channel, max_message_size = 5000, thread_sleep = 0.1, socket_timeout = 60) ⇒ LocalSocket
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/local_socket.rb', line 7 def initialize(receive_channel, send_channel, =5000, thread_sleep=0.1, socket_timeout=60) @receive_channel = receive_channel @send_channel = send_channel = @thread_sleep = thread_sleep @last_connected_status = nil @connected = false @receiver_thread = nil @connection_thread = nil bind end |
Instance Attribute Details
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
4 5 6 |
# File 'lib/local_socket.rb', line 4 def connected @connected end |
Instance Method Details
#bind ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/local_socket.rb', line 31 def bind begin File.unlink @receive_channel rescue => e puts "#{e}" end @socket = Socket.new(:UNIX, :DGRAM, 0) @snd_addrInfo = Addrinfo.unix(@send_channel) @rcv_addrInfo = Addrinfo.unix(@receive_channel) @socket.bind(@rcv_addrInfo) end |
#connection_changed ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/local_socket.rb', line 64 def connection_changed @connection_thread = Thread.new do loop do if send_msg(CONNECTION_TEST) @connected = true else @connected = false end if @last_connected_status != @connected yield @connected end @last_connected_status = @connected sleep @thread_sleep end end end |
#join ⇒ Object
43 44 45 |
# File 'lib/local_socket.rb', line 43 def join @receiver_thread.join end |
#message_received ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/local_socket.rb', line 48 def @receiver_thread = Thread.new do loop do begin result = @socket.recv_nonblock() if result != CONNECTION_TEST yield result end rescue => e puts "#{e}" unless "#{e}".include? "would block" end sleep @thread_sleep end end end |
#send_msg(msg) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/local_socket.rb', line 19 def send_msg(msg) flag = false begin @socket.sendmsg_nonblock(msg, 0, @snd_addrInfo) flag = true rescue => e puts "Error: #{e}" unless "#{e}".include? "Connection refused" flag = false end return flag end |