msgqueue.cpp
来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 639 行 · 第 1/2 页
CPP
639 行
/*_############################################################################ _## _## msgqueue.cpp _## _## SNMP++v3.2.21a _## ----------------------------------------------- _## Copyright (c) 2001-2006 Jochen Katz, Frank Fock _## _## This software is based on SNMP++2.6 from Hewlett Packard: _## _## Copyright (c) 1996 _## Hewlett-Packard Company _## _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. _## Permission to use, copy, modify, distribute and/or sell this software _## and/or its documentation is hereby granted without fee. User agrees _## to display the above copyright notice and this license notice in all _## copies of the software and any documentation of the software. User _## agrees to assume all liability for the use of the software; _## Hewlett-Packard and Jochen Katz make no representations about the _## suitability of this software for any purpose. It is provided _## "AS-IS" without warranty of any kind, either express or implied. User _## hereby grants a royalty-free license to any and all derivatives based _## upon this software code base. _## _## Stuttgart, Germany, Tue Nov 21 22:12:16 CET 2006 _## _##########################################################################*//*=================================================================== Copyright (c) 1999 Hewlett-Packard Company ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS. Permission to use, copy, modify, distribute and/or sell this software and/or its documentation is hereby granted without fee. User agrees to display the above copyright notice and this license notice in all copies of the software and any documentation of the software. User agrees to assume all liability for the use of the software; Hewlett-Packard makes no representations about the suitability of this software for any purpose. It is provided "AS-IS" without warranty of any kind,either express or implied. User hereby grants a royalty-free license to any and all derivatives based upon this software code base. M S G Q U E U E . C P P MSG QUEUE CLASS DECLARATION Description: HPUX version of Snmp class Language: ANSI C++ Author: Peter E Mellquist=====================================================================*/char msgqueue_version[]="#(@) SNMP++ $Id: msgqueue.cpp,v 1.14 2006/10/02 13:49:26 katz Exp $";//-----[ includes ]----------------------------------------------------//----[ snmp++ includes ]----------------------------------------------#include "snmp_pp/msgqueue.h" // queue for holding outstanding messages#include "snmp_pp/snmpmsg.h"#include "snmp_pp/eventlistholder.h"#include "snmp_pp/log.h"#include "snmp_pp/vb.h"#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif#define SNMP_PORT 161//--------[ externs ]---------------------------------------------------extern int send_snmp_request(SnmpSocket sock, unsigned char *send_buf, size_t send_len, Address &address);extern int receive_snmp_response(SnmpSocket sock, Snmp &snmp_session, Pdu &pdu, UdpAddress &fromaddress, OctetStr &engine_id, bool process_msg = true);//----[ CSNMPMessage class ]-------------------------------------------CSNMPMessage::CSNMPMessage(unsigned long id, Snmp * snmp, SnmpSocket socket, const SnmpTarget &target, Pdu &pdu, unsigned char * rawPdu, size_t rawPduLen, const Address & address, snmp_callback callBack, void * callData): m_uniqueId(id), m_snmp(snmp), m_socket(socket), m_pdu(pdu), m_rawPduLen(rawPduLen), m_callBack(callBack), m_callData(callData), m_reason(0), m_received(0){ // reset pdu mvs m_pdu.set_error_index(0); m_pdu.set_error_status(0); m_pdu.set_request_id(m_uniqueId); m_rawPdu = new unsigned char [rawPduLen]; memcpy(m_rawPdu, rawPdu, rawPduLen); m_address = (Address *)address.clone(); m_target = target.clone(); SetSendTime();}CSNMPMessage::~CSNMPMessage(){ delete [] m_rawPdu; delete m_address; delete m_target;}void CSNMPMessage::SetSendTime(){ m_sendTime.refresh(); // Kludge: When this was first designed the units were millisecs // However, later on the units for the target class were changed // to hundreths of secs. Multiply the hundreths of secs by 10 // to create the millisecs which the rest of the objects use. // 11-Dec-95 TM m_sendTime += (m_target->get_timeout() * 10);}int CSNMPMessage::SetPdu(const int reason, const Pdu &pdu, const UdpAddress &fromaddress){ if (Pdu::match_type(m_pdu.get_type(), pdu.get_type()) == false) { LOG_BEGIN(INFO_LOG | 1); LOG("MsgQueue: Response pdu type does not match, pdu is ignored: (id) (type1) (type2)"); LOG(m_uniqueId); LOG(m_pdu.get_type()); LOG(pdu.get_type()); LOG_END; return -1; } unsigned short orig_type = m_pdu.get_type(); if (m_received) { LOG_BEGIN(WARNING_LOG | 1); LOG("MsgQueue: Message is already marked as received (id) (reason) (new reason)"); LOG(m_uniqueId); LOG(reason); LOG(m_reason); LOG_END; // TODO: better check based on error codes if (reason || !m_reason) { LOG_BEGIN(WARNING_LOG | 1); LOG("MsgQueue: ignoring the second pdu"); LOG_END; return 0; } } m_received = 1; m_pdu = pdu; m_reason = reason; if ((orig_type == sNMP_PDU_INFORM) && (m_pdu.get_type() == sNMP_PDU_RESPONSE)) { // remove the first two vbs of the pdu if sysUpTime and notify_id if (m_pdu.get_vb_count() < 2) return 0; const Vb &vb1 = m_pdu.get_vb(0); if (vb1.get_syntax() != sNMP_SYNTAX_TIMETICKS) return 0; if (vb1.get_oid() != SNMP_MSG_OID_SYSUPTIME) return 0; const Vb &vb2 = m_pdu.get_vb(1); if (vb2.get_syntax() != sNMP_SYNTAX_OID) return 0; if (vb2.get_oid() != SNMP_MSG_OID_TRAPID) return 0; TimeTicks timeticks; Oid oid; vb1.get_value(timeticks); m_pdu.set_notify_timestamp(timeticks); vb2.get_value(oid); m_pdu.set_notify_id(oid); m_pdu.delete_vb(1); m_pdu.delete_vb(0); } return 0;}int CSNMPMessage::ResendMessage(){ if (m_received) { // Don't bother to resend if we already have the response SetSendTime(); return SNMP_CLASS_SUCCESS; } if (m_target->get_retry() <= 0) { // This message has timed out Callback(SNMP_CLASS_TIMEOUT); // perform callback with the error return SNMP_CLASS_TIMEOUT; } m_target->set_retry(m_target->get_retry() - 1); SetSendTime(); int status = send_snmp_request(m_socket, m_rawPdu, m_rawPduLen, *m_address); if (status != 0) return SNMP_CLASS_TL_FAILED; return SNMP_CLASS_SUCCESS;}int CSNMPMessage::Callback(const int reason){ snmp_callback tmp_callBack; if (m_callBack) { // prevent callbacks from using this message tmp_callBack = m_callBack; m_callBack = NULL; tmp_callBack(reason, m_snmp, m_pdu, *m_target, m_callData); return 0; } return 1;}//----[ CSNMPMessageQueueElt class ]--------------------------------------CSNMPMessageQueue::CSNMPMessageQueueElt::CSNMPMessageQueueElt( CSNMPMessage *message, CSNMPMessageQueueElt *next, CSNMPMessageQueueElt *previous): m_message(message), m_Next(next), m_previous(previous){ /* Finish insertion into doubly linked list */ if (m_Next) m_Next->m_previous = this; if (m_previous) m_previous->m_Next = this;}CSNMPMessageQueue::CSNMPMessageQueueElt::~CSNMPMessageQueueElt(){ /* Do deletion form doubly linked list */ if (m_Next) m_Next->m_previous = m_previous; if (m_previous) m_previous->m_Next = m_Next; if (m_message) delete m_message;}CSNMPMessage *CSNMPMessageQueue::CSNMPMessageQueueElt::TestId(const unsigned long uniqueId){ if (m_message && (m_message->GetId() == uniqueId)) return m_message; return 0;}//----[ CSNMPMessageQueue class ]--------------------------------------CSNMPMessageQueue::CSNMPMessageQueue(EventListHolder *holder, Snmp *session) : m_head(0, 0, 0), m_msgCount(0), m_idStack(0), m_stackTop(0), m_stackSize(0), my_holder(holder), m_snmpSession(session){ PushId(0);}CSNMPMessageQueue::~CSNMPMessageQueue(){ CSNMPMessageQueueElt *leftOver; lock(); /*--------------------------------------------------------*/ /* walk the list deleting any elements still on the queue */ /*--------------------------------------------------------*/ while ((leftOver = m_head.GetNext())) delete leftOver; if (m_idStack) delete [] m_idStack; unlock();}CSNMPMessage * CSNMPMessageQueue::AddEntry(unsigned long id, Snmp * snmp, SnmpSocket socket, const SnmpTarget &target, Pdu &pdu, unsigned char * rawPdu, size_t rawPduLen, const Address & address, snmp_callback callBack, void * callData){ if (snmp != m_snmpSession) { LOG_BEGIN(WARNING_LOG | 1); LOG("MsgQueue: WARNING: Adding message for other Snmp object."); LOG_END; } CSNMPMessage *newMsg = new CSNMPMessage(id, snmp, socket, target, pdu, rawPdu, rawPduLen, address, callBack, callData); lock(); /*---------------------------------------------------------*/ /* Insert entry at head of list, done automagically by the */ /* constructor function, so don't use the return value. */ /*---------------------------------------------------------*/ (void) new CSNMPMessageQueueElt(newMsg, m_head.GetNext(), &m_head);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?