|
1 /* |
|
2 * Copyright 2011 Trinity College Dublin |
|
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 |
|
21 #include <oasys/thread/SpinLock.h> |
|
22 |
|
23 #include "BPQFragmentList.h" |
|
24 |
|
25 namespace dtn { |
|
26 |
|
27 //---------------------------------------------------------------------- |
|
28 BPQFragmentList::BPQFragmentList(const std::string& name, oasys::SpinLock* lock) |
|
29 : Logger("BPQFragmentList", "/dtn/bpq-frag/list/%s", name.c_str()), |
|
30 name_(name) |
|
31 { |
|
32 if (lock != NULL) { |
|
33 lock_ = lock; |
|
34 own_lock_ = false; |
|
35 } else { |
|
36 lock_ = new oasys::SpinLock(); |
|
37 own_lock_ = true; |
|
38 } |
|
39 } |
|
40 |
|
41 //---------------------------------------------------------------------- |
|
42 BPQFragmentList::~BPQFragmentList() |
|
43 { |
|
44 clear(); |
|
45 if (own_lock_) { |
|
46 delete lock_; |
|
47 } |
|
48 lock_ = NULL; |
|
49 } |
|
50 |
|
51 //---------------------------------------------------------------------- |
|
52 void |
|
53 BPQFragmentList::set_name(const std::string& name) |
|
54 { |
|
55 name_ = name; |
|
56 logpathf("/dtn/bpq-frag/list/%s", name.c_str()); |
|
57 } |
|
58 |
|
59 //---------------------------------------------------------------------- |
|
60 void |
|
61 BPQFragmentList::insert_sorted(BPQFragment* new_fragment) |
|
62 { |
|
63 oasys::ScopeLock l(lock_, "BPQFragmentList::insert_sorted"); |
|
64 |
|
65 iterator iter; |
|
66 for (iter = list_.begin(); |
|
67 iter != list_.end(); |
|
68 ++iter) { |
|
69 |
|
70 if ((*iter)->offset() > new_fragment->offset()) { |
|
71 break; |
|
72 } |
|
73 } |
|
74 list_.insert(iter, new_fragment); |
|
75 } |
|
76 |
|
77 //---------------------------------------------------------------------- |
|
78 bool |
|
79 BPQFragmentList::is_complete(size_t total_len) const |
|
80 { |
|
81 oasys::ScopeLock l(lock_, "BPQFragmentList::is_complete"); |
|
82 |
|
83 size_t gap_start = 0; |
|
84 size_t gap_end = 0; |
|
85 |
|
86 const_iterator iter; |
|
87 for (iter = list_.begin(); |
|
88 iter != list_.end(); |
|
89 ++iter) { |
|
90 |
|
91 gap_end = (*iter)->offset(); |
|
92 |
|
93 if ( gap_end - gap_start != 0) { |
|
94 return false; |
|
95 } |
|
96 |
|
97 gap_start = (*iter)->offset() + (*iter)->length(); |
|
98 } |
|
99 |
|
100 gap_end = total_len; |
|
101 |
|
102 if ( gap_end - gap_start != 0) { |
|
103 return false; |
|
104 } else { |
|
105 return true; |
|
106 } |
|
107 } |
|
108 |
|
109 //---------------------------------------------------------------------- |
|
110 bool |
|
111 BPQFragmentList::requires_fragment ( |
|
112 size_t total_len, |
|
113 size_t frag_start, |
|
114 size_t frag_end) const |
|
115 { |
|
116 oasys::ScopeLock l(lock_, "BPQFragmentList::requires_fragment"); |
|
117 |
|
118 size_t gap_start = 0; |
|
119 size_t gap_end = 0; |
|
120 |
|
121 const_iterator iter; |
|
122 for (iter = list_.begin(); |
|
123 iter != list_.end(); |
|
124 ++iter) { |
|
125 |
|
126 BPQFragment* fragment = *iter; |
|
127 gap_end = fragment->offset(); |
|
128 |
|
129 if ( (gap_start < frag_start && frag_start < gap_end) || |
|
130 (gap_start < frag_end && frag_end < gap_end) ) { |
|
131 return true; |
|
132 } |
|
133 |
|
134 gap_start = fragment->offset() + fragment->length(); |
|
135 } |
|
136 |
|
137 gap_end = total_len; |
|
138 |
|
139 if ( (gap_start < frag_start && frag_start < gap_end) || |
|
140 (gap_start < frag_end && frag_end < gap_end) ) { |
|
141 return true; |
|
142 } else { |
|
143 return false; |
|
144 } |
|
145 } |
|
146 |
|
147 //---------------------------------------------------------------------- |
|
148 void |
|
149 BPQFragmentList::clear() |
|
150 { |
|
151 oasys::ScopeLock l(lock_, "BPQFragmentList::clear"); |
|
152 |
|
153 iterator iter; |
|
154 for (iter = list_.begin(); |
|
155 iter != list_.end(); |
|
156 ++iter) { |
|
157 |
|
158 delete *iter; |
|
159 } |
|
160 } |
|
161 |
|
162 } // namespace dtn |