IBR-DTNSuite  0.10
dtnconvert.cpp
Go to the documentation of this file.
1 /*
2  * dtnconvert.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 "config.h"
23 
24 #include <ibrdtn/data/Serializer.h>
26 #include <ibrdtn/data/Bundle.h>
27 #include <ibrdtn/data/EID.h>
28 
29 #include <ibrcommon/data/BLOB.h>
30 #include <ibrcommon/data/File.h>
31 
32 #include <cstdlib>
33 #include <iomanip>
34 #include <iostream>
35 #include <unistd.h>
36 
37 void print_help()
38 {
39  std::cout << "-- dtnconvert (IBR-DTN) --" << std::endl;
40  std::cout << "Syntax: dtnconvert [options]" << std::endl;
41  std::cout << std::endl;
42  std::cout << "* parameters *" << std::endl;
43  std::cout << " -h display this text" << std::endl;
44  std::cout << " -r read bundle data and print out some information" << std::endl;
45  std::cout << " -c create a bundle with stdin as payload" << std::endl;
46  std::cout << std::endl;
47  std::cout << "* options when creating a bundle *" << std::endl;
48  std::cout << " -s source EID of the bundle" << std::endl;
49  std::cout << " -d destination EID of the bundle" << std::endl;
50  std::cout << " -l lifetime of the bundle in seconds (default: 3600)" << std::endl;
51 }
52 
53 int main(int argc, char** argv)
54 {
55  int opt = 0;
56  int working_mode = 0;
57  dtn::data::EID _source;
58  dtn::data::EID _destination;
59  size_t _lifetime = 3600;
60 
61  while((opt = getopt(argc, argv, "hrcs:d:l:")) != -1)
62  {
63  switch (opt)
64  {
65  case 'h':
66  print_help();
67  break;
68 
69  case 'r':
70  working_mode = 1;
71  break;
72 
73  case 'c':
74  working_mode = 2;
75  break;
76 
77  case 'l':
78  _lifetime = atoi(optarg);
79  break;
80 
81  case 's':
82  _source = std::string(optarg);
83  break;
84 
85  case 'd':
86  _destination = std::string(optarg);
87  break;
88 
89  default:
90  std::cout << "unknown command" << std::endl;
91  return -1;
92  }
93  }
94 
95  if (working_mode == 0)
96  {
97  print_help();
98  return -1;
99  }
100 
101  switch (working_mode)
102  {
103  case 1:
104  {
105  dtn::data::DefaultDeserializer dd(std::cin);
107  dd >> b;
108 
109  std::cout << "flags: " << std::hex << std::setw( 2 ) << std::setfill( '0' ) << b.procflags.toString() << std::dec << std::endl;
110  std::cout << "source: " << b.source.getString() << std::endl;
111  std::cout << "destination: " << b.destination.getString() << std::endl;
112  std::cout << "timestamp: " << b.timestamp.toString() << std::endl;
113  std::cout << "sequence number: " << b.sequencenumber.toString() << std::endl;
114  std::cout << "lifetime: " << b.lifetime.toString() << std::endl;
115 
116  const dtn::data::PayloadBlock &pblock = b.find<dtn::data::PayloadBlock>();
117  ibrcommon::BLOB::Reference ref = pblock.getBLOB();
118 
119  // this part is protected agains other threads
120  {
121  ibrcommon::BLOB::iostream stream = ref.iostream();
122  std::cout << "payload size: " << stream.size() << std::endl;
123  }
124  break;
125  }
126 
127  case 2:
128  {
129  dtn::data::DefaultSerializer ds(std::cout);
131 
132  b.source = _source;
133  b.destination = _destination;
134  b.lifetime = _lifetime;
135 
137  ibrcommon::BLOB::Reference ref = pblock.getBLOB();
138 
139  // this part is protected agains other threads
140  {
141  ibrcommon::BLOB::iostream stream = ref.iostream();
142  (*stream) << std::cin.rdbuf();
143  }
144 
145  ds << b;
146  break;
147  }
148  }
149 
150  return 0;
151 }