📄 snmptable.java
字号:
/*
* Copyright (C) 2007 ETH Zurich
*
* This file is part of Fosstrak (www.fosstrak.org).
*
* Fosstrak is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* Fosstrak is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Fosstrak; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import org.fosstrak.reader.rprm.core.AntennaReadPoint;
import org.fosstrak.reader.rprm.core.NotificationChannel;
import org.fosstrak.reader.rprm.core.ReadPoint;
import org.fosstrak.reader.rprm.core.ReaderDevice;
import org.fosstrak.reader.rprm.core.ReaderProtocolException;
import org.fosstrak.reader.rprm.core.Source;
import org.fosstrak.reader.rprm.core.Trigger;
import org.fosstrak.reader.rprm.core.mgmt.IOPort;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.mib.EpcglobalReaderMib;
import org.fosstrak.reader.rprm.core.mgmt.agent.snmp.table.EpcgReaderServerTableRow.ServerTypeEnum;
import org.apache.log4j.Logger;
import org.snmp4j.agent.MOScope;
import org.snmp4j.agent.mo.DefaultMOTable;
import org.snmp4j.agent.mo.MOColumn;
import org.snmp4j.agent.mo.MOTableIndex;
import org.snmp4j.agent.mo.MOTableModel;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
/**
* Forms a SNMP table used in the reader management implementation.
*/
public class SnmpTable extends DefaultMOTable {
/**
* Denotes the table types.
*/
public enum TableTypeEnum {
EPCG_GLOBAL_COUNTERS_TABLE,
EPCG_READER_SERVER_TABLE,
EPCG_READ_POINT_TABLE,
EPCG_ANT_READ_POINT_TABLE,
EPCG_IO_PORT_TABLE,
EPCG_SOURCE_TABLE,
EPCG_NOTIFICATION_CHANNEL_TABLE,
EPCG_TRIGGER_TABLE,
EPCG_NOTIF_TRIG_TABLE,
EPCG_READ_TRIG_TABLE,
EPCG_RD_PNT_SRC_TABLE,
EPCG_NOTIF_CHAN_SRC_TABLE,
IF_TABLE,
IP_ADDR_TABLE,
IP_NET_TO_MEDIA_TABLE,
SNMP_TARGET_ADDR_TABLE,
SNMP_TARGET_PARAMS_TABLE,
SYS_OR_TABLE
}
private TableTypeEnum type;
private static Logger log = Logger.getLogger(SnmpTable.class);
/**
* Constructor.
*
* @param type
* Table type
* @param oid
* OID
* @param indexDef
* Index definition
* @param columns
* Columns
*/
public SnmpTable(TableTypeEnum type, OID oid, MOTableIndex indexDef, MOColumn[] columns) {
super(oid, indexDef, columns);
this.type = type;
}
/**
* Constructor.
*
* @param type
* Table type
* @param oid
* OID
* @param indexDef
* Index definition
* @param columns
* Columns
* @param model
* Table model
*/
public SnmpTable(TableTypeEnum type, OID oid, MOTableIndex indexDef, MOColumn[] columns, MOTableModel model) {
super(oid, indexDef, columns, model);
this.type = type;
}
/**
* Gets the value of the cell instance in the specified column and row.
*
* @param index
* the row index of the cell.
* @param col
* the column index of the cell.
* @return the value of the cell or <code>null</code> if such a cell does
* not exist.
*/
@Override
public Variable getValue(OID index, int col) {
update();
return super.getValue(index, col);
}
/**
* Gets the value of the cell instance with the specified instance OID.
*
* @param cellOID
* the instance OID of the requested cell.
* @return the value of the cell or <code>null</code> if such a cell does
* not exist.
*/
@Override
public Variable getValue(OID cellOID) {
update();
return super.getValue(cellOID);
}
/**
* Gets the indices of all rows contained in this table as a sorted
* <code>Vector</code>.
*
* @return Sorted indices
*/
public Vector<OID> getSortedIndices() {
Vector<OID> indices = new Vector<OID>();
Iterator iter = getModel().iterator();
while (iter.hasNext()) {
indices.add(((MOTableRow)iter.next()).getIndex());
}
Collections.sort(indices, new Comparator<OID>() {public int compare(OID index1, OID index2) {return index1.compareTo(index2);}});
return indices;
}
/**
* Gets the table type.
*
* @return Table type
*/
public TableTypeEnum getType() {
return type;
}
/**
* Finds the first object ID (<code>OID</code>) in the specified search
* range.
*
* @param range
* The <code>MOScope</code> for the search
* @return The <code>OID</code> that is included in the search range and
* <code>null</code> if no such instances could be found
*/
@Override
public OID find(MOScope range) {
update();
return super.find(range);
}
/**
* Updates the table.
*/
public void update() {
Vector<RowObjectContainer> conts;
Iterator iter;
ReadPoint[] readPoints;
Source[] sources;
NotificationChannel[] notifChans;
ReaderDevice readerDevice = null;
try {
readerDevice = ReaderDevice.getInstance();
} catch (ReaderProtocolException rpe) {
log.warn("Unable to obtain the ReaderDevice instance");
}
switch (type) {
case EPCG_GLOBAL_COUNTERS_TABLE:
// do nothing
break;
case EPCG_READER_SERVER_TABLE:
if (readerDevice != null) {
String dhcp = readerDevice.getDHCPServer();
String[] ntp = readerDevice.getNTPservers();
conts = new Vector<RowObjectContainer>((dhcp == null ? 0 : 1) + ntp.length);
if (dhcp != null) {
conts.add(new RowObjectContainer(TableTypeEnum.EPCG_READER_SERVER_TABLE, new Object[] { ServerTypeEnum.DHCP, dhcp }));
}
for (int i = 0; i < ntp.length; i++) {
conts.add(new RowObjectContainer(TableTypeEnum.EPCG_READER_SERVER_TABLE, new Object[] { ServerTypeEnum.TIME, ntp[i] }));
}
iter = getModel().iterator();
while (iter.hasNext()) {
SnmpTableRow row = (SnmpTableRow) iter.next();
if (((Integer32) row.getValue(EpcglobalReaderMib.idxEpcgReaderServerRowStatus)).toInt() == RowStatus.notInService) {
conts.add(row.getRowObjectContainer());
}
}
updateStoredRows(conts);
}else {
log.error(type + ": Unable to update.");
}
break;
case EPCG_READ_POINT_TABLE:
if (readerDevice != null) {
readPoints = readerDevice.getAllReadPoints();
conts = new Vector<RowObjectContainer>(readPoints.length);
for (int i = 0; i < readPoints.length; i++) {
conts.add(new RowObjectContainer(TableTypeEnum.EPCG_READ_POINT_TABLE, new Object[] {readPoints[i]}));
}
updateStoredRows(conts);
} else {
log.error(type + ": Unable to update.");
}
break;
case EPCG_ANT_READ_POINT_TABLE:
if (readerDevice != null) {
readPoints = readerDevice.getAllReadPoints();
conts = new Vector<RowObjectContainer>();
for (int i = 0; i < readPoints.length; i++) {
if (readPoints[i] instanceof AntennaReadPoint) {
conts.add(new RowObjectContainer(TableTypeEnum.EPCG_ANT_READ_POINT_TABLE, new Object[] {readPoints[i]}));
}
}
updateStoredRows(conts);
} else {
log.error(type + ": Unable to update.");
}
break;
case EPCG_IO_PORT_TABLE:
if (readerDevice != null) {
IOPort[] ioPorts = readerDevice.getAllIOPorts();
conts = new Vector<RowObjectContainer>(ioPorts.length);
for (int i = 0; i < ioPorts.length; i++) {
conts.add(new RowObjectContainer(TableTypeEnum.EPCG_IO_PORT_TABLE, new Object[] {ioPorts[i]}));
}
updateStoredRows(conts);
} else {
log.error(type + ": Unable to update.");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -