• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

tools/src/dtnsend.cpp

Go to the documentation of this file.
00001 /*
00002  * dtnsend.cpp
00003  *
00004  *  Created on: 15.10.2009
00005  *      Author: morgenro
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 #include <ibrcommon/Logger.h>
00017 
00018 #include <iostream>
00019 
00020 void print_help()
00021 {
00022         cout << "-- dtnsend (IBR-DTN) --" << endl;
00023         cout << "Syntax: dtnsend [options] <dst> <filename>"  << endl;
00024         cout << " <dst>         set the destination eid (e.g. dtn://node/filetransfer)" << endl;
00025         cout << " <filename>    the file to transfer" << endl;
00026         cout << "* optional parameters *" << endl;
00027         cout << " -h|--help     display this text" << endl;
00028         cout << " --src <name>  set the source application name (e.g. filetransfer)" << endl;
00029         cout << " -p <0..2>     set the bundle priority (0 = low, 1 = normal, 2 = high)" << endl;
00030         cout << " --lifetime <seconds>" << endl;
00031         cout << "               set the lifetime of outgoing bundles; default: 3600" << endl;
00032         cout << " -U <socket>   use UNIX domain sockets" << endl;
00033         cout << " -n <copies>   create <copies> bundle copies" << endl;
00034         cout << " --encrypt     request encryption on the bundle layer" << endl;
00035         cout << " --sign        request signature on the bundle layer" << endl;
00036         cout << " --custody     request custody transfer of the bundle" << endl;
00037 
00038 }
00039 
00040 int main(int argc, char *argv[])
00041 {
00042         bool error = false;
00043         string file_destination = "dtn://local/filetransfer";
00044         string file_source = "filetransfer";
00045         unsigned int lifetime = 3600;
00046         bool use_stdin = false;
00047         std::string filename;
00048         ibrcommon::File unixdomain;
00049         int priority = 1;
00050         int copies = 1;
00051         bool bundle_encryption = false;
00052         bool bundle_signed = false;
00053         bool bundle_custody = false;
00054 
00055 //      ibrcommon::Logger::setVerbosity(99);
00056 //      ibrcommon::Logger::addStream(std::cout, ibrcommon::Logger::LOGGER_ALL, ibrcommon::Logger::LOG_DATETIME | ibrcommon::Logger::LOG_LEVEL);
00057 
00058         std::list<std::string> arglist;
00059 
00060         for (int i = 0; i < argc; i++)
00061         {
00062                 if (argv[i][0] == '-')
00063                 {
00064                         std::string arg = argv[i];
00065 
00066                         // print help if requested
00067                         if (arg == "-h" || arg == "--help")
00068                         {
00069                                 print_help();
00070                                 return 0;
00071                         }
00072                         else if (arg == "--encrypt")
00073                         {
00074                                 bundle_encryption = true;
00075                         }
00076                         else if (arg == "--sign")
00077                         {
00078                                 bundle_signed = true;
00079                         }
00080                         else if (arg == "--custody")
00081                         {
00082                                 bundle_custody = true;
00083                         }
00084                         else if (arg == "--src" && argc > i)
00085                         {
00086                                 if (++i > argc)
00087                                 {
00088                                         std::cout << "argument missing!" << std::endl;
00089                                         return -1;
00090                                 }
00091 
00092                                 file_source = argv[i];
00093                         }
00094                         else if (arg == "--lifetime" && argc > i)
00095                         {
00096                                 if (++i > argc)
00097                                 {
00098                                         std::cout << "argument missing!" << std::endl;
00099                                         return -1;
00100                                 }
00101 
00102                                 stringstream data; data << argv[i];
00103                                 data >> lifetime;
00104                         }
00105                         else if (arg == "-p" && argc > i)
00106                         {
00107                                 if (++i > argc)
00108                                 {
00109                                         std::cout << "argument missing!" << std::endl;
00110                                         return -1;
00111                                 }
00112                                 stringstream data; data << argv[i];
00113                                 data >> priority;
00114                         }
00115                         else if (arg == "-U" && argc > i)
00116                         {
00117                                 if (++i > argc)
00118                                 {
00119                                         std::cout << "argument missing!" << std::endl;
00120                                         return -1;
00121                                 }
00122 
00123                                 unixdomain = ibrcommon::File(argv[i]);
00124                         }
00125                         else if (arg == "-n" && argc > i)
00126                         {
00127                                 if (++i > argc)
00128                                 {
00129                                         std::cout << "argument missing!" << std::endl;
00130                                         return -1;
00131                                 }
00132 
00133                                 stringstream data; data << argv[i];
00134                                 data >> copies;
00135 
00136                                 if( copies < 1 ) {
00137                                         std::cout << "invalid number of bundle copies!" << std::endl;
00138                                         return -1;
00139                                 }
00140                         } 
00141                         else
00142                         {
00143                                 std::cout << "invalid argument " << arg << std::endl;
00144                                 return -1;
00145                         }
00146                 }
00147                 else
00148                 {
00149                         arglist.push_back(argv[i]);
00150                 }
00151         }
00152 
00153         if (arglist.size() <= 1)
00154         {
00155                 print_help();
00156                 return -1;
00157         } else if (arglist.size() == 2)
00158         {
00159                 std::list<std::string>::iterator iter = arglist.begin(); iter++;
00160 
00161                 // the first parameter is the destination
00162                 file_destination = (*iter);
00163 
00164                 use_stdin = true;
00165         }
00166         else if (arglist.size() > 2)
00167         {
00168                 std::list<std::string>::iterator iter = arglist.begin(); iter++;
00169 
00170                 // the first parameter is the destination
00171                 file_destination = (*iter); iter++;
00172 
00173                 // the second parameter is the filename
00174                 filename = (*iter);
00175         }
00176 
00177         try {
00178                 // Create a stream to the server using TCP.
00179                 ibrcommon::tcpclient conn;
00180 
00181                 // check if the unixdomain socket exists
00182                 if (unixdomain.exists())
00183                 {
00184                         // connect to the unix domain socket
00185                         conn.open(unixdomain);
00186                 }
00187                 else
00188                 {
00189                         // connect to the standard local api port
00190                         conn.open("127.0.0.1", 4550);
00191 
00192                         // enable nodelay option
00193                         conn.enableNoDelay();
00194                 }
00195 
00196                 try {
00197                         // Initiate a client for synchronous receiving
00198                         dtn::api::Client client(file_source, conn, dtn::api::Client::MODE_SENDONLY);
00199 
00200                         // Connect to the server. Actually, this function initiate the
00201                         // stream protocol by starting the thread and sending the contact header.
00202                         client.connect();
00203 
00204                         // target address
00205                         EID addr = EID(file_destination);
00206 
00207                         try {
00208                                 if (use_stdin)
00209                                 {
00210                                         cout << "Transfer stdin to " << addr.getNodeEID() << endl;
00211 
00212                                         // create an empty BLOB
00213                                         ibrcommon::BLOB::Reference ref = ibrcommon::TmpFileBLOB::create();
00214 
00215                                         // copy cin to a BLOB
00216                                         (*ref.iostream()) << cin.rdbuf();
00217 
00218                                         for(int u=0; u<copies; u++){
00219                                                 dtn::api::BLOBBundle b(file_destination, ref);
00220 
00221                                                 // enable encryption if requested
00222                                                 if (bundle_encryption) b.requestEncryption();
00223 
00224                                                 // enable signature if requested
00225                                                 if (bundle_signed) b.requestSigned();
00226 
00227                                                 // enable custody transfer if requested
00228                                                 if (bundle_custody) b.requestCustodyTransfer();
00229 
00230                                                 // set the lifetime
00231                                                 b.setLifetime(lifetime);
00232 
00233                                                 // set the bundles priority
00234                                                 b.setPriority(dtn::api::Bundle::BUNDLE_PRIORITY(priority));
00235 
00236                                                 // send the bundle
00237                                                 client << b;
00238 
00239                                                 if (copies > 1)
00240                                                 {
00241                                                         std::cout << "sent copy #" << (u+1) << std::endl;
00242                                                 }
00243                                         }
00244                                 }
00245                                 else
00246                                 {
00247                                         cout << "Transfer file \"" << filename << "\" to " << addr.getNodeEID() << endl;
00248                                         
00249                                         for(int u=0; u<copies; u++){
00250                                                 // create a bundle from the file
00251                                                 dtn::api::FileBundle b(file_destination, filename);
00252 
00253                                                 // enable encryption if requested
00254                                                 if (bundle_encryption) b.requestEncryption();
00255 
00256                                                 // enable signature if requested
00257                                                 if (bundle_signed) b.requestSigned();
00258 
00259                                                 // enable custody transfer if requested
00260                                                 if (bundle_custody) b.requestCustodyTransfer();
00261 
00262                                                 // set the lifetime
00263                                                 b.setLifetime(lifetime);
00264 
00265                                                 // set the bundles priority
00266                                                 b.setPriority(dtn::api::Bundle::BUNDLE_PRIORITY(priority));
00267 
00268                                                 // send the bundle
00269                                                 client << b;
00270 
00271                                                 if (copies > 1)
00272                                                 {
00273                                                         std::cout << "sent copy #" << (u+1) << std::endl;
00274                                                 }
00275                                         }
00276                                 }
00277 
00278                                 // flush the buffers
00279                                 client.flush();
00280                         } catch (const ibrcommon::IOException &ex) {
00281                                 std::cerr << "Error while sending bundle." << std::endl;
00282                                 std::cerr << "\t" << ex.what() << std::endl;
00283                                 error = true;
00284                         }
00285 
00286                         // Shutdown the client connection.
00287                         client.close();
00288 
00289                         // print out the transmitted bytes
00290                         std::cout << client.lastack << " bytes sent" << std::endl;
00291 
00292                 } catch (const ibrcommon::IOException &ex) {
00293                         cout << "Error: " << ex.what() << endl;
00294                         error = true;
00295                 } catch (const dtn::api::ConnectionException&) {
00296                         // connection already closed, the daemon was faster
00297                 }
00298 
00299                 // close the tcpstream
00300                 conn.close();
00301         } catch (const std::exception &ex) {
00302                 cout << "Error: " << ex.what() << endl;
00303                 error = true;
00304         }
00305 
00306         if (error) return -1;
00307 
00308         return 0;
00309 }

Generated on Wed Mar 30 2011 11:11:49 for IBR-DTNSuite by  doxygen 1.7.1