IBR-DTN  1.0.0
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"
25 #include <ibrcommon/thread/MutexLock.h>
26 
27 namespace dtn
28 {
29  namespace data
30  {
31  // set initial sequence number to zero
32  Number PrimaryBlock::__sequencenumber = 0;
33 
34  // set initial absolute sequence number to a random value
35  Number PrimaryBlock::__sequencenumber_abs = dtn::data::Number().random<uint32_t>();
36 
37  // set last assigned time-stamp to zero
38  Timestamp PrimaryBlock::__last_timestamp = 0;
39 
40  // initialize lock for sequence number assignment
41  ibrcommon::Mutex PrimaryBlock::__sequence_lock;
42 
43  PrimaryBlock::PrimaryBlock(bool zero_timestamp)
44  : lifetime(3600), appdatalength(0)
45  {
46  relabel(zero_timestamp);
47 
48  // by default set destination as singleton bit
50  }
51 
53  {
54  }
55 
56  void PrimaryBlock::set(FLAGS flag, bool value)
57  {
58  procflags.setBit(flag, value);
59  }
60 
61  bool PrimaryBlock::get(FLAGS flag) const
62  {
63  return procflags.getBit(flag);
64  }
65 
67  {
68  if (get(PRIORITY_BIT1))
69  {
70  return PRIO_MEDIUM;
71  }
72 
73  if (get(PRIORITY_BIT2))
74  {
75  return PRIO_HIGH;
76  }
77 
78  return PRIO_LOW;
79  }
80 
82  {
83  // set the priority to the real bundle
84  switch (p)
85  {
86  case PRIO_LOW:
87  set(PRIORITY_BIT1, false);
88  set(PRIORITY_BIT2, false);
89  break;
90 
91  case PRIO_HIGH:
92  set(PRIORITY_BIT1, false);
93  set(PRIORITY_BIT2, true);
94  break;
95 
96  case PRIO_MEDIUM:
97  set(PRIORITY_BIT1, true);
98  set(PRIORITY_BIT2, false);
99  break;
100  }
101  }
102 
104  {
105  return get(FRAGMENT);
106  }
107 
109  {
110  set(FRAGMENT, val);
111  }
112 
113  bool PrimaryBlock::operator!=(const PrimaryBlock& other) const
114  {
115  return (const BundleID&)*this != (const BundleID&)other;
116  }
117 
118  bool PrimaryBlock::operator==(const PrimaryBlock& other) const
119  {
120  return (const BundleID&)*this == (const BundleID&)other;
121  }
122 
123  bool PrimaryBlock::operator<(const PrimaryBlock& other) const
124  {
125  return (const BundleID&)*this < other;
126  }
127 
128  bool PrimaryBlock::operator>(const PrimaryBlock& other) const
129  {
130  return (const BundleID&)*this > other;
131  }
132 
133  void PrimaryBlock::relabel(bool zero_timestamp)
134  {
135  if ((dtn::utils::Clock::getRating() > 0.0) && !zero_timestamp)
136  {
138 
139  ibrcommon::MutexLock l(__sequence_lock);
140  if (timestamp > __last_timestamp) {
141  __last_timestamp = timestamp;
142  __sequencenumber = 0;
143  }
144 
145  sequencenumber = __sequencenumber;
146  __sequencenumber++;
147  }
148  else
149  {
150  // set timestamp to zero
151  timestamp = 0;
152 
153  // assign absolute sequence number
154  ibrcommon::MutexLock l(__sequence_lock);
155  sequencenumber = __sequencenumber_abs;
156  __sequencenumber_abs++;
157 
158  // trim sequence-number to 32-bit for compatibility reasons
159  __sequencenumber_abs.trim<uint32_t>();
160  }
161  }
162  }
163 }
void setBit(E flag, const bool &value)
Definition: Number.h:54
Bitset< FLAGS > procflags
Definition: PrimaryBlock.h:118
bool operator<(const PrimaryBlock &other) const
void trim()
Definition: SDNV.h:409
dtn::data::Timestamp timestamp
Definition: BundleID.h:54
bool getBit(E flag) const
Definition: Number.h:66
PrimaryBlock(bool zero_timestamp=false)
void setPriority(PRIORITY p)
PRIORITY getPriority() const
bool operator==(const PrimaryBlock &other) const
dtn::data::Number sequencenumber
Definition: BundleID.h:55
bool get(FLAGS flag) const
bool operator>(const PrimaryBlock &other) const
void setFragment(bool val)
SDNV< E > & random()
Definition: SDNV.h:400
static dtn::data::Timestamp getTime()
Definition: Clock.cpp:167
static double getRating()
Definition: Clock.cpp:59
void relabel(bool zero_timestamp=false)
dtn::data::SDNV< Size > Number
Definition: Number.h:38
dtn::data::SDNV< Size > Timestamp
Definition: Number.h:41
void set(FLAGS flag, bool value)
bool operator!=(const PrimaryBlock &other) const