Quantcast
Channel: C# Based Open Source SNMP Library for .NET and Mono
Viewing all articles
Browse latest Browse all 576

New Post: Issue creating INFORM listener on a worker thread

$
0
0
Hi,

First off, many thanks for an excellent library - I've only been working with it for a couple of weeks but it's proven extremely easy to use.

I'm experiencing a strange issue in regard to receiving INFORMs with the library. I'm currently developing two pieces of software - one in C# using #SNMP and one in Java using SNMP4J. The C# component is an extension to a pre-existing program and is implementing the INFORM listener, the Java component is a new development and is sending the INFORM messages.

I've done some testing with the following configurations:

(NET-SNMP) snmptrapd receiving messages from the java code - works fine
c# code receiving messages from (NET-SNMP) snmptrap - works fine
c# code receiving messages from the java code - doesn't work

The c# code is based on the sample snmptrapd.cs. Now, here's the strange thing. I create a simple test listener using the following code:
internal static IUnityContainer Container { get; private set; }

        static void Main(string[] args)
        {
            Container = new UnityContainer().LoadConfiguration("snmptrapd");
            var users = Container.Resolve<UserRegistry>();
            users.Add(new OctetString("??????????"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("??????????"))));

            var trapv1 = Container.Resolve<TrapV1MessageHandler>("TrapV1Handler");
            trapv1.MessageReceived += WatcherTrapV1Received;
            var trapv2 = Container.Resolve<TrapV2MessageHandler>("TrapV2Handler");
            trapv2.MessageReceived += WatcherTrapV2Received;
            var inform = Container.Resolve<InformRequestMessageHandler>("InformHandler");
            inform.MessageReceived += WatcherInformRequestReceived;
            using (var engine = Container.Resolve<SnmpEngine>())
            {
                engine.ExceptionRaised += new EventHandler<Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs>(engine_ExceptionRaised);
                IPEndPoint endPoint = new IPEndPoint(new IPAddress(new byte[]{127,0,0,1}), 162);
                engine.Listener.AddBinding(endPoint);
                engine.Listener.ExceptionRaised += new EventHandler<Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs>(Listener_ExceptionRaised);
                engine.Start();
                Console.WriteLine("Bound on " + endPoint.Address + ":" + endPoint.Port);
                Console.WriteLine("#SNMP is available at http://sharpsnmplib.codeplex.com");
                Console.WriteLine("Press any key to stop . . . ");
                Console.Read();
                engine.Stop();
            }
        }

        static void Listener_ExceptionRaised(object sender, Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs e)
        {
            Console.WriteLine(e.Exception.ToString());
        }

        static void engine_ExceptionRaised(object sender, Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs e)
        {
            Console.WriteLine("Exception: " + e.Exception.ToString());
        }

        private static void WatcherInformRequestReceived(object sender, InformRequestMessageReceivedEventArgs e)
        {
            Console.WriteLine("Inform message received");
            Console.WriteLine(e.InformRequestMessage);
        }

        private static void WatcherTrapV2Received(object sender, TrapV2MessageReceivedEventArgs e)
        {
            Console.WriteLine("Trap V2 message received");
            Console.WriteLine(e.TrapV2Message);
        }

        private static void WatcherTrapV1Received(object sender, TrapV1MessageReceivedEventArgs e)
        {
            Console.WriteLine("Trap V1 message received");
            Console.WriteLine(e.TrapV1Message);
        }
This works fine with the Java code. I place (almost) exactly the same code in the original C# project, and it doesn't work. The changes are twofold:
  1. The program is designed to be unattended, so the keypress stuff was removed, and instead, we perform:
                while (_running)
                    System.Threading.Thread.Sleep(500);
after engine.Start(). At the moment, the variable always has the value true, so this is in effect an infinite loop (although in the future we will need to be able to change it to stop the listener).
  1. Rather than running on the main thread, the code is wrapped in a void Start() method and started on a new thread:
                ThreadStart ts = () =>
                {
                    listener.Start();
                };
                Thread thread = new Thread(ts);
                thread.Name = listener.Name;
                thread.Start();
This is the only thread that utilizes #SNMP objects and there is only ever a single instance of the thread, simply to isolate it from the rest of the work that the program is doing.

I know the library isn't guaranteed to be thread-safe, but I assumed that we would be alright using it on a thread as long as we guaranteed that only that one thread used the library.

Any ideas on what might be going wrong? We're using TritonMate.

Thanks,

Austen

Viewing all articles
Browse latest Browse all 576

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>