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