C++之测试snmp的注册模块

13 篇文章 0 订阅
4 篇文章 0 订阅

## main.cpp

#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include "dssx_helper.h"
#include "typedef.h"
#include "oidManager.h"

RET_CODE GET_PARAM_STRING(UINT32 uiParamId, STRING paramVal){
	cout << __FUNCTION__ << endl;
	OidManager * pManager = OidManager::getInstance();
	pManager->getString(uiParamId,paramVal);
	cout << "\n" << endl;
	return RC_SUCCESS;
}

RET_CODE SET_PARAM_STRING(UINT32 uiParamId, STRING paramVal){
	cout << __FUNCTION__ << endl;
	OidManager * pManager = OidManager::getInstance();
	pManager->setString(uiParamId,paramVal);
	cout << "\n" << endl;
	return RC_SUCCESS;
}

RET_CODE GET_PARAM_UINT32(UINT32 uiParamId, UINT32 *uiParamVal){
	cout << __FUNCTION__ << endl;
	OidManager * pManager = OidManager::getInstance();
	pManager->getUint32(uiParamId,uiParamVal);
	cout << "\n" << endl;
	return RC_SUCCESS;
}

RET_CODE SET_PARAM_UINT32(UINT32 uiParamId, UINT32 uiParamVal){
	cout << __FUNCTION__ << endl;
	OidManager * pManager = OidManager::getInstance();
	pManager->setUint32(uiParamId,uiParamVal);
	cout << "\n" << endl;
	return RC_SUCCESS;
}

RET_CODE GET_PARAM_INT32(UINT32 uiParamId, INT32 *iParamVal){
	cout << __FUNCTION__ << endl;
	OidManager * pManager = OidManager::getInstance();
	pManager->getInt32(uiParamId,iParamVal);
	cout << "\n" << endl;
	return RC_SUCCESS;
}

RET_CODE SET_PARAM_INT32(UINT32 uiParamId, INT32 iParamVal){
	cout << __FUNCTION__ << endl;
	OidManager * pManager = OidManager::getInstance();
	pManager->setInt32(uiParamId,iParamVal);
	cout << "\n" << endl;
	return RC_SUCCESS;
}

int main(int argc,char **argv){
	if (argc < 2){
		cout << __FUNCTION__ << "::\t" << "Usage: ./command modulename" << endl;
		return 0;
	}
	string fname = string(argv[1]) + ".txt";
	OidManager::getInstance()->loadOidFile(fname.c_str());
    DSSX_INIT(argv[1]);
    DSSX_REG_PARAM_STRING_CALLBACK(GET_PARAM_STRING,SET_PARAM_STRING);
	DSSX_REG_PARAM_UINT32_CALLBACK(GET_PARAM_UINT32,SET_PARAM_UINT32);
	DSSX_REG_PARAM_INT32_CALLBACK(GET_PARAM_INT32,SET_PARAM_INT32);
	while(1){
		sleep(1);
	}
	return 0;
}


## oidManager.h

#ifndef OID_MANAGER_H
#define OID_MANAGER_H

#include <string>
#include <vector>
#include "typedef.h"
using namespace std;

class Node{
public:
	Node(string _req,string _type,string _id,string _val,string _comment){
		req = _req;
		type = _type;
		id = _id;
		val = _val;
		comment = _comment;
	}
	string req;
	string type;
	string id;
	string val;
	string comment;
};

class OidManager{
public:
	static OidManager * getInstance();
	bool loadOidFile(const string & fname);
	/*getString & setString*/
	bool getString(UINT32 paramId ,STRING val);
	bool setString(UINT32 paramId ,STRING val);
	/*getUin32 & setUint32*/
	bool getUint32(UINT32 paramId ,UINT32 *uiParamVal);
	bool setUint32(UINT32 paramId ,UINT32 uiParamVal);
	/*Int32*/
	bool getInt32(UINT32 paramId ,INT32 *iParamVal);
	bool setInt32(UINT32 paramId ,INT32 Val);
private:
	OidManager(){
		m_isLoaded = false;
		m_nodeList.clear();
	}
	~OidManager(){
	}
	Node parseBuffeToNode(const char * buffer);
private:
	bool m_isLoaded;
	vector<Node> m_nodeList;
};

