Class: Pandemic::ServerSide::Peer
- Inherits:
-
Object
- Object
- Pandemic::ServerSide::Peer
- Includes:
- Util
- Defined in:
- lib/pandemic/server_side/peer.rb
Defined Under Namespace
Classes: PeerUnavailableException
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #add_incoming_connection(conn) ⇒ Object
- #client_request(request, body) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
-
#initialize(addr, server) ⇒ Peer
constructor
A new instance of Peer.
Methods included from Util
#host_port, #logger, #with_mutex
Constructor Details
#initialize(addr, server) ⇒ Peer
Returns a new instance of Peer.
8 9 10 11 12 13 14 15 |
# File 'lib/pandemic/server_side/peer.rb', line 8 def initialize(addr, server) @host, @port = host_port(addr) @server = server @pending_requests = with_mutex({}) @incoming_connection_listeners = [] @inc_threads_mutex = Mutex.new initialize_connection_pool end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/pandemic/server_side/peer.rb', line 6 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
6 7 8 |
# File 'lib/pandemic/server_side/peer.rb', line 6 def port @port end |
Instance Method Details
#add_incoming_connection(conn) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pandemic/server_side/peer.rb', line 60 def add_incoming_connection(conn) # debug("Adding incoming connection") connect # if we're not connected, we should be thread = Thread.new(conn) do |connection| begin # debug("Incoming connection thread started") while @server.running # debug("Listening for incoming requests") request = connection.read(15) # debug("Read incoming request from peer") if request.nil? # debug("Incoming connection request is nil") break else # debug("Received incoming (#{request.strip})") handle_incoming_request(request, connection) if request =~ /^P/ handle_incoming_response(request, connection) if request =~ /^R/ end end rescue Exception => e warn("Unhandled exception in peer listener thread:\n#{e.inspect}\n#{e.backtrace.join("\n")}") ensure # debug("Incoming connection closing") conn.close if conn && !conn.closed? @inc_threads_mutex.synchronize { @incoming_connection_listeners.delete(Thread.current)} if @incoming_connection_listeners.empty? disconnect end end end @inc_threads_mutex.synchronize {@incoming_connection_listeners.push(thread) if thread.alive? } end |
#client_request(request, body) ⇒ Object
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 |
# File 'lib/pandemic/server_side/peer.rb', line 31 def client_request(request, body) # debug("Sending client's request to peer") # debug("Connection pool has #{@connection_pool.available_count} of #{@connection_pool.connections_count} connections available") successful = true @pending_requests.synchronize do @pending_requests[request.hash] = request end begin @connection_pool.with_connection do |connection| if connection && !connection.closed? # debug("Writing client's request") connection.write("P#{request.hash}#{[body.size].pack('N')}#{body}") connection.flush # debug("Finished writing client's request") else successful = false end end rescue Exception @pending_requests.synchronize { @pending_requests.delete(request.hash) } raise else if !successful @pending_requests.synchronize { @pending_requests.delete(request.hash) } end end end |
#connect ⇒ Object
17 18 19 20 |
# File 'lib/pandemic/server_side/peer.rb', line 17 def connect # debug("Forced connection to peer") @connection_pool.connect end |
#connected? ⇒ Boolean
27 28 29 |
# File 'lib/pandemic/server_side/peer.rb', line 27 def connected? @connection_pool.connected? end |
#disconnect ⇒ Object
22 23 24 25 |
# File 'lib/pandemic/server_side/peer.rb', line 22 def disconnect # debug("Disconnecting from peer") @connection_pool.disconnect end |