|
1 /* |
|
2 * Copyright 2006 Intel Corporation |
|
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 "APIBlockProcessor.h" |
|
22 |
|
23 #include "BlockInfo.h" |
|
24 #include "BundleProtocol.h" |
|
25 |
|
26 namespace dtn { |
|
27 |
|
28 template <> APIBlockProcessor* |
|
29 oasys::Singleton<APIBlockProcessor>::instance_ = NULL; |
|
30 |
|
31 //---------------------------------------------------------------------- |
|
32 APIBlockProcessor::APIBlockProcessor() : |
|
33 BlockProcessor(BundleProtocol::API_EXTENSION_BLOCK) |
|
34 { |
|
35 } |
|
36 |
|
37 //---------------------------------------------------------------------- |
|
38 int |
|
39 APIBlockProcessor::consume(Bundle* bundle, |
|
40 BlockInfo* block, |
|
41 u_char* buf, |
|
42 size_t len) |
|
43 { |
|
44 (void)bundle; |
|
45 (void)block; |
|
46 (void)buf; |
|
47 (void)len; |
|
48 |
|
49 return -1; |
|
50 } |
|
51 |
|
52 //---------------------------------------------------------------------- |
|
53 int |
|
54 APIBlockProcessor::generate(const Bundle* bundle, |
|
55 BlockInfoVec* xmit_blocks, |
|
56 BlockInfo* block, |
|
57 const LinkRef& link, |
|
58 bool last) |
|
59 { |
|
60 (void)bundle; |
|
61 (void)link; |
|
62 |
|
63 // there must be a corresponding source block created at the API |
|
64 const BlockInfo* source = block->source(); |
|
65 ASSERT(source != NULL); |
|
66 ASSERT(source->owner() == this); |
|
67 |
|
68 // source block must include at least a block header, if not actual data |
|
69 ASSERT(source->contents().len() != 0); |
|
70 ASSERT(source->data_offset() != 0); |
|
71 |
|
72 u_int8_t flags = source->flags(); |
|
73 if (last) { |
|
74 flags |= BundleProtocol::BLOCK_FLAG_LAST_BLOCK; |
|
75 } else { |
|
76 flags &= ~BundleProtocol::BLOCK_FLAG_LAST_BLOCK; |
|
77 } |
|
78 |
|
79 generate_preamble(xmit_blocks, block, source->type(), flags, source->data_length()); |
|
80 ASSERT(block->data_offset() == source->data_offset()); |
|
81 ASSERT(block->data_length() == source->data_length()); |
|
82 |
|
83 BlockInfo::DataBuffer* contents = block->writable_contents(); |
|
84 contents->reserve(block->full_length()); |
|
85 memcpy(contents->buf() + block->data_offset(), |
|
86 source->contents().buf() + block->data_offset(), |
|
87 block->data_length()); |
|
88 contents->set_len(block->full_length()); |
|
89 |
|
90 return BP_SUCCESS; |
|
91 } |
|
92 |
|
93 } // namespace dtn |