#endif


## oidManager.cpp

#include "oidManager.h"
#include <strstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <iterator>
using namespace std;

string toString(int n){
    strstream m;
    m<<n;
    string s;
    m>>s;
    return s;
}

int toINT(string str){
    strstream m;
    m<<str;
    int n;
    m>>n;
    return n;
}

static OidManager * g_instance = NULL;
/******************************************************************************/
OidManager * OidManager::getInstance(){
	if (g_instance == NULL)
		g_instance = new OidManager();
	return g_instance;
}
/******************************************************************************/
bool OidManager::loadOidFile(const string & fname){
	if (m_isLoaded){
		cout << __FUNCTION__ << "::\t" << "oid config file is loaded." << endl;
		return true;
	}
	FILE * fp = fopen(fname.c_str(),"r");
	if ( fp == NULL){
		cout << __FUNCTION__ << "::\t" << "fopen() failed ,please make sure " << fname << "exist" << endl;
		return false;
	}
	char buffer[100];
	while(true){
		memset(buffer,'\0',sizeof(buffer));
		char * ret = fgets(buffer, sizeof(buffer), fp);
		if (ret == NULL){
			cout << __FUNCTION__ << "::\t" << "fgets() read eof." << endl;
			break;
		}
		Node node = parseBuffeToNode(buffer);
		m_nodeList.push_back(node);
	}
	fclose(fp);
}

Node OidManager::parseBuffeToNode(const char * buffer){
	char req[10], type[10], id[10], val[20], comment[20];
	memset(req,	'\0',10);
	memset(type,'\0',10);
	memset(id,	'\0',10);
	memset(val,	'\0',20);
	memset(comment,	'\0',20);
	cout << buffer << endl;
	sscanf(buffer,"%s\t%s\t%s\t%s\t%s",req,type,id,val,comment);
#ifdef DDEBUG
	cout << __FUNCTION__ << "::\t" << "Parsed Commont <" << req << ">" << endl;
	cout << __FUNCTION__ << "::\t" << "Parsed Commont <" << type << ">" << endl;
	cout << __FUNCTION__ << "::\t" << "Parsed Commont <" << id << ">" << endl;
	cout << __FUNCTION__ << "::\t" << "Parsed Commont <" << val << ">" << endl;
	cout << __FUNCTION__ << "::\t" << "Parsed Commont <" << comment << ">" << endl;
#endif
	return Node(string(req),string(type),string(id),string(val),string(comment));
}

