-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouteTable.h
197 lines (167 loc) · 5.59 KB
/
RouteTable.h
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright 2004-2006 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Modifications made to this file by the patch file dtn2_mfs-33289-1.patch
* are Copyright 2015 United States Government as represented by NASA
* Marshall Space Flight Center. All Rights Reserved.
*
* Released under the NASA Open Source Software Agreement version 1.3;
* You may obtain a copy of the Agreement at:
*
* http://ti.arc.nasa.gov/opensource/nosa/
*
* The subject software is provided "AS IS" WITHOUT ANY WARRANTY of any kind,
* either expressed, implied or statutory and this agreement does not,
* in any manner, constitute an endorsement by government agency of any
* results, designs or products resulting from use of the subject software.
* See the Agreement for the specific language governing permissions and
* limitations.
*/
#ifndef _BUNDLE_ROUTETABLE_H_
#define _BUNDLE_ROUTETABLE_H_
#include <set>
#include <oasys/debug/Log.h>
#include <oasys/util/StringBuffer.h>
#include <oasys/util/StringUtils.h>
#include <oasys/serialize/Serialize.h>
#include "RouteEntry.h"
namespace dtn {
/**
* Class that implements the routing table, implemented
* with an stl vector.
*/
class RouteTable : public oasys::Logger {
public:
/**
* Constructor
*/
RouteTable(const std::string& router_name);
/**
* Destructor
*/
virtual ~RouteTable();
/**
* Add a route entry.
*/
bool add_entry(RouteEntry* entry);
/**
* Remove a route entry.
*/
bool del_entry(const EndpointIDPattern& dest, const LinkRef& next_hop);
/**
* Remove entries that match the given predicate.
*/
template<typename Predicate>
size_t del_matching_entries(Predicate test);
/**
* Remove all entries to the given endpoint id pattern.
*
* @return the number of entries removed
*/
size_t del_entries(const EndpointIDPattern& dest);
/**
* Remove all entries that rely on the given next_hop link
*
* @return the number of entries removed
**/
size_t del_entries_for_nexthop(const LinkRef& next_hop);
/**
* Clear the whole route table.
*/
void clear();
/**
* Fill in the entry_vec with the list of all entries whose
* patterns match the given eid and next hop. If the next hop is
* NULL, it is ignored.
*
* @return the count of matching entries
*/
size_t get_matching(const EndpointID& eid, const LinkRef& next_hop,
RouteEntryVec* entry_vec) const;
/**
* Syntactic sugar to call get_matching for all links.
*
* @return the count of matching entries
*/
size_t get_matching(const EndpointID& eid,
RouteEntryVec* entry_vec) const
{
LinkRef link("RouteTable::get_matching: null");
return get_matching(eid, link, entry_vec);
}
/**
* Dump a string representation of the routing table.
*/
void dump(oasys::StringBuffer* buf) const;
/**
* Return the size of the table.
*/
int size() { return route_table_.size(); }
/**
* Return the routing table. Asserts that the RouteTable
* spin lock is held by the caller.
*/
const RouteEntryVec *route_table();
/**
* Accessor for the RouteTable internal lock.
*/
oasys::Lock* lock() { return &lock_; }
protected:
/// Helper function for get_matching
size_t get_matching_helper(const EndpointID& eid,
const LinkRef& next_hop,
RouteEntryVec* entry_vec,
bool* loop,
int level) const;
/// The routing table itself
RouteEntryVec route_table_;
/**
* Lock to protect internal data structures.
*/
mutable oasys::SpinLock lock_;
};
//----------------------------------------------------------------------
template <typename Predicate>
inline size_t
RouteTable::del_matching_entries(Predicate pred)
{
oasys::ScopeLock l(&lock_, "RouteTable::del_matching_entries");
// XXX/demmer there should be a way to avoid having to go through
// twice but i can't figure it out...
bool found = false;
for (RouteEntryVec::iterator iter = route_table_.begin();
iter != route_table_.end(); ++iter)
{
if (pred(*iter)) {
found = true;
delete *iter;
*iter = NULL;
}
}
// short circuit if nothing was deleted
if (!found)
return 0;
size_t old_size = route_table_.size();
// this stl ugliness first sorts the vector so all the null
// entries are at the end, then cleans them out with erase()
RouteEntryVec::iterator new_end =
std::remove_if(route_table_.begin(), route_table_.end(),
std::bind2nd(std::equal_to<RouteEntry*>(), 0));
route_table_.erase(new_end, route_table_.end());
return old_size - route_table_.size();
}
} // namespace dtn
#endif /* _BUNDLE_ROUTETABLE_H_ */