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.



48
49
50
51
# File 'lib/snmp/manager.rb', line 48

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

Instance Method Details

#force_next(next_id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/snmp/manager.rb', line 61

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



53
54
55
56
57
58
59
# File 'lib/snmp/manager.rb', line 53

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