예제 #1
0
        /// <summary>
        /// 带有异步回调的GetResponse;
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="PduList"></param>
        /// <param name="Community"></param>
        /// <param name="IpAddr"></param>
        public override void GetRequest(AsyncCallback callback, List <string> PduList, string Community, string IpAddr)
        {
            // 当确认全部获取SNMP数据后,调用callback回调;
            SnmpMessageResult           res       = new SnmpMessageResult();
            OctetString                 community = new OctetString(Community);
            AgentParameters             param     = new AgentParameters(community);
            Dictionary <string, string> rest      = new Dictionary <string, string>();

            param.Version = SnmpVersion.Ver2;

            // 创建代理(基站);
            UdpTarget target = ConnectToAgent(Community, IpAddr);

            // Pdu请求形式Get;
            Pdu pdu = new Pdu(PduType.Get);

            foreach (string pdulist in PduList)
            {
                pdu.VbList.Add(pdulist);
            }

            Task tsk = Task.Factory.StartNew(() => {
                // 接收结果;
                m_Result = (SnmpV2Packet)target.Request(pdu, param);

                if (m_Result != null)
                {
                    // ErrorStatus other then 0 is an error returned by
                    // the Agent - see SnmpConstants for error definitions
                    if (m_Result.Pdu.ErrorStatus != 0)
                    {
                        // agent reported an error with the request
                        Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                          m_Result.Pdu.ErrorStatus,
                                          m_Result.Pdu.ErrorIndex);

                        rest.Add(m_Result.Pdu.ErrorIndex.ToString(), m_Result.Pdu.ErrorStatus.ToString());
                        res.SetSNMPReslut(rest);
                        Thread.Sleep(3111);
                        callback(res);
                    }
                    else
                    {
                        for (int i = 0; i < m_Result.Pdu.VbCount; i++)
                        {
                            rest.Add(m_Result.Pdu.VbList[i].Oid.ToString(), m_Result.Pdu.VbList[i].Value.ToString());
                            res.SetSNMPReslut(rest);
                            Thread.Sleep(3111);
                            callback(res);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No response received from SNMP agent.");
                }

                target.Close();
            });
        }
예제 #2
0
        /// <summary>
        /// GetNext的对外接口
        /// 该函数会在第一次收到客户端请求后;
        /// 在每次收到基站的GetResponse之后,都会调用客户端注册的回调函数
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="PduList"></param>
        public void GetNextRequestWhenStop(AsyncCallback callback, AsyncCallback callback_WhenStop, List <string> PduList)
        {
            SnmpMessageResult res      = new SnmpMessageResult();
            List <string>     templist = new List <string>();

            templist = PduList;

            if ((PduList.Count == 0) || (PduList == null))      // 如果PduList内容为空,则不进行处理;
            {
                return;
            }

            {
                Dictionary <string, string> NextRest = new Dictionary <string, string>();
                List <List <string> >       AllList  = new List <List <string> >();
                bool GetNextorNot = true;

                AllList.Add(templist);
                while (GetNextorNot)                                 // 持续获取,直到最后一个结果;
                {
                    NextRest = this.GetNext(ref templist);           // 得到返回结果;
                    if (templist.Count == 0)                         // 当返回结果为空时,停止GetNext;
                    {
                        GetNextorNot    = false;
                        res.IsCompleted = true;
                        callback_WhenStop(res);
                    }
                    res.SetSNMPReslut(NextRest);
                    callback(res);
                }
                return;
            }
        }
예제 #3
0
        /// <summary>
        /// GetNext的对外接口
        /// 该函数会在第一次收到客户端请求后;
        /// 在每次收到基站的GetResponse之后,都会调用客户端注册的回调函数
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="PduList"></param>
        public override void GetNextRequest(AsyncCallback callback, List <string> PduList)
        {
            SnmpMessageResult res = new SnmpMessageResult();

            if ((PduList.Count == 0) && (PduList == null))      // 如果PduList内容为空,则不进行处理;
            {
                return;
            }
            Task tsk = Task.Factory.StartNew(() =>
            {
                Dictionary <string, string> NextRest = new Dictionary <string, string>();
                List <List <string> > AllList        = new List <List <string> >();
                bool GetNextorNot = true;

                AllList.Add(PduList);
                while (GetNextorNot)                                // 持续获取,知道最后一个结果;
                {
                    NextRest = this.GetNext(ref PduList);           // 得到返回结果;
                    if (PduList.Count == 0)                         // 当返回结果为空时,停止GetNext;
                    {
                        GetNextorNot = false;
                    }
                    res.SetSNMPReslut(NextRest);
                    callback(res);
                }
                return;
            });
        }