ln.snmp/types/Sequence.cs

104 lines
2.6 KiB
C#

// /**
// * File: Sequence.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
namespace ln.snmp.types
{
public class Sequence : AbstractSequence
{
private List<Variable> items = new List<Variable>();
public Sequence()
:base(new Identifier(IdentifierClass.UNIVERSAL,true,0x10))
{ }
public Sequence(IEnumerable<Variable> variables)
:this()
{
foreach (Variable variable in variables)
Add(variable);
}
public override Variable[] Items => items.ToArray();
public override void Add(Variable item)
{
this.items.Add(item);
}
public override void Remove(Variable item)
{
this.items.Remove(item);
}
public override void Remove(int n)
{
this.items.RemoveAt(n);
}
}
public abstract class AbstractSequence : Variable
{
protected AbstractSequence(Identifier identifier)
:base(identifier)
{
}
/* Items */
public abstract Variable[] Items { get; }
public abstract void Add(Variable item);
public abstract void Remove(Variable item);
public virtual void Remove(int n)
{
Remove(Items[n]);
}
public virtual void RemoveAll()
{
foreach (Variable item in Items)
Remove(item);
}
/* SNMP Variable */
public override object Value {
get => Items;
set => throw new NotImplementedException();
}
public override byte[] Bytes
{
get
{
MemoryStream payload = new MemoryStream();
foreach (Variable item in Items)
{
item.Write(payload);
}
return payload.ToArray();
}
set
{
MemoryStream bytes = new MemoryStream(value);
while (bytes.Position < bytes.Length)
{
Add(Variable.Read(bytes));
}
}
}
public override string ToString()
{
return string.Format("[Sequence {0}]",String.Join(", ",Items.Select((x) => x.ToString())));
}
}
}