|
1 /* |
|
2 * Copyright 2007 Baylor University |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 #include <dtn-config.h> |
|
19 #endif |
|
20 #include "ProphetLinkList.h" |
|
21 |
|
22 namespace dtn |
|
23 { |
|
24 |
|
25 LinkRef |
|
26 ProphetLinkList::NULL_LINK("ProphetLinkList"); |
|
27 |
|
28 ProphetLinkList::ProphetLinkList() |
|
29 {} |
|
30 |
|
31 ProphetLinkList::~ProphetLinkList() |
|
32 { |
|
33 clear(); |
|
34 } |
|
35 |
|
36 void |
|
37 ProphetLinkList::add(const LinkRef& l) |
|
38 { |
|
39 iterator i; |
|
40 if (! find(l->remote_eid().c_str(),i)) |
|
41 { |
|
42 ProphetLink* lr = new ProphetLink(l); |
|
43 list_.insert(i,lr); |
|
44 } |
|
45 } |
|
46 |
|
47 void |
|
48 ProphetLinkList::del(const LinkRef& l) |
|
49 { |
|
50 iterator i; |
|
51 if (find(l->remote_eid().c_str(),i)) |
|
52 { |
|
53 delete *i; |
|
54 list_.erase(i); |
|
55 } |
|
56 } |
|
57 |
|
58 const prophet::Link* |
|
59 ProphetLinkList::find(const char* remote_eid) const |
|
60 { |
|
61 iterator i; |
|
62 ProphetLinkList* me = const_cast<ProphetLinkList*>(this); |
|
63 if (me != NULL && me->find(remote_eid,i)) |
|
64 return (*i); |
|
65 return NULL; |
|
66 } |
|
67 |
|
68 const LinkRef& |
|
69 ProphetLinkList::find_ref(const prophet::Link* link) const |
|
70 { |
|
71 if (link == NULL) return NULL_LINK; |
|
72 return find_ref(link->remote_eid()); |
|
73 } |
|
74 |
|
75 const LinkRef& |
|
76 ProphetLinkList::find_ref(const char* remote_eid) const |
|
77 { |
|
78 iterator i; |
|
79 ProphetLinkList* me = const_cast<ProphetLinkList*>(this); |
|
80 if (me != NULL && me->find(remote_eid,i)) |
|
81 return (*i)->ref(); |
|
82 return NULL_LINK; |
|
83 } |
|
84 |
|
85 void |
|
86 ProphetLinkList::clear() |
|
87 { |
|
88 while (!list_.empty()) |
|
89 { |
|
90 delete list_.front(); |
|
91 list_.pop_front(); |
|
92 } |
|
93 } |
|
94 |
|
95 bool |
|
96 ProphetLinkList::find(const char* remote_eid, iterator& i) |
|
97 { |
|
98 i = list_.begin(); |
|
99 while (i != list_.end() && |
|
100 (strncmp((*i)->remote_eid(),remote_eid,strlen((*i)->remote_eid())) < 0) ) |
|
101 i++; |
|
102 if (i == list_.end()) return false; |
|
103 return strncmp((*i)->remote_eid(),remote_eid,strlen((*i)->remote_eid())) == 0; |
|
104 } |
|
105 |
|
106 }; // namespace dtn |