IBR-DTNSuite  0.12
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 << endl;
41  std::cout << "* optional parameters *" << std::endl;
42  std::cout << " -h Display this text" << std::endl;
43  std::cout << " -r Read bundle data and print out some information" << std::endl;
44  std::cout << " -c Create a bundle with stdin as payload" << std::endl;
45  std::cout << std::endl;
46  std::cout << "* options when creating a bundle *" << std::endl;
47  std::cout << " -s Source EID of the bundle" << std::endl;
48  std::cout << " -d Destination EID of the bundle" << std::endl;
49  std::cout << " -l Lifetime of the bundle in seconds (default: 3600)" << std::endl;
50 }
51 
52 int main(int argc, char** argv)
53 {
54  int opt = 0;
55  int working_mode = 0;
56  dtn::data::EID _source;
57  dtn::data::EID _destination;
58  size_t _lifetime = 3600;
59 
60  while((opt = getopt(argc, argv, "hrcs:d:l:")) != -1)
61  {
62  switch (opt)
63  {
64  case 'h':
65  print_help();
66  break;
67 
68  case 'r':
69  working_mode = 1;
70  break;
71 
72  case 'c':
73  working_mode = 2;
74  break;
75 
76  case 'l':
77  _lifetime = atoi(optarg);
78  break;
79 
80  case 's':
81  _source = std::string(optarg);
82  break;
83 
84  case 'd':
85  _destination = std::string(optarg);
86  break;
87 
88  default:
89  std::cout << "unknown command" << std::endl;
90  return -1;
91  }
92  }
93 
94  if (working_mode == 0)
95  {
96  print_help();
97  return -1;
98  }
99 
100  switch (working_mode)
101  {
102  case 1:
103  {
104  dtn::data::DefaultDeserializer dd(std::cin);
106  dd >> b;
107 
108  std::cout << "flags: " << std::hex << std::setw( 2 ) << std::setfill( '0' ) << b.procflags.toString() << std::dec << std::endl;
109  std::cout << "source: " << b.source.getString() << std::endl;
110  std::cout << "destination: " << b.destination.getString() << std::endl;
111  std::cout << "timestamp: " << b.timestamp.toString() << std::endl;
112  std::cout << "sequence number: " << b.sequencenumber.toString() << std::endl;
113  std::cout << "lifetime: " << b.lifetime.toString() << std::endl;
114 
116  std::cout << "fragment offset: " << b.fragmentoffset.toString() << std::endl;
117  std::cout << "app data length: " << b.appdatalength.toString() << std::endl;
118  }
119 
120  const dtn::data::PayloadBlock &pblock = b.find<dtn::data::PayloadBlock>();
121  ibrcommon::BLOB::Reference ref = pblock.getBLOB();
122 
123  // this part is protected agains other threads
124  std::cout << "payload size: " << ref.size() << std::endl;
125  break;
126  }
127 
128  case 2:
129  {
130  dtn::data::DefaultSerializer ds(std::cout);
132 
133  b.source = _source;
134  b.destination = _destination;
135  b.lifetime = _lifetime;
136 
138  ibrcommon::BLOB::Reference ref = pblock.getBLOB();
139 
140  // this part is protected agains other threads
141  {
142  ibrcommon::BLOB::iostream stream = ref.iostream();
143  (*stream) << std::cin.rdbuf();
144  }
145 
146  ds << b;
147  break;
148  }
149  }
150 
151  return 0;
152 }