diff options
| author | Felix Hanley <felix@userspace.com.au> | 2018-02-09 08:42:22 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2018-02-09 08:42:22 +0000 |
| commit | 2ded0704c8f675c3d92cf2b4874a32c65faf2553 (patch) | |
| tree | c3b437cbe7129ea300d7a98e438b0f5fd3fd8344 /dht/routing_table.go | |
| parent | be4f4b36cc7fb46c70364d524d64c7188c1ae0f3 (diff) | |
| download | dhtsearch-2ded0704c8f675c3d92cf2b4874a32c65faf2553.tar.gz dhtsearch-2ded0704c8f675c3d92cf2b4874a32c65faf2553.tar.bz2 | |
Basic DHT functions
Diffstat (limited to 'dht/routing_table.go')
| -rw-r--r-- | dht/routing_table.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/dht/routing_table.go b/dht/routing_table.go new file mode 100644 index 0000000..0252519 --- /dev/null +++ b/dht/routing_table.go @@ -0,0 +1,57 @@ +package dht + +import ( + "net" + "sync" +) + +// Keep it simple for now +type routingTable struct { + id Infohash + address net.UDPAddr + nodes []*remoteNode + max int + sync.Mutex +} + +func newRoutingTable(id Infohash) *routingTable { + k := &routingTable{id: id, max: 4000} + k.refresh() + return k +} + +func (k *routingTable) add(rn *remoteNode) { + k.Lock() + defer k.Unlock() + + // Check IP and ports are valid and not self + if (rn.address.String() == k.address.String() && rn.address.Port == k.address.Port) || !rn.id.Valid() || rn.id.Equal(k.id) { + return + } + k.nodes = append(k.nodes, rn) +} + +func (k *routingTable) getNodes() []*remoteNode { + k.Lock() + defer k.Unlock() + return k.nodes +} + +func (k *routingTable) isEmpty() bool { + k.Lock() + defer k.Unlock() + return len(k.nodes) == 0 +} + +func (k *routingTable) isFull() bool { + k.Lock() + defer k.Unlock() + return len(k.nodes) >= k.max +} + +// For now +func (k *routingTable) refresh() { + k.Lock() + defer k.Unlock() + k.nodes = make([]*remoteNode, 0) +} |
