Class: SNMP::RequestId

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp/manager.rb

Overview

Manage a request-id in the range 1..2**31-1

Constant Summary collapse

MAX_REQUEST_ID =
2**31

Instance Method Summary collapse

Constructor Details

#initializeRequestId

Returns a new instance of RequestId.


64
65
66
67
# File 'lib/snmp/manager.rb', line 64

def initialize
  @lock = Mutex.new
  @request_id = rand(MAX_REQUEST_ID)
end

Instance Method Details

#force_next(next_id) ⇒ Object


77
78
79
80
81
82
83
84
85
86
# File 'lib/snmp/manager.rb', line 77

def force_next(next_id)
  new_request_id = next_id.to_i
  if new_request_id < 1 || new_request_id >= MAX_REQUEST_ID
    raise "Invalid request id: #{new_request_id}"
  end
  new_request_id = MAX_REQUEST_ID if new_request_id == 1
  @lock.synchronize do
    @request_id = new_request_id - 1
  end
end

#nextObject


69
70
71
72
73
74
75
# File 'lib/snmp/manager.rb', line 69

def next
  @lock.synchronize do
    @request_id += 1
    @request_id = 1 if @request_id == MAX_REQUEST_ID
    return @request_id
  end
end