Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

changed peer communication to be more concise

  • Loading branch information...
commit eb5ea8374edff0fe3df4202fdd5e4ac33895ba8f 1 parent 2693379
@arya authored
Showing with 35 additions and 47 deletions.
  1. +34 −46 lib/pandemic/server_side/peer.rb
  2. +1 −1  test/peer_test.rb
View
80 lib/pandemic/server_side/peer.rb
@@ -40,7 +40,7 @@ def client_request(request, body)
@connection_pool.with_connection do |connection|
if connection && !connection.closed?
# debug("Writing client's request")
- connection.write("PROCESS #{request.hash} #{body.size}\n#{body}")
+ connection.write("P#{request.hash}#{[body.size].pack('N')}#{body}")
connection.flush
# debug("Finished writing client's request")
else
@@ -68,7 +68,7 @@ def add_incoming_connection(conn)
# debug("Incoming connection thread started")
while @server.running
# debug("Listening for incoming requests")
- request = connection.gets
+ request = connection.read(15)
# debug("Read incoming request from peer")
if request.nil?
@@ -76,8 +76,8 @@ def add_incoming_connection(conn)
break
else
# debug("Received incoming (#{request.strip})")
- handle_incoming_request(request, connection) if request =~ /^PROCESS/
- handle_incoming_response(request, connection) if request =~ /^RESPONSE/
+ handle_incoming_request(request, connection) if request =~ /^P/
+ handle_incoming_response(request, connection) if request =~ /^R/
end
end
rescue Exception => e
@@ -127,52 +127,40 @@ def initialize_connection_pool
def handle_incoming_request(request, connection)
# debug("Identified as request")
- if request.strip =~ /^PROCESS ([A-Za-z0-9]+) ([0-9]+)$/
- hash = $1
- size = $2.to_i
- # debug("Incoming request: #{hash} #{size}")
- begin
- # debug("Reading request body")
- request_body = connection.read(size)
- # debug("Finished reading request body")
- rescue EOFError, TruncatedDataError
- # debug("Failed to read request body")
- # TODO: what to do here?
- return false
- rescue Exception => e
- warn("Unhandled exception in incoming request read:\n#{e.inspect}\n#{e.backtrace.join("\n")}")
- end
- # debug("Processing body")
- process_request(hash, request_body)
- else
- warn("Malformed incoming request: #{request.strip}")
- # when the incoming request was malformed
- # TODO: what to do here?
+ hash = request[1,10]
+ size = request[11, 4].unpack('N').first
+ # debug("Incoming request: #{hash} #{size}")
+ begin
+ # debug("Reading request body")
+ request_body = connection.read(size)
+ # debug("Finished reading request body")
+ rescue EOFError, TruncatedDataError
+ # debug("Failed to read request body")
+ # TODO: what to do here?
+ return false
+ rescue Exception => e
+ warn("Unhandled exception in incoming request read:\n#{e.inspect}\n#{e.backtrace.join("\n")}")
end
+ # debug("Processing body")
+ process_request(hash, request_body)
end
def handle_incoming_response(response, connection)
- if response.strip =~ /^RESPONSE ([A-Za-z0-9]+) ([0-9]+)$/
- hash = $1
- size = $2.to_i
- # debug("Incoming response: #{hash} #{size}")
- begin
- # debug("Reading response body")
- response_body = connection.read(size)
- # debug("Finished reading response body")
- rescue EOFError, TruncatedDataError
- # debug("Failed to read response body")
- # TODO: what to do here?
- return false
- rescue Exception => e
- warn("Unhandled exception in incoming response read:\n#{e.inspect}\n#{e.backtrace.join("\n")}")
- end
- process_response(hash, response_body)
- else
- warn("Malformed incoming response: #{response.strip}")
- # when the incoming response was malformed
- # TODO: what to do here?
+ hash = response[1,10]
+ size = response[11, 4].unpack('N').first
+ # debug("Incoming response: #{hash} #{size}")
+ begin
+ # debug("Reading response body")
+ response_body = connection.read(size)
+ # debug("Finished reading response body")
+ rescue EOFError, TruncatedDataError
+ # debug("Failed to read response body")
+ # TODO: what to do here?
+ return false
+ rescue Exception => e
+ warn("Unhandled exception in incoming response read:\n#{e.inspect}\n#{e.backtrace.join("\n")}")
end
+ process_response(hash, response_body)
end
@@ -184,7 +172,7 @@ def process_request(hash, body)
# debug("Processing finished (#{hash})")
@connection_pool.with_connection do |connection|
# debug( "Sending response (#{hash})")
- connection.write("RESPONSE #{hash} #{response.size}\n#{response}")
+ connection.write("R#{hash}#{[response.size].pack('N')}#{response}")
connection.flush
# debug( "Finished sending response (#{hash})")
end
View
2  test/peer_test.rb
@@ -39,7 +39,7 @@ class PeerTest < Test::Unit::TestCase
@connection_pool.expects(:with_connection).yields(conn)
conn.stubs(:closed? => false)
- conn.expects(:write).with("PROCESS #{request.hash} #{body.size}\n#{body}")
+ conn.expects(:write).with("P#{request.hash}#{[body.size].pack('N')}#{body}")
conn.expects(:flush)
@peer.client_request(request, body)
Please sign in to comment.
Something went wrong with that request. Please try again.