PowerSNMP for .NET
Replace a Value in the Agent's Response



The following example demonstrates how to respond to Get requests for values maintained outside of Agent.Variables. This can be useful in applications where the data source changes frequently.

C#
Copy Code
private void Form1_Load(object sender, EventArgs e)
{
    //Add a placeholder variable (ifNumber here representing any variable maintained outside of Agent.Variables)
    agent1.Variables.Add(new Variable(agent1.Mib.GetByNodeName(NodeName.ifNumber), "0"));
    //Start listening for requests
    agent1.Start(agent1_MessageReceived, null);
}

private void agent1_MessageReceived(Agent agent, RequestMessage request, object state)
{
    //Create response
    ResponseMessage response = agent1.CreateResponse(request);

    //If the variable was requested, replace its value in the response
    foreach (Variable vari in response.Variables)
        if (vari.Id == agent1.Mib.GetByNodeName(NodeName.ifNumber).GetIid())
            vari.Value = new Dart.Snmp.SimpleType.Integer(myValue);

    agent1.Send(response, request.Origin);
}
Visual Basic
Copy Code
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    'Add a placeholder variable (ifNumber here representing any variable maintained outside of Agent.Variables)
    agent1.Variables.Add(New Variable(agent1.Mib.GetByNodeName(NodeName.ifNumber), "0"))
    'Start listening for requests
    agent1.Start(AddressOf agent1_MessageReceived, Nothing)
End Sub

Private Sub agent1_MessageReceived(ByVal agent As Agent, ByVal request As RequestMessage, ByVal state As Object)
    'Create response
    Dim response As ResponseMessage = agent1.CreateResponse(request)

    'If the variable was requested, replace its value in the response
    For Each vari As Variable In response.Variables
        If vari.Id = agent1.Mib.GetByNodeName(NodeName.ifNumber).GetIid() Then
            vari.Value = New Dart.Snmp.SimpleType.Integer(myValue)
        End If
    Next vari

    agent1.Send(response, request.Origin)
End Sub

PowerSNMP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic