Class: Pandemic::ServerSide::Request

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

Constant Summary collapse

@@request_count =
MutexCounter.new
@@late_responses =
MutexCounter.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#host_port, #logger, #with_mutex

Constructor Details

#initialize(body) ⇒ Request

Returns a new instance of Request.



20
21
22
23
24
25
26
27
# File 'lib/pandemic/server_side/request.rb', line 20

def initialize(body)
  @request_number = @@request_count.inc
  @body = body
  @responses = []
  @responses_mutex = Monitor.new
  @waiter = @responses_mutex.new_cond
  @complete = false
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/pandemic/server_side/request.rb', line 8

def body
  @body
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#max_responsesObject

Returns the value of attribute max_responses.



9
10
11
# File 'lib/pandemic/server_side/request.rb', line 9

def max_responses
  @max_responses
end

Class Method Details

.total_late_responsesObject



16
17
18
# File 'lib/pandemic/server_side/request.rb', line 16

def self.total_late_responses
  @@late_responses.real_total
end

.total_request_countObject



12
13
14
# File 'lib/pandemic/server_side/request.rb', line 12

def self.total_request_count
  @@request_count.real_total
end

Instance Method Details

#add_response(response) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pandemic/server_side/request.rb', line 29

def add_response(response)
  @responses_mutex.synchronize do
    if @responses.frozen? # too late
      @@late_responses.inc
      return  
    end
    # debug("Adding response")
    @responses << response
    if @max_responses && @responses.size >= @max_responses
      # debug("Hit max responses, waking up waiting thread")
      wakeup_waiting_thread
      @complete = true
    end
  end
end

#cancel!Object



53
54
55
56
57
58
# File 'lib/pandemic/server_side/request.rb', line 53

def cancel!
  # consider telling peers that they should stop, but for now this is fine.
  @responses_mutex.synchronize do
    wakeup_waiting_thread
  end
end

#hashObject



78
79
80
# File 'lib/pandemic/server_side/request.rb', line 78

def hash
  @hash ||= Digest::MD5.hexdigest("#{@request_number} #{@body}")[0,10]
end

#responsesObject



49
50
51
# File 'lib/pandemic/server_side/request.rb', line 49

def responses
  @responses # its frozen so we don't have to worry about mutex
end

#wait_for_responsesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pandemic/server_side/request.rb', line 60

def wait_for_responses
  @responses_mutex.synchronize do
    return if @complete
    if Config.response_timeout <= 0
      @waiter.wait
    elsif !MONITOR_TIMEOUT_AVAILABLE
      Thread.new do
        sleep Config.response_timeout
        wakeup_waiting_thread
      end
      @waiter.wait
    else
      @waiter.wait(Config.response_timeout)
    end
    @responses.freeze
  end
end

#wakeup_waiting_threadObject



45
46
47
# File 'lib/pandemic/server_side/request.rb', line 45

def wakeup_waiting_thread
  @waiter.signal if @waiter
end