IBR-DTNSuite  0.8
tools/src/dtnconvert.cpp
Go to the documentation of this file.
00001 /*
00002  * dtnconvert.cpp
00003  *
00004  *  Created on: 11.01.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "config.h"
00009 
00010 #include <ibrdtn/data/Serializer.h>
00011 #include <ibrdtn/data/PayloadBlock.h>
00012 #include <ibrdtn/data/Bundle.h>
00013 #include <ibrdtn/data/EID.h>
00014 
00015 #include <ibrcommon/data/BLOB.h>
00016 #include <ibrcommon/data/File.h>
00017 
00018 #include <cstdlib>
00019 #include <iomanip>
00020 #include <iostream>
00021 #include <unistd.h>
00022 
00023 void print_help()
00024 {
00025         std::cout << "-- dtnconvert (IBR-DTN) --" << std::endl;
00026         std::cout << "Syntax: dtnconvert [options]"  << std::endl;
00027         std::cout << std::endl;
00028         std::cout << "* parameters *" << std::endl;
00029         std::cout << " -h        display this text" << std::endl;
00030         std::cout << " -r        read bundle data and print out some information" << std::endl;
00031         std::cout << " -c        create a bundle with stdin as payload" << std::endl;
00032         std::cout << std::endl;
00033         std::cout << "* options when creating a bundle *" << std::endl;
00034         std::cout << " -s        source EID of the bundle" << std::endl;
00035         std::cout << " -d        destination EID of the bundle" << std::endl;
00036         std::cout << " -l        lifetime of the bundle in seconds (default: 3600)" << std::endl;
00037 }
00038 
00039 int main(int argc, char** argv)
00040 {
00041         int opt = 0;
00042         int working_mode = 0;
00043         dtn::data::EID _source;
00044         dtn::data::EID _destination;
00045         size_t _lifetime = 3600;
00046 
00047         while((opt = getopt(argc, argv, "hrcs:d:l:")) != -1)
00048         {
00049                 switch (opt)
00050                 {
00051                 case 'h':
00052                         print_help();
00053                         break;
00054 
00055                 case 'r':
00056                         working_mode = 1;
00057                         break;
00058 
00059                 case 'c':
00060                         working_mode = 2;
00061                         break;
00062 
00063                 case 'l':
00064                         _lifetime = atoi(optarg);
00065                         break;
00066 
00067                 case 's':
00068                         _source = std::string(optarg);
00069                         break;
00070 
00071                 case 'd':
00072                         _destination = std::string(optarg);
00073                         break;
00074 
00075                 default:
00076                         std::cout << "unknown command" << std::endl;
00077                         return -1;
00078                 }
00079         }
00080 
00081         if (working_mode == 0)
00082         {
00083                 print_help();
00084                 return -1;
00085         }
00086 
00087         switch (working_mode)
00088         {
00089                 case 1:
00090                 {
00091                         dtn::data::DefaultDeserializer dd(std::cin);
00092                         dtn::data::Bundle b;
00093                         dd >> b;
00094 
00095                         std::cout << "flags: " << std::hex << std::setw( 2 ) << std::setfill( '0' ) << b._procflags << std::dec << std::endl;
00096                         std::cout << "source: " << b._source.getString() << std::endl;
00097                         std::cout << "destination: " << b._destination.getString() << std::endl;
00098                         std::cout << "timestamp: " << b._timestamp << std::endl;
00099                         std::cout << "sequence number: " << b._sequencenumber << std::endl;
00100                         std::cout << "lifetime: " << b._lifetime << std::endl;
00101 
00102                         const dtn::data::PayloadBlock &pblock = b.getBlock<dtn::data::PayloadBlock>();
00103                         ibrcommon::BLOB::Reference ref = pblock.getBLOB();
00104 
00105                         // this part is protected agains other threads
00106                         {
00107                                 ibrcommon::BLOB::iostream stream = ref.iostream();
00108                                 std::cout << "payload size: " << stream.size() << std::endl;
00109                         }
00110                         break;
00111                 }
00112 
00113                 case 2:
00114                 {
00115                         dtn::data::DefaultSerializer ds(std::cout);
00116                         dtn::data::Bundle b;
00117 
00118                         b._source = _source;
00119                         b._destination = _destination;
00120                         b._lifetime = _lifetime;
00121 
00122                         const dtn::data::PayloadBlock &pblock = b.push_back<dtn::data::PayloadBlock>();
00123                         ibrcommon::BLOB::Reference ref = pblock.getBLOB();
00124 
00125                         // this part is protected agains other threads
00126                         {
00127                                 ibrcommon::BLOB::iostream stream = ref.iostream();
00128                                 (*stream) << std::cin.rdbuf();
00129                         }
00130 
00131                         ds << b;
00132                         break;
00133                 }
00134         }
00135 
00136         return 0;
00137 }