/******************************************************************************/
/*getString & setString*/
bool OidManager::getString(UINT32 paramId ,STRING val){
	vector<Node>::iterator pIterator = m_nodeList.begin();
	for (pIterator; pIterator != m_nodeList.end(); pIterator++){
		if ( pIterator->req == "get" && pIterator->type == "string"){
			if ( toString(paramId) == pIterator->id){
				strcpy(val, pIterator->val.c_str());
				break;
			}
		}
	}
	if (pIterator == m_nodeList.end()){
		cout << __FUNCTION__ << "::\t" << "Cannot Find the Record with paramId " << paramId << endl;
		return false;
	}
	cout << __FUNCTION__ << "::\t" << "Find the Record (" << pIterator->comment << ")" << endl;
	return true;
}
bool OidManager::setString(UINT32 paramId ,STRING val){
	vector<Node>::iterator pIterator = m_nodeList.begin();
	for (pIterator; pIterator != m_nodeList.end(); pIterator++){
		if ( pIterator->req == "set" && pIterator->type == "string"){
			if ( toString(paramId) == pIterator->id){
				break;
			}
		}
	}
	if (pIterator == m_nodeList.end()){
		cout << __FUNCTION__ << "::\t" << "Cannot Find the Record with paramId " << paramId << endl;
		return false;
	}
	cout << __FUNCTION__ << "::\t" << "Find the Record " << pIterator->comment << ")" << endl;
	return true;
}
/******************************************************************************/
/*getUin32 & setUint32*/
bool OidManager::getUint32(UINT32 paramId ,UINT32 *uiParamVal){
	vector<Node>::iterator pIterator = m_nodeList.begin();
	for (pIterator; pIterator != m_nodeList.end(); pIterator++){
		if ( pIterator->req == "get" && pIterator->type == "uint"){
			if ( toString(paramId) == pIterator->id){
				*uiParamVal = toINT(pIterator->val);
				break;
			}
		}
	}
	if (pIterator == m_nodeList.end()){
		cout << __FUNCTION__ << "::\t" << "Cannot Find the Record with paramId " << paramId << endl;
		return false;
	}
	cout << __FUNCTION__ << "::\t" << "Find the Record " << pIterator->comment << ")" << endl;
	return true;
}
bool OidManager::setUint32(UINT32 paramId ,UINT32 uiParamVal){
	vector<Node>::iterator pIterator = m_nodeList.begin();
	for (pIterator; pIterator != m_nodeList.end(); pIterator++){
		if ( pIterator->req == "set" && pIterator->type == "uint"){
			if ( toString(paramId) == pIterator->id){
				break;
			}
		}
	}
	if (pIterator == m_nodeList.end()){
		cout << __FUNCTION__ << "::\t" << "Cannot Find the Record with paramId " << paramId << endl;
		return false;
	}
	cout << __FUNCTION__ << "::\t" << "Find the Record " << pIterator->comment << ")" << endl;
	return true;
}
/******************************************************************************/
/*Int32*/
bool OidManager::getInt32(UINT32 paramId ,INT32 *iParamVal){
	vector<Node>::iterator pIterator = m_nodeList.begin();
	for (pIterator; pIterator != m_nodeList.end(); pIterator++){
		if ( pIterator->req == "get" && pIterator->type == "int"){
			if ( toString(paramId) == pIterator->id){
				*iParamVal = toINT(pIterator->val);
				break;
			}
		}
	}
	if (pIterator == m_nodeList.end()){
		cout << __FUNCTION__ << "::\t" << "Cannot Find the Record with paramId " << paramId << endl;
		return false;
	}
	cout << __FUNCTION__ << "::\t" << "Find the Record " << pIterator->comment << ")" << endl;
	return true;
}
bool OidManager::setInt32(UINT32 paramId ,INT32 Val){
	vector<Node>::iterator pIterator = m_nodeList.begin();
	for (pIterator; pIterator != m_nodeList.end(); pIterator++){
		if ( pIterator->req == "set" && pIterator->type == "int"){
			if ( toString(paramId) == pIterator->id){
				break;
			}
		}
	}
	if (pIterator == m_nodeList.end()){
		cout << __FUNCTION__ << "::\t" << "Cannot Find the Record with paramId " << paramId << endl;
		return false;
	}
	cout << __FUNCTION__ << "::\t" << "Find the Record " << pIterator->comment << ")" << endl;
	return true;
}
/******************************************************************************/


## Makefile

TARGET=ok
INCLUDES=-I/home/zengzhe/avod/api/common -I/home/zengzhe/avod/api/emod_helper
LIBS=-L/home/zengzhe/avod/lib/LOCAL -ldssx -lcommon -lpthread
SOURCES=main.cpp oidManager.cpp
$(TARGET):
	g++ $(SOURCES) -o $(TARGET) $(INCLUDES) $(LIBS)
clean:
	rm -rf $(TARGET) *.o *~


## server_suu.txt

get	string	10	getVersion	getVersion
get	string	11	queryUpdate	queryUpdate
get	string	12	queryUpdateStat	queryUpdateStat
get	string	13	ConfirmUpdate	ConfirmUpdate


## 用法说明

./ok server_suu.txt

该模块是配合自定义snmpd一起进行使用的辅助模块,用于验证是否正确设置了snmpd的扩展oid的接口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值