IBR-DTNSuite  0.10
PrimaryBlock.cpp
Go to the documentation of this file.
1 /*
2  * PrimaryBlock.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
23 #include "ibrdtn/data/Exceptions.h"
24 #include "ibrdtn/utils/Clock.h"
26 
27 namespace dtn
28 {
29  namespace data
30  {
31  Number PrimaryBlock::__sequencenumber = 0;
32  Timestamp PrimaryBlock::__last_timestamp = 0;
33  ibrcommon::Mutex PrimaryBlock::__sequence_lock;
34 
36  : timestamp(0), sequencenumber(0), lifetime(3600), fragmentoffset(0), appdatalength(0)
37  {
38  relabel();
39 
40  // by default set destination as singleton bit
41  set(DESTINATION_IS_SINGLETON, true);
42  }
43 
45  {
46  }
47 
48  void PrimaryBlock::set(FLAGS flag, bool value)
49  {
50  procflags.setBit(flag, value);
51  }
52 
53  bool PrimaryBlock::get(FLAGS flag) const
54  {
55  return procflags.getBit(flag);
56  }
57 
59  {
60  if (get(PRIORITY_BIT1))
61  {
62  return PRIO_MEDIUM;
63  }
64 
65  if (get(PRIORITY_BIT2))
66  {
67  return PRIO_HIGH;
68  }
69 
70  return PRIO_LOW;
71  }
72 
74  {
75  // set the priority to the real bundle
76  switch (p)
77  {
78  case PRIO_LOW:
79  set(PRIORITY_BIT1, false);
80  set(PRIORITY_BIT2, false);
81  break;
82 
83  case PRIO_HIGH:
84  set(PRIORITY_BIT1, false);
85  set(PRIORITY_BIT2, true);
86  break;
87 
88  case PRIO_MEDIUM:
89  set(PRIORITY_BIT1, true);
90  set(PRIORITY_BIT2, false);
91  break;
92  }
93  }
94 
95  bool PrimaryBlock::operator!=(const PrimaryBlock& other) const
96  {
97  return !((*this) == other);
98  }
99 
100  bool PrimaryBlock::operator==(const PrimaryBlock& other) const
101  {
102  if (other.timestamp != timestamp) return false;
103  if (other.sequencenumber != sequencenumber) return false;
104  if (other.source != source) return false;
105  if (other.get(PrimaryBlock::FRAGMENT) != get(PrimaryBlock::FRAGMENT)) return false;
106 
107  if (get(PrimaryBlock::FRAGMENT))
108  {
109  if (other.fragmentoffset != fragmentoffset) return false;
110  if (other.appdatalength != appdatalength) return false;
111  }
112 
113  return true;
114  }
115 
116  bool PrimaryBlock::operator<(const PrimaryBlock& other) const
117  {
118  if (source < other.source) return true;
119  if (source != other.source) return false;
120 
121  if (timestamp < other.timestamp) return true;
122  if (timestamp != other.timestamp) return false;
123 
124  if (sequencenumber < other.sequencenumber) return true;
125  if (sequencenumber != other.sequencenumber) return false;
126 
127  if (other.get(PrimaryBlock::FRAGMENT))
128  {
129  if (!get(PrimaryBlock::FRAGMENT)) return true;
130  return (fragmentoffset < other.fragmentoffset);
131  }
132 
133  return false;
134  }
135 
136  bool PrimaryBlock::operator>(const PrimaryBlock& other) const
137  {
138  return !(((*this) < other) || ((*this) == other));
139  }
140 
142  {
144  }
145 
146  std::string PrimaryBlock::toString() const
147  {
148  return dtn::data::BundleID(*this).toString();
149  }
150 
152  {
154  {
155  timestamp = 0;
156  }
157  else
158  {
160  }
161 
162  ibrcommon::MutexLock l(__sequence_lock);
163  if (timestamp > __last_timestamp) {
164  __last_timestamp = timestamp;
165  __sequencenumber = 0;
166  }
167 
168  sequencenumber = __sequencenumber;
169  __sequencenumber++;
170  }
171  }
172 }