📄 tableutils.java
字号:
int pos = startCol + c;
VariableBinding vb = response.get(r * cols + c);
if (vb.isException()) {
continue;
}
OID id = vb.getOid();
OID col = columnOIDs[pos];
if (id.startsWith(col)) {
OID index =
new OID(id.getValue(), col.size(), id.size() - col.size());
if ((upperBoundIndex != null) &&
(index.compareTo(upperBoundIndex) > 0)) {
continue;
}
if ((lastMinIndex == null) ||
(index.compareTo(lastMinIndex) < 0)) {
lastMinIndex = index;
}
anyMatch = true;
if ((row == null) || (!row.getRowIndex().equals(index))) {
row = null;
for (ListIterator it = rowCache.listIterator(rowCache.size());
it.hasPrevious(); ) {
Row lastRow = (Row) it.previous();
int compareResult = index.compareTo(lastRow.getRowIndex());
if (compareResult == 0) {
row = lastRow;
break;
}
else if (compareResult > 0) {
break;
}
}
}
if (row == null) {
row = new Row(index);
if (rowCache.size() == 0) {
rowCache.add(row);
}
else if (((Row) rowCache.getFirst()).getRowIndex().compareTo(
index) >= 0) {
rowCache.addFirst(row);
}
else {
for (ListIterator it = rowCache.listIterator(rowCache.size());
it.hasPrevious(); ) {
Row lastRow = (Row) it.previous();
if (index.compareTo(lastRow.index) >= 0) {
it.set(row);
it.add(lastRow);
break;
}
}
}
}
if (((!row.setNumComplete(pos)) ||
(row.size() > pos)) && (row.get(pos) != null)) {
finished = true;
listener.finished(new TableEvent(this, userObject,
TableEvent.STATUS_WRONG_ORDER));
return;
}
row.setNumComplete(pos);
if (pos < row.getNumComplete()) {
row.set(pos, vb);
}
else {
row.add(vb);
}
lastReceived.set(pos, vb.getOid());
}
}
}
while ((rowCache.size() > 0) &&
(((Row) rowCache.getFirst()).getNumComplete() ==
columnOIDs.length) &&
((lastMinIndex == null) ||
(((Row) rowCache.getFirst()).getRowIndex().compareTo(
lastMinIndex) < 0))) {
if (!listener.next(getTableEvent())) {
finished = true;
listener.finished(new TableEvent(this, userObject));
return;
}
}
if (!sendNextChunk()) {
if (anyMatch) {
sent = 0;
sendNextChunk();
}
else {
emptyCache();
finished = true;
listener.finished(new TableEvent(this, userObject));
}
}
}
}
protected boolean checkResponse(ResponseEvent event) {
if (event.getError() != null) {
finished = true;
emptyCache();
listener.finished(new TableEvent(this, userObject, event.getError()));
}
else if (event.getResponse() == null) {
finished = true;
// timeout
emptyCache();
listener.finished(new TableEvent(this, userObject,
TableEvent.STATUS_TIMEOUT));
}
else if (event.getResponse().getType() == PDU.REPORT) {
finished = true;
emptyCache();
listener.finished(new TableEvent(this, userObject,
event.getResponse()));
}
else if (event.getResponse().getErrorStatus() != PDU.noError) {
finished = true;
emptyCache();
listener.finished(new TableEvent(this, userObject,
event.getResponse().getErrorStatus()));
}
else {
return true;
}
return false;
}
private void emptyCache() {
while (rowCache.size() > 0) {
if (!listener.next(getTableEvent())) {
break;
}
}
}
private TableEvent getTableEvent() {
Row r = (Row) rowCache.removeFirst();
r.setNumComplete(columnOIDs.length);
VariableBinding[] vbs = new VariableBinding[r.size()];
r.copyInto(vbs);
return new TableEvent(this, userObject, r.getRowIndex(), vbs);
}
public Row getRow(OID index) {
for (ListIterator it = rowCache.listIterator(rowCache.size() + 1);
it.hasPrevious(); ) {
Row r = (Row) it.previous();
if (index.equals(r.getRowIndex())) {
return r;
}
}
return null;
}
}
/**
* The <code>DenseTableRequest</code> extends TableRequest to implement a
* faster table retrieval than the original. Caution:
* This version does not correctly retrieve sparse tables!
*
* @author Frank Fock
* @since 1.5
*/
class DenseTableRequest extends TableRequest {
protected DenseTableRequest(Target target,
OID[] columnOIDs,
TableListener listener,
Object userObject,
OID lowerBoundIndex,
OID upperBoundIndex) {
super(target, columnOIDs, listener, userObject, lowerBoundIndex,
upperBoundIndex);
}
public synchronized void onResponse(ResponseEvent event) {
// Do not forget to cancel the asynchronous request! ;-)
session.cancel(event.getRequest(), this);
if (finished) {
return;
}
if (checkResponse(event)) {
int startCol = ((Integer) event.getUserObject()).intValue();
PDU request = event.getRequest();
PDU response = event.getResponse();
int cols = request.size();
int rows = response.size() / cols;
OID lastMinIndex = null;
for (int r = 0; r < rows; r++) {
Row row = null;
for (int c = 0; c < request.size(); c++) {
int pos = startCol + c;
VariableBinding vb = response.get(r * cols + c);
if (vb.isException()) {
continue;
}
OID id = vb.getOid();
OID col = columnOIDs[pos];
if (id.startsWith(col)) {
OID index =
new OID(id.getValue(), col.size(), id.size() - col.size());
if ((upperBoundIndex != null) &&
(index.compareTo(upperBoundIndex) > 0)) {
continue;
}
if ((lastMinIndex == null) ||
(index.compareTo(lastMinIndex) < 0)) {
lastMinIndex = index;
}
if (row == null) {
row = new Row(index);
}
row.setNumComplete(pos);
if (pos < row.getNumComplete()) {
row.set(pos, vb);
}
else {
row.add(vb);
}
lastReceived.set(pos, vb.getOid());
}
}
if (row != null) {
if (!listener.next(new TableEvent(this, userObject, row.getRowIndex(),
(VariableBinding[])
row.toArray(new VariableBinding[0])))) {
finished = true;
listener.finished(new TableEvent(this, userObject));
return;
}
}
}
if (!sendNextChunk()) {
finished = true;
listener.finished(new TableEvent(this, userObject));
}
}
}
}
/**
* Creates a SNMP table row for a table that support the RowStatus
* mechanism for row creation.
* @param target
* the Target SNMP entity for the operation.
* @param rowStatusColumnOID
* the column OID of the RowStatus column (without any instance identifier).
* @param rowIndex
* the OID denoting the index of the table row to create.
* @param values
* the values of columns to set in the row. If <code>values</code> is
* <code>null</code> the row is created via the tripple mode row creation
* mechanism (RowStatus is set to createAndWait). Otherwise, each variable
* binding has to contain the OID of the columnar object ID (without any
* instance identifier) and its value. On return, the variable bindings
* will be modified so that the variable binding OIDs will contain the
* instance OIDs of the respective columns (thus column OID + rowIndex).
* @return ResponseEvent
* the ResponseEvent instance returned by the SNMP session on response
* of the internally sent SET request. If <code>null</code>, an IO
* exception has occurred. Otherwise, if the response PDU is
* <code>null</code> a timeout has occured, Otherwise, check the error
* status for {@link SnmpConstants#SNMP_ERROR_SUCCESS} to verify that the
* row creation was successful.
* @since 1.6
*/
public ResponseEvent createRow(Target target,
OID rowStatusColumnOID, OID rowIndex,
VariableBinding[] values) {
PDU pdu = pduFactory.createPDU(target);
OID rowStatusID = new OID(rowStatusColumnOID);
rowStatusID.append(rowIndex);
VariableBinding rowStatus = new VariableBinding(rowStatusID);
if (values != null) {
// one-shot mode
rowStatus.setVariable(new Integer32(ROWSTATUS_CREATEANDGO));
}
else {
rowStatus.setVariable(new Integer32(ROWSTATUS_CREATEANDWAIT));
}
pdu.add(rowStatus);
if (values != null) {
// append index to all columnar values
for (int i = 0; i < values.length; i++) {
OID columnOID = new OID(values[i].getOid());
columnOID.append(rowIndex);
values[i].setOid(columnOID);
}
pdu.addAll(values);
}
pdu.setType(PDU.SET);
try {
ResponseEvent responseEvent = session.send(pdu, target);
return responseEvent;
}
catch (IOException ex) {
logger.error(ex);
}
return null;
}
/**
* Destroys a SNMP table row from a table that support the RowStatus
* mechanism for row creation/deletion.
* @param target
* the Target SNMP entity for the operation.
* @param rowStatusColumnOID
* the column OID of the RowStatus column (without any instance identifier).
* @param rowIndex
* the OID denoting the index of the table row to destroy.
* @return ResponseEvent
* the ResponseEvent instance returned by the SNMP session on response
* of the internally sent SET request. If <code>null</code>, an IO
* exception has occurred. Otherwise, if the response PDU is
* <code>null</code> a timeout has occured, Otherwise, check the error
* status for {@link SnmpConstants#SNMP_ERROR_SUCCESS} to verify that the
* row creation was successful.
* @since 1.7.6
*/
public ResponseEvent destroyRow(Target target,
OID rowStatusColumnOID, OID rowIndex) {
PDU pdu = pduFactory.createPDU(target);
OID rowStatusID = new OID(rowStatusColumnOID);
rowStatusID.append(rowIndex);
VariableBinding rowStatus = new VariableBinding(rowStatusID);
rowStatus.setVariable(new Integer32(ROWSTATUS_DESTROY));
pdu.add(rowStatus);
pdu.setType(PDU.SET);
try {
ResponseEvent responseEvent = session.send(pdu, target);
return responseEvent;
}
catch (IOException ex) {
logger.error(ex);
}
return null;
}
class Row extends Vector {
private static final long serialVersionUID = -2297277440117636627L;
private OID index;
public Row(OID index) {
super();
this.index = index;
}
public OID getRowIndex() {
return index;
}
public int getNumComplete() {
return super.size();
}
public boolean setNumComplete(int numberOfColumnsComplete) {
int sz = numberOfColumnsComplete - getNumComplete();
for (int i = 0; i < sz; i++) {
super.add(null);
}
return (sz >= 0);
}
}
class InternalTableListener implements TableListener {
private List rows = new LinkedList();
public boolean next(TableEvent event) {
rows.add(event);
return true;
}
public synchronized void finished(TableEvent event) {
if ((event.getStatus() != TableEvent.STATUS_OK) ||
(event.getIndex() != null)) {
rows.add(event);
}
notify();
}
public List getRows() {
return rows;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -