77 lines
1.8 KiB
C#
77 lines
1.8 KiB
C#
using System;
|
|
using ln.types.btree;
|
|
using ln.types.net;
|
|
namespace skyspot.ip
|
|
{
|
|
public class IPPool<T> where T : IComparable
|
|
{
|
|
public IPv4 FirstIP { get; set; }
|
|
public IPv4 LastIP { get; set; }
|
|
|
|
public IPv4 NextIP { get; set;}
|
|
|
|
BTree<IPv4, T> forward = new BTree<IPv4, T>();
|
|
BTree<T, IPv4> reverse = new BTree<T, IPv4>();
|
|
|
|
public IPPool()
|
|
{
|
|
}
|
|
|
|
public T FindForward(IPv4 ip)
|
|
{
|
|
lock (this)
|
|
{
|
|
return forward[ip];
|
|
}
|
|
}
|
|
public IPv4 FindReverse(T lookup)
|
|
{
|
|
lock (this)
|
|
{
|
|
return reverse[lookup];
|
|
}
|
|
}
|
|
|
|
public void Assign(IPv4 ip, T assigned)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (forward.ContainsKey(ip) || reverse.ContainsKey(assigned))
|
|
throw new ArgumentException("<ip> or <assigend> already in pool");
|
|
forward.Add(ip, assigned);
|
|
reverse.Add(assigned, ip);
|
|
}
|
|
}
|
|
public void Remove(IPv4 ip)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (forward.ContainsKey(ip))
|
|
{
|
|
reverse.Remove(forward[ip]);
|
|
forward.Remove(ip);
|
|
}
|
|
}
|
|
}
|
|
public void RemoveAssigned(T assigned)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (reverse.ContainsKey(assigned))
|
|
{
|
|
forward.Remove(reverse[assigned]);
|
|
reverse.Remove(assigned);
|
|
}
|
|
}
|
|
}
|
|
|
|
public IPv4 Allocate(T assigned)
|
|
{
|
|
if (reverse.ContainsKey(assigned))
|
|
return reverse[assigned];
|
|
|
|
}
|
|
|
|
}
|
|
}
|