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 "ibrcommon/net/tcpclient.h"
00012 #include "ibrcommon/thread/Mutex.h"
00013 #include "ibrcommon/thread/MutexLock.h"
00014
00015 #include <csignal>
00016 #include <sys/types.h>
00017 #include <iostream>
00018
00019 void print_help()
00020 {
00021 cout << "-- dtnrecv (IBR-DTN) --" << endl;
00022 cout << "Syntax: dtnrecv [options]" << endl;
00023 cout << " <filename> the file to transfer" << endl;
00024 cout << "* optional parameters *" << endl;
00025 cout << " -h|--help display this text" << endl;
00026 cout << " --file <filename> write the incoming data to the a file instead of the standard output" << endl;
00027 cout << " --name <name> set the application name (e.g. filetransfer)" << endl;
00028 cout << " --timeout <seconds> receive timeout in seconds" << endl;
00029
00030 }
00031
00032 void writeBundle(bool stdout, string filename, dtn::api::Bundle &b)
00033 {
00034 ibrcommon::BLOB::Reference data = b.getData();
00035
00036
00037 if (stdout)
00038 {
00039 ibrcommon::MutexLock l(data);
00040 cout << (*data).rdbuf();
00041 }
00042 else
00043 {
00044 cout << " received." << endl;
00045 cout << "Writing bundle payload to " << filename << endl;
00046
00047 fstream file(filename.c_str(), ios::in|ios::out|ios::binary|ios::trunc);
00048
00049 ibrcommon::MutexLock l(data);
00050 file << (*data).rdbuf();
00051
00052 file.close();
00053
00054 cout << "finished" << endl;
00055 }
00056 }
00057
00058 dtn::api::Client *_client = NULL;
00059 ibrcommon::tcpclient *_conn = NULL;
00060
00061 void term(int signal)
00062 {
00063 if (signal >= 1)
00064 {
00065 if (_client != NULL)
00066 {
00067 _client->close();
00068 _conn->close();
00069 }
00070 exit(0);
00071 }
00072 }
00073
00074 int main(int argc, char *argv[])
00075 {
00076
00077 signal(SIGINT, term);
00078 signal(SIGTERM, term);
00079
00080 int ret = EXIT_SUCCESS;
00081 string filename = "";
00082 string name = "filetransfer";
00083 bool stdout = true;
00084 int timeout = 0;
00085
00086 for (int i = 0; i < argc; i++)
00087 {
00088 string arg = argv[i];
00089
00090
00091 if (arg == "-h" || arg == "--help")
00092 {
00093 print_help();
00094 return ret;
00095 }
00096
00097 if (arg == "--name" && argc > i)
00098 {
00099 name = argv[i + 1];
00100 }
00101
00102 if (arg == "--file" && argc > i)
00103 {
00104 filename = argv[i + 1];
00105 stdout = false;
00106 }
00107
00108 if (arg == "--timeout" && argc > i)
00109 {
00110 timeout = atoi(argv[i + 1]);
00111 }
00112 }
00113
00114 try {
00115
00116 ibrcommon::tcpclient conn("127.0.0.1", 4550);
00117
00118
00119 dtn::api::Client client(name, conn);
00120
00121
00122 _conn = &conn;
00123 _client = &client;
00124
00125
00126
00127 client.connect();
00128
00129 if (!stdout) std::cout << "Wait for incoming bundle... " << std::flush;
00130
00131
00132 dtn::api::Bundle b = client.getBundle(timeout);
00133
00134
00135 writeBundle(stdout, filename, b);
00136
00137
00138 client.close();
00139
00140
00141 conn.close();
00142
00143
00144 while (!client.eof())
00145 {
00146 client >> b;
00147
00148
00149 writeBundle(stdout, filename, b);
00150 }
00151 } catch (...) {
00152 ret = EXIT_FAILURE;
00153 }
00154
00155 return ret;
00156 }