00001
00002
00003
00004
00005
00006
00007
00008 #include "config.h"
00009 #include <ibrdtn/api/Client.h>
00010 #include <ibrdtn/api/FileBundle.h>
00011 #include <ibrdtn/api/BLOBBundle.h>
00012 #include <ibrcommon/net/tcpclient.h>
00013 #include <ibrcommon/thread/Mutex.h>
00014 #include <ibrcommon/thread/MutexLock.h>
00015 #include <ibrcommon/data/BLOB.h>
00016
00017 #include <iostream>
00018
00019 void print_help()
00020 {
00021 cout << "-- dtnsend (IBR-DTN) --" << endl;
00022 cout << "Syntax: dtnsend [options] <dst> <filename>" << endl;
00023 cout << " <dst> set the destination eid (e.g. dtn://node/filetransfer)" << endl;
00024 cout << " <filename> the file to transfer" << endl;
00025 cout << "* optional parameters *" << endl;
00026 cout << " -h|--help display this text" << endl;
00027 cout << " --src <name> set the source application name (e.g. filetransfer)" << endl;
00028 cout << " -p <0..2> set the bundle priority (0 = low, 1 = normal, 2 = high)" << endl;
00029 cout << " --lifetime <seconds> set the lifetime of outgoing bundles; default: 3600" << endl;
00030
00031 }
00032
00033 int main(int argc, char *argv[])
00034 {
00035 string file_destination = "dtn://local/filetransfer";
00036 string file_source = "filetransfer";
00037 unsigned int lifetime = 3600;
00038 bool use_stdin = false;
00039 std::string filename;
00040 int priority = 1;
00041
00042 std::list<std::string> arglist;
00043
00044 for (int i = 0; i < argc; i++)
00045 {
00046 if (argv[i][0] == '-')
00047 {
00048 std::string arg = argv[i];
00049
00050
00051 if (arg == "-h" || arg == "--help")
00052 {
00053 print_help();
00054 return 0;
00055 }
00056
00057 if (arg == "--src" && argc > i)
00058 {
00059 if (++i > argc)
00060 {
00061 std::cout << "argument missing!" << std::endl;
00062 return -1;
00063 }
00064
00065 file_source = argv[i];
00066 }
00067
00068 if (arg == "--lifetime" && argc > i)
00069 {
00070 if (++i > argc)
00071 {
00072 std::cout << "argument missing!" << std::endl;
00073 return -1;
00074 }
00075
00076 stringstream data; data << argv[i];
00077 data >> lifetime;
00078 }
00079
00080 if (arg == "-p" && argc > i)
00081 {
00082 if (++i > argc)
00083 {
00084 std::cout << "argument missing!" << std::endl;
00085 return -1;
00086 }
00087 stringstream data; data << argv[i];
00088 data >> priority;
00089 }
00090 }
00091 else
00092 {
00093 arglist.push_back(argv[i]);
00094 }
00095 }
00096
00097 if (arglist.size() <= 1)
00098 {
00099 print_help();
00100 return -1;
00101 } else if (arglist.size() == 2)
00102 {
00103 std::list<std::string>::iterator iter = arglist.begin(); iter++;
00104
00105
00106 file_destination = (*iter);
00107
00108 use_stdin = true;
00109 }
00110 else if (arglist.size() > 2)
00111 {
00112 std::list<std::string>::iterator iter = arglist.begin(); iter++;
00113
00114
00115 file_destination = (*iter); iter++;
00116
00117
00118 filename = (*iter);
00119 }
00120
00121 try {
00122
00123 ibrcommon::tcpclient conn("127.0.0.1", 4550);
00124
00125 try {
00126
00127 dtn::api::Client client(dtn::api::Client::MODE_SENDONLY, file_source, conn);
00128
00129
00130
00131 client.connect();
00132
00133
00134 EID addr = EID(file_destination);
00135
00136 try {
00137 if (use_stdin)
00138 {
00139 cout << "Transfer stdin to " << addr.getNodeEID() << endl;
00140
00141
00142 ibrcommon::BLOB::Reference ref = ibrcommon::TmpFileBLOB::create();
00143
00144
00145 {
00146 ibrcommon::MutexLock l(ref);
00147 (*ref) << cin.rdbuf();
00148 }
00149
00150 dtn::api::BLOBBundle b(file_destination, ref);
00151
00152
00153 b.setLifetime(lifetime);
00154
00155
00156 b.setPriority(dtn::api::Bundle::BUNDLE_PRIORITY(priority));
00157
00158
00159 client << b;
00160 }
00161 else
00162 {
00163 cout << "Transfer file \"" << filename << "\" to " << addr.getNodeEID() << endl;
00164
00165
00166 dtn::api::FileBundle b(file_destination, filename);
00167
00168
00169 b.setLifetime(lifetime);
00170
00171
00172 b.setPriority(dtn::api::Bundle::BUNDLE_PRIORITY(priority));
00173
00174
00175 client << b;
00176 }
00177
00178
00179 client.flush();
00180 } catch (ibrcommon::IOException ex) {
00181 std::cerr << "Error while sending bundle." << std::endl;
00182 std::cerr << "\t" << ex.what() << std::endl;
00183 }
00184
00185
00186 client.close();
00187 } catch (ibrcommon::IOException ex) {
00188 cout << "Error: " << ex.what() << endl;
00189 }
00190
00191
00192 conn.close();
00193 } catch (...) {
00194
00195 }
00196
00197 return 0;
00198 }