// Manager class.
// Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Threading;
#pragma warning disable 612,618
namespace GSF.Net.Snmp.Messaging
{
///
/// SNMP manager component that provides SNMP operations.
///
///
/// Drag this component into your form in designer, or create an instance in code.
/// Currently only SNMP v1 and v2c operations are supported.
///
[CLSCompliant(false)]
[Obsolete("Please use Messenger class instead.")]
public sealed class Manager
{
private const int DefaultPort = 161;
private readonly object _locker = new object();
private int _timeout = 5000;
private VersionCode _version;
///
/// Initializes a new instance of the class.
///
public Manager()
{
MaxRepetitions = 10;
}
///
/// Default protocol version for operations.
///
/// By default, the value is SNMP v1.
public VersionCode DefaultVersion
{
get
{
return _version;
}
set
{
lock (_locker)
{
_version = value;
}
}
}
///
/// Timeout value.
///
/// By default, the value is 5,000-milliseconds (5 seconds).
public int Timeout
{
get
{
return _timeout;
}
set
{
Interlocked.Exchange(ref _timeout, value);
}
}
///
/// Gets a variable bind.
///
/// Endpoint.
/// Community name.
/// Variable bind.
///
public Variable GetSingle(IPEndPoint endpoint, string community, Variable variable)
{
List variables = new List { variable };
return Messenger.Get(_version, endpoint, new OctetString(community), variables, _timeout)[0];
}
///
/// Gets a variable bind.
///
/// Address.
/// Community name.
/// Variable bind.
///
public Variable GetSingle(string address, string community, Variable variable)
{
return GetSingle(IPAddress.Parse(address), community, variable);
}
///
/// Gets a variable bind.
///
/// Address.
/// Community name.
/// Variable bind.
///
public Variable GetSingle(IPAddress address, string community, Variable variable)
{
return GetSingle(new IPEndPoint(address, DefaultPort), community, variable);
}
///
/// Gets a list of variable binds.
///
/// Endpoint.
/// Community name.
/// Variable binds.
///
public IList Get(IPEndPoint endpoint, string community, IList variables)
{
return Messenger.Get(_version, endpoint, new OctetString(community), variables, _timeout);
}
///
/// Gets a list of variable binds.
///
/// Address.
/// Community name.
/// Variable binds.
///
public IList Get(string address, string community, IList variables)
{
return Get(IPAddress.Parse(address), community, variables);
}
///
/// Gets a list of variable binds.
///
/// Address.
/// Community name.
/// Variable binds.
///
public IList Get(IPAddress address, string community, IList variables)
{
return Get(new IPEndPoint(address, DefaultPort), community, variables);
}
///
/// Sets a variable bind.
///
/// Endpoint.
/// Community name.
/// Variable bind.
///
public Variable SetSingle(IPEndPoint endpoint, string community, Variable variable)
{
List variables = new List { variable };
return Messenger.Set(_version, endpoint, new OctetString(community), variables, _timeout)[0];
}
///
/// Sets a variable bind.
///
/// Address.
/// Community name.
/// Variable bind.
///
public Variable SetSingle(IPAddress address, string community, Variable variable)
{
return SetSingle(new IPEndPoint(address, DefaultPort), community, variable);
}
///
/// Sets a variable bind.
///
/// Address.
/// Community name.
/// Variable bind.
///
public Variable SetSingle(string address, string community, Variable variable)
{
return SetSingle(IPAddress.Parse(address), community, variable);
}
///
/// Sets a list of variable binds.
///
/// Endpoint.
/// Community name.
/// Variable binds.
///
public IList Set(IPEndPoint endpoint, string community, IList variables)
{
return Messenger.Set(_version, endpoint, new OctetString(community), variables, _timeout);
}
///
/// Sets a list of variable binds.
///
/// Address.
/// Community name.
/// Variable binds.
///
public IList Set(string address, string community, IList variables)
{
return Set(IPAddress.Parse(address), community, variables);
}
///
/// Sets a list of variable binds.
///
/// Address.
/// Community name.
/// Variable binds.
///
public IList Set(IPAddress address, string community, IList variables)
{
return Set(new IPEndPoint(address, DefaultPort), community, variables);
}
///
/// Gets a table of variables.
///
/// Endpoint.
/// Community name.
/// Table OID.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", MessageId = "Return", Justification = "ByDesign")]
public Variable[,] GetTable(IPEndPoint endpoint, string community, ObjectIdentifier table)
{
return Messenger.GetTable(DefaultVersion, endpoint, new OctetString(community), table, Timeout, MaxRepetitions);
}
///
/// Gets or sets the max repetitions for GET BULK operations.
///
/// The max repetitions.
public int MaxRepetitions { get; set; }
///
/// Gets a table of variables.
///
/// Address.
/// Community name.
/// Table OID.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", MessageId = "Return", Justification = "ByDesign")]
public Variable[,] GetTable(IPAddress address, string community, ObjectIdentifier table)
{
return GetTable(new IPEndPoint(address, DefaultPort), community, table);
}
///
/// Gets a table of variables.
///
/// Address.
/// Community name.
/// Table OID.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", MessageId = "Return", Justification = "ByDesign")]
public Variable[,] GetTable(string address, string community, ObjectIdentifier table)
{
return GetTable(IPAddress.Parse(address), community, table);
}
///
/// Returns a that represents this .
///
///
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "SNMP manager: timeout: {0}; version: {1}", Timeout.ToString(CultureInfo.InvariantCulture), DefaultVersion);
}
}
}
#pragma warning restore 612,618