forked from named-data/YaNFD
/
table.go
99 lines (82 loc) · 2.07 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* YaNFD - Yet another NDN Forwarding Daemon
*
* Copyright (C) 2020-2021 Eric Newberry.
*
* This file is licensed under the terms of the MIT License, as found in LICENSE.md.
*/
package face
import (
"sync"
"github.com/eric135/YaNFD/core"
"github.com/eric135/YaNFD/dispatch"
"github.com/eric135/YaNFD/ndn"
)
// FaceTable is the global face table for this forwarder
var FaceTable Table
// Table hold all faces used by the forwarder.
type Table struct {
Faces map[uint64]LinkService
mutex sync.RWMutex
nextFaceID uint64
}
func init() {
FaceTable.Faces = make(map[uint64]LinkService)
FaceTable.nextFaceID = 1
}
// Add adds a face to the face table.
func (t *Table) Add(face LinkService) {
t.mutex.Lock()
defer t.mutex.Unlock()
faceID := uint64(0)
isExistingFaceID := true
for isExistingFaceID {
faceID = t.nextFaceID
t.nextFaceID++
_, isExistingFaceID = t.Faces[faceID]
}
t.Faces[faceID] = face
face.SetFaceID(faceID)
// Add to dispatch
dispatch.AddFace(faceID, face)
core.LogDebug("FaceTable", "Registered FaceID=", faceID)
}
// Get gets the face with the specified ID (if any) from the face table.
func (t *Table) Get(id uint64) LinkService {
t.mutex.RLock()
defer t.mutex.RUnlock()
face, ok := t.Faces[id]
if ok {
return face
}
return nil
}
// GetByURI gets the face with the specified remote URI (if any) from the face table.
func (t *Table) GetByURI(remoteURI *ndn.URI) LinkService {
t.mutex.RLock()
defer t.mutex.RUnlock()
for _, face := range t.Faces {
if face.RemoteURI().String() == remoteURI.String() {
return face
}
}
return nil
}
// GetAll returns points to all faces.
func (t *Table) GetAll() []LinkService {
t.mutex.RLock()
defer t.mutex.RUnlock()
faces := make([]LinkService, 0, len(t.Faces))
for _, face := range t.Faces {
faces = append(faces, face)
}
return faces
}
// Remove removes a face from the face table.
func (t *Table) Remove(id uint64) {
t.mutex.Lock()
defer t.mutex.Unlock()
delete(t.Faces, id)
// Remove from dispatch
dispatch.RemoveFace(id)
core.LogDebug("FaceTable", "Unregistered FaceID=", id)
}