Sha256: 3d75f28b5fa794c6938f1dcb442c38ca68860a4167f37a706fc78dbaba748706
Contents?: true
Size: 1.25 KB
Versions: 12
Compression:
Stored size: 1.25 KB
Contents
require 'socket' require 'json' class FakeWireServer def initialize(port, protocol_table) @port, @protocol_table = port, protocol_table end def run @server = TCPServer.open(@port) loop { handle_connections } end private def handle_connections Thread.start(@server.accept) { |socket| open_session_on socket } end def open_session_on(socket) begin SocketSession.new(socket, @protocol_table).start rescue Exception => e raise e ensure socket.close end end class SocketSession def initialize(socket, protocol) @socket = socket @protocol = protocol end def start while message = @socket.gets handle(message) end end private def handle(data) if protocol_entry = response_to(data.strip) send_response(protocol_entry['response']) else serialized_exception = { :message => "Not understood: #{data}", :backtrace => [] } send_response(['fail', serialized_exception ].to_json) end end def response_to(data) @protocol.detect do |entry| JSON.parse(entry['request']) == JSON.parse(data) end end def send_response(response) @socket.puts response + "\n" end end end
Version data entries
12 entries across 12 versions & 4 rubygems