1. SNMP Syntax Classes
SNMP++之SNMP Syntax的对象模型(Object Modeling Technique)视图
SNMP++的SNMP syntax classe描绘了一个(具有)C++面向对象风格的视图。(即)用于(描述)SNMP的SMI之ASN.1的数据类型视图。它包含了映射到对应的SMI类型的一组类的集合。而且为了方便使用,还引入了一些非SMI的类。SNMP++为这些SNMP数据类型提供了强大、简单易用的接口。下表概要地描述了各种SNMP++ syntax classes:
SNMP++ Syntax Class Name | Class Description | SMI or ASN.1 Counter part |
SnmpSyntax | Parent of all syntax classes. | No ASN.1 counter part, used for OO structure. |
Oid | Object identifier class. | ASN.1 Object Identifier. |
OctectStr | Octet string class. | ASN.1 Octet string. |
Uint32 | Unsigned 32 bit integer class. | SMI unsigned 32 bit integer. |
TimeTicks | TimeTicks class. | SMI time ticks. |
Counter32 | 32 bit counter class. | SMI 32 bit counter. |
Gauge32 | 32 bit gauge class. | SMI 32 bit gauge. |
Int32 | Signed 32 bit integer. | SMI 32 bit signed integer. |
Counter64 | 64 bit counter class. | SMI 64 bit counter. |
Address | Abstract address class. | No ASN.1 counter part used for OO structure. |
IpAddress | IP address class. | SMI IP address. |
UdpAddress | UdpAddress class | SMI IP address with port specification. |
IpxAddress | IPX address class. | No ASN.1 or SMI counter part |
IpxSockAddress | IPX Address class with socket number. | No ASN.1 or SMI counter part |
MacAddress | MAC address class. | SMI counter part |
GenAddress | Generic Address | No ASN.1 or SMI counter part. |
2. Object Id Class
SNMP++之Oid Class的对象模型(Object Modeling Technique)视图
2.1. 对象标识符类
对象标识符类(Oid)封装了SMI的对象标识。信息管理库(MIB)中所定义的SMI的对象是一种在MIB中找到的数据元素的数据标识。与SMI Oid相关的结构和函数,自然都是面向对象的。事实上Oid class与C++的String class有许多共同之处。如果你熟悉C++的String class或者MFC的CString class,那么你就会感觉Oid class用起来很亲切、简单。Oid class被设计成了快速有效的类;它可以定义和操作对象标识;不依赖现有的 SNMP API,完全是可移植的;可以在任何ANSI C++编译器上进行编译。
2.2. Oid Class的成员函数列表
Oid Class Member Functions | Description |
Constructors |
|
Oid::Oid( void); | 构造一个空的的Oid |
Oid::Oid( const char *dotted_string); | 用带点的字符串构造新的Oid |
Oid::Oid( const Oid &oid); | 用已有的Oid构造新的Oid |
Oid::Oid( const unsigned long *data, int len); | 通过一个指针和长度来构造一个新的Oid |
Destructor |
|
Oid::~Oid( ); | 销毁一个Oid,释放所有的堆 |
Overloaded Operators |
|
Oid & operator = ( const char *dotted_string); | 将一个带点的字符串付给一个Oid |
Oid & operator = ( const Oid &oid); | 将一个Oid付给另一个Oid |
int operator == ( const Oid &lhs, const Oid& rhs); | 判断两个Oid是否相等 |
int operator == ( const Oid& lhs, const char*dotted_string); | 判断一个Oid是否和一个带点的字符串相等 |
int operator != ( const Oid &lhs, const Oid& rhs); | 判断两个Oid是否不等 |
int operator != ( const Oid & lhs, const char *dotted_string); | 判断一个Oid是否和一个带点的字符串不相等 |
int operator < ( const Oid &lhs, const Oid& rhs); | 判断一个Oid是否比另一个小 |
int operator < ( const Oid &lhs, const char *dotted_string); | 判断一个Oid是否比一个带点的字符串小 |
int operator <=( const Oid &lhs,const Oid &rhs); | 判断一个Oid是否“小于等于”另一个 |
int operator <= ( const Oid &lhs, const char *dotted_string); | 判断一个Oid是否“小于等于”一个带点的字符串 |
int operator > ( const Oid &lhs, const Oid &rhs); | 判断一个Oid是否比另一个大 |
int operator > ( const Oid &lhs, const char * dotted_string); | 判断一个Oid是否比一个带点的字符串大 |
int operator >= ( const Oid&lhs, const Oid &rhs); | 判断一个Oid是否“大于等于”另一个 |
int operator >= ( const Oid &lhs, const char* dotted_string); | 判断一个Oid是否“大于等于”一个带点的字符串 |
Oid& operator += ( const char *dotted_string); | 将一个带点的字符串加到一个Oid后面 |
Oid& operator +=( const unsigned long i); | 将一个整数加到一个带点的Oid字符串后面 |
Oid& operator+=( const Oid& oid); | 将一个Oid加到另一个Oid后面 |
unsigned long &operator [ ] ( int position); | Access an individual sub-element of an Oid, read or write. 访问Oid的一个独立子单元 |
Output Member Functions |
|
char * get_printable( const unsigned int n); | 返回一个Oid的n个带点格式的子单元 |
char *get_printable( const unsigned long s, const unsigned long n); | 返回从s开始,以点号分割的n个子单元 |
char *get_printable(); | 返回以点号分割的Oid字符串 |
operator char *(); | 同上 |
Miscellaneous Member Functions |
|
set_data (const unsigned long *data,const unsigned long n); | 用指针和长度来设置一个Oid |
unsigned long len( ); | 返回Oid中子单元的个数(长度) |
trim( const unsigned long n=1); | 删除Oid最右边的n个子单元,默认删除1个 |
nCompare( const unsigned long n, const Oid& oid); | 从左至右比较Oid的前n个子单元 |
RnCompare( const unsigned long n, const Oid& oid); | 从右至左比较Oid的后n个子单元 |
int valid( ); | 返回Oid的有效性 |
2.3. 一些Oid Class的例子
下面的例子展示了Oid Class的不同用法。Oid Class不需要依赖其他库和模块。下列代码在ANSI/ISO C++上编译通过
#include “oid.h” void oid_example() { // construct an Oid with a dotted string and print it out Oid o1(" 1.2.3 .4.5.6.7.8.9.1"); cout << “o1= “ << o1.get_printable (); // construct an Oid with another Oid and print it out Oid o2(o1); cout << “o2= ” << o2.get_printable(); // trim o 2’ s last value and print it out o2.trim(1); cout << “o2= ” << o2.get_printable();
// add a 2 value to the end of o2 and print it out o2+=2; cout << “o2= ” << o2.get_printable();
// create a new Oid, o3 Oid o3; // assign o 3 a value and print it out o3=" 1.2.3 .4.5.6.7.8.9.3"; cout << “o3= ” << o3.get_printable();
// create o4 Oid o4; // assign o4 o 1’ s value o4=o1;
// trim off o4 by 1 o4.trim(1);
// concat a 4 onto o4 and print it out o4+=” .4” ; cout << “o4= ” << o4.get_printable();
// make o5 from o1 and print it out Oid o5(o1); cout << “o5= ” << o5.get_printable(); |
// compare two not equal oids if (o1==o2) cout << "O1 EQUALS O2"; else cout << "o1 not equal to o2";
// print out a piece of o1 cout << "strval(3) of O1 = “ << o1.get_printable(3);
// print out a piece of o1 cout << "strval(1,3) of O1 = “ << o1.get_printable(1,3);
// set o1's last subid o1[ o1.len()-1] = 49; cout << "O1 modified = “ << o1.get_printable();
// set o1's 3rd subid o1[2]=49; cout << "O1 modified = “ << o1.get_printable();
// get the last subid of 02 cout << "last of o2 = “ << o2[o2.len()-1];
// get the 3rd subid of 02 cout << "3rd of o2 = “ << o2[2];
// ncompare if (o1.nCompare(3,o2)) cout << "nCompare o1,o2,3 =="; else cout << "nCompare o1,o2,3 !=";
// make an array of oids Oid oids[30]; int w; for ( w=0;w<30;w++) { oids[w] = "300.301.302.303.304.305.306.307"; oids[w] += (w+1); } for (w=0;w<25;w++) { sprintf( msg,"Oids[%d] = %s",w, oids[w].get_printable()); printf(“%s”,msg, strlen(msg)); } } |