[SNMP4J] Unregister MO with context doesnt work
Frank Fock
fock at agentpp.com
Tue Jun 17 20:58:08 CEST 2014
Hi Dave,
Thank you for reporting this bug. It was actually caused by a bug (hole)
in the corresponding
JUnit test and therefore remained undiscovered until now.
The latest snapshot of SNMP4J-Agent should fix the issue.
The patch is:
Index: MOScopeComparator.java
===================================================================
--- MOScopeComparator.java (revision 2722)
+++ MOScopeComparator.java (working copy)
@@ -35,7 +35,7 @@
* is checked. If it is unbounded (upper bound is <code>null</code), then
* the scoped is deemed to be greater than the query. Otherwise, the
upper bound
* of the scope is compared with the lower bound of the query. Scope
and query
- * are deemd to be equal if both bounds are equal and are both included.
+ * are deemed to be equal if both bounds are equal and both are included.
* Otherwise the scope is deemed to be less than the query.
*
* @author Frank Fock
@@ -77,19 +77,31 @@
result = -compareContextScope(o2, (MOContextScope) o1);
}
else {
- result = o1.getLowerBound().compareTo(o2.getLowerBound());
- if (result == 0) {
- if ((!o1.isUpperIncluded()) ||
- (!o2.isLowerIncluded())) {
- return -1;
- }
- }
+ result = compareScope(o1, o2);
}
}
return result;
}
private static int compareContextScope(MOScope scope, MOContextScope
scope2) {
+ if (scope == scope2) {
+ return 0;
+ }
+ int result = compareScope(scope, scope2);
+ if ((result == 0) && (scope instanceof MOContextScope)) {
+ OctetString c1 = ((MOContextScope)scope).getContext();
+ OctetString c2 = scope2.getContext();
+ if ((c1 != null) && (c2 != null)) {
+ result = c1.compareTo(c2);
+ }
+ }
+ return result;
+ }
+
+ private static int compareScope(MOScope scope, MOScope scope2) {
+ if ((scope == scope2) || scope.equals(scope2)) {
+ return 0;
+ }
int result = 0;
if (scope.getUpperBound() == null) {
result = 1;
@@ -104,28 +116,19 @@
else {
result = scope.getUpperBound().compareTo(scope2.getUpperBound());
if (result == 0) {
+ result += (scope.isUpperIncluded() ? -1 : 0);
+ result += (scope2.isUpperIncluded() ? 1 : 0);
if (result == 0) {
- result += (scope.isUpperIncluded() ? -1 : 0);
- result += (scope2.isUpperIncluded() ? 1 : 0);
- }
- if (result == 0) {
result =
scope.getUpperBound().compareTo(scope2.getLowerBound());
if (result == 0) {
if ((!scope.isUpperIncluded()) ||
- (!scope2.isLowerIncluded())) {
+ (!scope2.isLowerIncluded())) {
return -1;
}
}
}
}
}
- if ((result == 0) && (scope instanceof MOContextScope)) {
- OctetString c1 = ((MOContextScope)scope).getContext();
- OctetString c2 = scope2.getContext();
- if ((c1 != null) && (c2 != null)) {
- result = c1.compareTo(c2);
- }
- }
return result;
}
Best regards,
Frank
Am 17.06.2014 16:44, schrieb Pentzlin, David [Contractor]:
> Maybe I should add:
> If a managedobject without context(null) will be unregistered the result of the comperator is:
> public int compare(MOScope o1, MOScope o2) {
> if (o1==o2) {
> return 0; // ensure identity is equal
> }......
> Because the instance of the scope is the same (mo.getscope()).
>
> Register or unregister of a mo with context will always use a new instance of DefaultMOContextScope:
> new DefaultMOContextScope(context, mo.getScope());
>
> so o1==o2 in the comperator is false and the rest of the comperator will never result in 0 if the mo is the same.
> If you change the comperator to:
> public int compare(MOScope o1, MOScope o2) {
> if (o1.equals(o2)) {
> return 0; // ensure identity is equal
> }.....
> It will work with the DefaultMOContextScope too (it will compare the content instead of the instance).
> Now registry.remove(key); will remove the mo from registry
>
> Is this the correct fix for this bug?
>
> Best regards
> Dave
>
> -----Previous Message-----
> Subject: [SNMP4J] Unregister MO with context doesnt work
>
> Unregister a managed object will call "public void unregister(ManagedObject mo, OctetString context)" (DefaultMOServer.java).
> This will remove the mo from registry (treemap) with "registry.remove(key);"
>
> The problem is the comperator from the treemap registry will call: "compareContextScope(MOScope scope, MOContextScope scope2)"
> In this function it will compare first the upperbound and if it is the same it will do:
> if (result == 0) {
> result = scope.getUpperBound().compareTo(scope2.getLowerBound());
>
> and this is always 1 so the result is always -1 or 1 even if it is exact the same scope. So it will never be removed from registry.
>
> If you unregister without context (context==null) the compare function:
> result = o1.getLowerBound().compareTo(o2.getLowerBound());
> will return 0 and it will be removed from registry.
>
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://oosnmp.net/mailman/listinfo/snmp4j
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://oosnmp.net/mailman/listinfo/snmp4j
--
---
AGENT++
Maximilian-Kolbe-Str. 10
73257 Koengen, Germany
https://agentpp.com
Phone: +49 7024 8688230
Fax: +49 7024 8688231
More information about the SNMP4J
mailing list