Class: Pandemic::ServerSide::Client

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/pandemic/server_side/client.rb

Defined Under Namespace

Classes: DisconnectClient

Constant Summary collapse

REQUEST_FLAGS =
{:async => 'a'}
EMPTY_STRING =
""
REQUEST_REGEXP =
/^([0-9]+)(?: ([#{REQUEST_FLAGS.values.join('')}]*))?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#host_port, #logger, #with_mutex

Constructor Details

#initialize(connection, server) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
# File 'lib/pandemic/server_side/client.rb', line 12

def initialize(connection, server)
  @connection = connection
  @server = server
  @received_requests = 0
  @responded_requests = MutexCounter.new                                                             
  @current_request = nil
end

Instance Attribute Details

#received_requestsObject

Returns the value of attribute received_requests.



10
11
12
# File 'lib/pandemic/server_side/client.rb', line 10

def received_requests
  @received_requests
end

#responded_requestsObject

Returns the value of attribute responded_requests.



10
11
12
# File 'lib/pandemic/server_side/client.rb', line 10

def responded_requests
  @responded_requests
end

Instance Method Details

#closeObject



83
84
85
# File 'lib/pandemic/server_side/client.rb', line 83

def close
  @listener_thread.raise(DisconnectClient)
end

#handle_request(request) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pandemic/server_side/client.rb', line 89

def handle_request(request)
  @current_request = Request.new(request)
  response = begin
    @server.handle_client_request(@current_request)
  rescue Exception => e
    warn("Unhandled exception in handle client request:\n#{e.inspect}\n#{e.backtrace.join("\n")}")
    nil
  end
  @current_request = nil
  return response
end

#listenObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pandemic/server_side/client.rb', line 20

def listen
  unless @connection.nil?
    @listener_thread.kill if @listener_thread
    @listener_thread = Thread.new do
      begin
        while @server.running
          # debug("Waiting for incoming request")
          request = @connection.gets
          # info("Received incoming request")
          @received_requests += 1
          
          if request.nil?
            # debug("Incoming request is nil")
            @connection.close
            @connection = nil
            break
          elsif request.strip! =~ REQUEST_REGEXP
            size, flags = $1.to_i, $2.to_s.split(EMPTY_STRING)
            # debug("Reading request body (size #{size})")
            body = @connection.read(size)
            # debug("Finished reading request body")
            if flags.include?(REQUEST_FLAGS[:async])
              Thread.new do
                handle_request(body)
                @responded_requests.inc
              end
            else
              response = handle_request(body)
              if response
                # debug("Writing response to client")

                # the connection could be closed, we'll let it be rescued if it is.
                @connection.write("#{response.size}\n#{response}")
                @connection.flush
                # debug("Finished writing response to client")
              else
                # debug("Writing error code to client")
                
                @connection.write("-1\n")
                @connection.flush
                # debug("Finished writing error code to client")
              end
              @responded_requests.inc
            end
          end
        end
      rescue DisconnectClient
        info("Closing client connection")
        close_connection
      rescue Errno::EPIPE
        info("Connection to client lost")
        close_connection
      rescue Exception => e
        warn("Unhandled exception in client listen thread:\n#{e.inspect}\n#{e.backtrace.join("\n")}")
      ensure
        @current_request.cancel! if @current_request
        @server.client_closed(self)
      end
    end
  end
  return self
end