Class: Volt::MessageBus::PeerConnection
- Defined in:
- lib/volt/server/message_bus/peer_to_peer/peer_connection.rb
Constant Summary collapse
- CONNECT_TIMEOUT =
2
Instance Attribute Summary collapse
-
#peer_server_id ⇒ Object
readonly
The server id for the connected server.
-
#socket ⇒ Object
readonly
The server id for the connected server.
Class Method Summary collapse
-
.connect_to(message_bus, ips, port) ⇒ Object
Because servers can have many ips, we try the various ip’s until we are able to connect to one.
Instance Method Summary collapse
-
#disconnect ⇒ Object
Close the socket, kill worker threads, and remove from message_bus’s peer_connections.
-
#initialize(socket, ip, port, message_bus, server = false) ⇒ PeerConnection
constructor
A new instance of PeerConnection.
- #listen ⇒ Object
- #publish(message) ⇒ Object
- #run_worker ⇒ Object
Constructor Details
#initialize(socket, ip, port, message_bus, server = false) ⇒ PeerConnection
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 17 def initialize(socket, ip, port, , server=false) = @ip = ip @port = port @server = server @socket = socket @server_id = .server_id = SizedQueue.new(500) @reconnect_mutex = Mutex.new # The encoder handles things like formatting and encryption = MessageEncoder.new failed = false begin if server # Wait for announcement @peer_server_id = .(@socket) .(@socket, @server_id) else # Announce .(@socket, @server_id) @peer_server_id = .(@socket) end rescue IOError => e failed = true end # Make sure we aren't already connected .remove_duplicate_connections # Don't connect to self if @failed || @peer_server_id == @server_id # Close the connection close return end @listen_thread = Thread.new do # Listen for messages in a new thread listen end @worker_thread = Thread.new do run_worker end end |
Instance Attribute Details
#peer_server_id ⇒ Object (readonly)
The server id for the connected server
15 16 17 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 15 def peer_server_id @peer_server_id end |
#socket ⇒ Object (readonly)
The server id for the connected server
15 16 17 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 15 def socket @socket end |
Class Method Details
.connect_to(message_bus, ips, port) ⇒ Object
Because servers can have many ips, we try the various ip’s until we are able to connect to one.
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 136 def self.connect_to(, ips, port) ips.split(',').each do |ip| begin socket = SocketWithTimeout.new(ip, port, CONNECT_TIMEOUT) return PeerConnection.new(socket, ip, port, ) rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e # Unable to connect, next next end end return false end |
Instance Method Details
#disconnect ⇒ Object
Close the socket, kill worker threads, and remove from message_bus’s peer_connections
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 68 def disconnect @disconnected = true .push(:QUIT) begin @socket.close rescue => e # Ignore close error, since we may not be connected end @listen_thread.kill @worker_thread.kill .remove_peer_connection(self) end |
#listen ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 106 def listen loop do begin while ( = .(@socket)) # puts "Message: #{message.inspect}" break if @disconnected .() end # Got nil from socket rescue Errno::ECONNRESET, Errno::EPIPE, IOError => e # handle below end if !@disconnected && !@server # Connection was dropped, try to reconnect connected = reconnect! # Couldn't reconnect, die break unless connected else break end end end |
#publish(message) ⇒ Object
83 84 85 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 83 def publish() .push() end |
#run_worker ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/volt/server/message_bus/peer_to_peer/peer_connection.rb', line 87 def run_worker while ( = .pop) break if == :QUIT begin .(@socket, ) # 'Error: closed stream' comes in sometimes rescue Errno::ECONNREFUSED, Errno::EPIPE, IOError => e # was also rescuing Error if reconnect! retry else # Unable to reconnect, die break end end end end |