IBR-DTNSuite  0.10
CustodySignalBlock.cpp
Go to the documentation of this file.
1 /*
2  * CustodySignalBlock.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 
22 #include "ibrdtn/data/Bundle.h"
25 #include <stdlib.h>
26 #include <sstream>
27 
28 namespace dtn
29 {
30  namespace data
31  {
33  : AdministrativeBlock(32), custody_accepted(false), reason(NO_ADDITIONAL_INFORMATION),
34  fragment_length(0), timeofsignal()
35  {
36  }
37 
39  {
40  }
41 
43  {
44  ibrcommon::BLOB::Reference r = p.getBLOB();
46  (*stream).get(_admfield);
47 
48  // check type field
49  if ((_admfield >> 4) != 2) throw WrongRecordException();
50 
51  char status = 0;
52  (*stream).get(status);
53 
54  // decode custody acceptance
55  custody_accepted = (status & 0x01);
56 
57  // decode reason flag
58  reason = REASON_CODE(status >> 1);
59 
60  if ( refsFragment() )
61  {
62  bundleid.fragment = true;
63 
64  (*stream) >> bundleid.offset;
65  (*stream) >> fragment_length;
66  }
67 
68  (*stream) >> timeofsignal;
69  (*stream) >> bundleid.timestamp;
70  (*stream) >> bundleid.sequencenumber;
71 
72  BundleString source;
73  (*stream) >> source;
74  bundleid.source = EID(source);
75  }
76 
78  {
81 
82  // clear the whole data first
83  stream.clear();
84 
85  // write the content
86  (*stream).put(_admfield);
87 
88  // encode reason flag
89  char status = static_cast<char>(reason << 1);
90 
91  // encode custody acceptance
92  if (custody_accepted) status |= 0x01;
93 
94  // write the status byte
95  (*stream).put(status);
96 
97  if ( refsFragment() )
98  {
99  (*stream) << bundleid.offset;
100  (*stream) << fragment_length;
101  }
102 
104 
105  (*stream) << timeofsignal
108  << sourceid;
109  }
110 
112  {
113  // set bundle parameter
114  if (other.get(Bundle::FRAGMENT))
115  {
116  bundleid.offset = other.offset;
118 
119  setFragment(true);
120  bundleid.fragment = true;
121  }
122 
123  bundleid.timestamp = other.timestamp;
125  bundleid.source = other.source;
126  }
127 
129  {
130  // set bundle parameter
131  if (other.get(Bundle::FRAGMENT))
132  {
135 
136  setFragment(true);
137  bundleid.fragment = true;
138  }
139 
140  bundleid.timestamp = other.timestamp;
142  bundleid.source = other.source;
143  }
144 
145  bool CustodySignalBlock::match(const Bundle& other) const
146  {
147  if (bundleid.timestamp != other.timestamp) return false;
148  if (bundleid.sequencenumber != other.sequencenumber) return false;
149  if (bundleid.source != other.source) return false;
150 
151  // set bundle parameter
152  if (other.get(Bundle::FRAGMENT))
153  {
154  if (!bundleid.fragment) return false;
155  if (bundleid.offset != other.fragmentoffset) return false;
156  if (fragment_length != other.appdatalength) return false;
157  }
158 
159  return true;
160  }
161 
162  }
163 }