Class: SimpleRPC::LocalSocket
- Inherits:
-
Object
- Object
- SimpleRPC::LocalSocket
- Defined in:
- lib/local_socket.rb
Constant Summary collapse
- CONNECTION_TEST =
"connection_test"
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #add_listener(event, listener) ⇒ Object
- #bind ⇒ Object
- #connect ⇒ Object
-
#initialize(receive_channel, send_channel, max_message_size = 5000, thread_sleep = 0.1) ⇒ LocalSocket
constructor
A new instance of LocalSocket.
- #notify(event, data) ⇒ Object
- #send_msg(msg) ⇒ Object
- #setup_receiver_thread ⇒ Object
- #setup_status_thread ⇒ Object
Constructor Details
#initialize(receive_channel, send_channel, max_message_size = 5000, thread_sleep = 0.1) ⇒ LocalSocket
Returns a new instance of LocalSocket.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/local_socket.rb', line 13 def initialize(receive_channel, send_channel, =5000, thread_sleep=0.1) @receive_channel = receive_channel @send_channel = send_channel = @thread_sleep = thread_sleep @last_status = nil @status = false @receiver_thread = nil @_thread = nil @listeners = {} end |
Instance Attribute Details
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/local_socket.rb', line 9 def status @status end |
Instance Method Details
#add_listener(event, listener) ⇒ Object
89 90 91 92 |
# File 'lib/local_socket.rb', line 89 def add_listener(event, listener) @listeners[event] ||= Set.new @listeners[event] << listener end |
#bind ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/local_socket.rb', line 43 def bind begin File.unlink @receive_channel rescue => e puts "#{e}" unless "#{e}".include? "No such file" end @socket = Socket.new(:UNIX, :DGRAM, 0) @snd_addrInfo = Addrinfo.unix(@send_channel) @rcv_addrInfo = Addrinfo.unix(@receive_channel) @socket.bind(@rcv_addrInfo) end |
#connect ⇒ Object
37 38 39 40 41 |
# File 'lib/local_socket.rb', line 37 def connect bind setup_receiver_thread setup_status_thread end |
#notify(event, data) ⇒ Object
94 95 96 97 98 |
# File 'lib/local_socket.rb', line 94 def notify(event, data) @listeners[event] && @listeners[event].each do |listener| listener.call data end end |
#send_msg(msg) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/local_socket.rb', line 25 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" or "#{e}".include? "No such file" flag = false end return flag end |
#setup_receiver_thread ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/local_socket.rb', line 55 def setup_receiver_thread @receiver_thread = Thread.new do loop do begin result = @socket.recv_nonblock() if result != CONNECTION_TEST notify(Events::MESSAGE_RECEIVED, result) end rescue => e puts "#{e}" unless "#{e}".include? "would block" end sleep @thread_sleep end end end |
#setup_status_thread ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/local_socket.rb', line 71 def setup_status_thread @status_thread = Thread.new do # todo:clintmod implement timeout loop do if send_msg(CONNECTION_TEST) @status = Status::CONNECTED else @status = Status::DISCONNECTED end if @last_status != @status notify(Events::CONNECTION_CHANGED, @status) end @last_status = @status sleep @thread_sleep end end end |