From 5223bc7c163c9cd52e9ae4d8ad93a96e40e86632 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 30 May 2011 18:47:19 +0300 Subject: [PATCH] Base plugin structure implemented for SNMP Agent plugin --- .../stargazer/plugins/other/snmp/snmp.cpp | 93 +++++++++++++++++++ projects/stargazer/plugins/other/snmp/snmp.h | 69 ++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 projects/stargazer/plugins/other/snmp/snmp.h diff --git a/projects/stargazer/plugins/other/snmp/snmp.cpp b/projects/stargazer/plugins/other/snmp/snmp.cpp index e69de29b..4d26a8b9 100644 --- a/projects/stargazer/plugins/other/snmp/snmp.cpp +++ b/projects/stargazer/plugins/other/snmp/snmp.cpp @@ -0,0 +1,93 @@ +#include "asn1/OpenPDU.h" + +#include "snmp.h" +#include "stg/common.h" + +class SNMP_AGENT_CREATOR +{ +private: + SNMP_AGENT * snmpAgent; + +public: + SNMP_AGENT_CREATOR() + : snmpAgent(new SNMP_AGENT()) + { + }; + ~SNMP_AGENT_CREATOR() + { + delete snmpAgent; + }; + + SNMP_AGENT * GetPlugin() + { + return snmpAgent; + }; +}; + +SNMP_AGENT_CREATOR pc; + +int output(const void * buffer, size_t size, void * data) +{ +int * fd = static_cast(data); +return write(*fd, buffer, size); +} + +int SendOpenPDU(int fd, OpenPDU & msg) +{ +asn_enc_rval_t error; + +error = der_encode(&asn_DEF_OpenPDU, &msg, output, &fd); + +if (error.encoded == -1) + { + printfd(__FILE__, "Could not encode OpenPDU (at %s)\n", + error.failed_type ? error.failed_type->name : "unknown"); + return -1; + } +else + { + printfd(__FILE__, "OpenPDU encoded successfully"); + } +return 0; +} + +SNMP_AGENT::SNMP_AGENT() + : PLUGIN(), + running(false), + stopped(true) +{ +pthread_mutex_init(&mutex, NULL); +} + +SNMP_AGENT::~SNMP_AGENT() +{ +pthread_mutex_destroy(&mutex); +} + +int SNMP_AGENT::ParseSettings() +{ +return 0; +} + +int SNMP_AGENT::Start() +{ +return 0; +} + +int SNMP_AGENT::Stop() +{ +return 0; +} + +void * SNMP_AGENT::Runner(void * d) +{ +SNMP_AGENT * snmpAgent = static_cast(d); + +snmpAgent->Run(); + +return NULL; +} + +void SNMP_AGENT::Run() +{ +} diff --git a/projects/stargazer/plugins/other/snmp/snmp.h b/projects/stargazer/plugins/other/snmp/snmp.h new file mode 100644 index 00000000..7f4d67d1 --- /dev/null +++ b/projects/stargazer/plugins/other/snmp/snmp.h @@ -0,0 +1,69 @@ +#ifndef __SNMP_AGENT_H__ +#define __SNMP_AGENT_H__ + +#include + +#include +#include + +#include "stg/os_int.h" +#include "stg/plugin.h" +#include "stg/module_settings.h" +#include "stg/users.h" + +extern "C" PLUGIN * GetPlugin(); + +class USER; +class SETTINGS; +//----------------------------------------------------------------------------- +class SNMP_AGENT_SETTINGS { +public: + SNMP_AGENT_SETTINGS(); + virtual ~SNMP_AGENT_SETTINGS() {} + const std::string & GetStrError() const { return errorStr; } + int ParseSettings(const MODULE_SETTINGS & s); + +private: + mutable std::string errorStr; +}; +//----------------------------------------------------------------------------- +class SNMP_AGENT : public PLUGIN { +public: + SNMP_AGENT(); + virtual ~SNMP_AGENT(); + + void SetUsers(USERS *) {} + void SetTariffs(TARIFFS *) {} + void SetAdmins(ADMINS *) {} + void SetTraffcounter(TRAFFCOUNTER *) {} + void SetStore(STORE *) {} + void SetStgSettings(const SETTINGS *) {} + void SetSettings(const MODULE_SETTINGS &) {} + int ParseSettings(); + + int Start(); + int Stop(); + int Reload() { return 0; } + bool IsRunning() { return running && !stopped; } + + const std::string & GetStrError() const { return errorStr; } + const std::string GetVersion() const { return "Stg SNMP Agent 1.0"; } + uint16_t GetStartPosition() const { return 100; } + uint16_t GetStopPosition() const { return 100; } + +private: + static void * Runner(void * d); + void Run(); + + mutable std::string errorStr; + SNMP_AGENT_SETTINGS snmpAgentSettings; + MODULE_SETTINGS settings; + + pthread_t thread; + pthread_mutex_t mutex; + bool running; + bool stopped; +}; +//----------------------------------------------------------------------------- + +#endif -- 2.44.2