KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > snmp > TestAndIncr


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - TestAndIncr.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent.mo.snmp;
23
24 import org.snmp4j.agent.mo.MOScalar;
25 import org.snmp4j.agent.mo.MOAccessImpl;
26 import org.snmp4j.smi.Integer32;
27 import org.snmp4j.smi.OID;
28 import org.snmp4j.agent.request.SubRequest;
29 import org.snmp4j.mp.SnmpConstants;
30 import org.snmp4j.agent.request.RequestStatus;
31
32 public class TestAndIncr extends MOScalar {
33
34   public TestAndIncr(OID oid) {
35     super(oid, MOAccessImpl.ACCESS_READ_WRITE, new Integer32(0));
36   }
37
38   public void commit(SubRequest request) {
39     RequestStatus status = request.getStatus();
40     int v = ((Integer32)getValue()).getValue();
41     request.setUndoValue(getValue());
42     if (v == Integer.MAX_VALUE) {
43       v = 0;
44     }
45     else {
46       v++;
47     }
48     setValue(new Integer32(v));
49     status.setPhaseComplete(true);
50   }
51
52   public void prepare(SubRequest request) {
53     super.prepare(request);
54     if (!request.hasError()) {
55       Integer32 value = (Integer32) request.getVariableBinding().getVariable();
56       if (!getValue().equals(value)) {
57         request.getStatus().
58             setErrorStatus(SnmpConstants.SNMP_ERROR_INCONSISTENT_VALUE);
59       }
60     }
61   }
62
63   public int isValueOK(SubRequest request) {
64     Integer32 value = (Integer32) request.getVariableBinding().getVariable();
65     if (value.getValue() < 0) {
66       return SnmpConstants.SNMP_ERROR_WRONG_VALUE;
67     }
68     return SnmpConstants.SNMP_ERROR_SUCCESS;
69   }
70
71 }
72
Popular Tags