// Data factory type.
// 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.
/*
* Created by SharpDevelop.
* User: lextm
* Date: 2008/4/30
* Time: 19:49
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Globalization;
using System.IO;
namespace GSF.Net.Snmp
{
///
/// Factory that creates instances.
///
public static class DataFactory
{
///
/// Creates an instance from buffer.
///
/// Buffer
///
public static ISnmpData CreateSnmpData(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
return CreateSnmpData(buffer, 0, buffer.Length);
}
///
/// Creates an instance from stream.
///
/// Stream.
/// Type code.
///
public static ISnmpData CreateSnmpData(int type, Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
Tuple length = stream.ReadPayloadLength();
try
{
switch ((SnmpType)type)
{
case SnmpType.Counter32:
return new Counter32(length, stream);
case SnmpType.Counter64:
return new Counter64(length, stream);
case SnmpType.Gauge32:
return new Gauge32(length, stream);
case SnmpType.ObjectIdentifier:
return new ObjectIdentifier(length, stream);
case SnmpType.Null:
return new Null(length, stream);
case SnmpType.NoSuchInstance:
return new NoSuchInstance(length, stream);
case SnmpType.NoSuchObject:
return new NoSuchObject(length, stream);
case SnmpType.EndOfMibView:
return new EndOfMibView(length, stream);
case SnmpType.Integer32:
return new Integer32(length, stream);
case SnmpType.OctetString:
return new OctetString(length, stream);
case SnmpType.IPAddress:
return new IP(length, stream);
case SnmpType.TimeTicks:
return new TimeTicks(length, stream);
case SnmpType.Sequence:
return new Sequence(length, stream);
case SnmpType.TrapV1Pdu:
return new TrapV1Pdu(length, stream);
case SnmpType.TrapV2Pdu:
return new TrapV2Pdu(length, stream);
case SnmpType.GetRequestPdu:
return new GetRequestPdu(length, stream);
case SnmpType.ResponsePdu:
return new ResponsePdu(length, stream);
case SnmpType.GetBulkRequestPdu:
return new GetBulkRequestPdu(length, stream);
case SnmpType.GetNextRequestPdu:
return new GetNextRequestPdu(length, stream);
case SnmpType.SetRequestPdu:
return new SetRequestPdu(length, stream);
case SnmpType.InformRequestPdu:
return new InformRequestPdu(length, stream);
case SnmpType.ReportPdu:
return new ReportPdu(length, stream);
case SnmpType.Opaque:
return new Opaque(length, stream);
case SnmpType.EndMarker:
return null;
case SnmpType.Unsigned32:
// IMPORTANT: return Gauge32 for Unsigned32 case as workaround of RFC 1442 time entities.
return new Gauge32(length, stream);
default:
throw new SnmpException(string.Format(CultureInfo.InvariantCulture, "unsupported data type: {0}", (SnmpType)type));
}
}
catch (Exception ex)
{
if (ex is SnmpException)
{
throw;
}
throw new SnmpException("data construction exception", ex);
}
}
///
/// Creates an instance from buffer.
///
/// Buffer
/// Index
/// Count
///
public static ISnmpData CreateSnmpData(byte[] buffer, int index, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
using (MemoryStream m = new MemoryStream(buffer, index, count, false))
{
return CreateSnmpData(m);
}
}
///
/// Creates an instance from stream.
///
/// Stream
///
public static ISnmpData CreateSnmpData(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
return CreateSnmpData(stream.ReadByte(), stream);
}
}
}