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 #include "ibrdtn/data/PayloadBlock.h"
00015 #include "ibrdtn/data/Bundle.h"
00016 #include "ibrcommon/data/BLOB.h"
00017 #include "ibrcommon/data/File.h"
00018 #include "ibrcommon/appstreambuf.h"
00019
00020 #include <stdlib.h>
00021 #include <iostream>
00022 #include <map>
00023 #include <vector>
00024 #include <csignal>
00025 #include <sys/types.h>
00026
00027 using namespace ibrcommon;
00028
00029 void print_help()
00030 {
00031 cout << "-- dtninbox (IBR-DTN) --" << endl;
00032 cout << "Syntax: dtninbox [options] <name> <inbox>" << endl;
00033 cout << " <name> the application name" << endl;
00034 cout << " <inbox> directory where incoming files should be placed" << endl;
00035 cout << "* optional parameters *" << endl;
00036 cout << " -h|--help display this text" << endl;
00037 cout << " -w|--workdir temporary work directory" << endl;
00038 }
00039
00040 map<string,string> readconfiguration(int argc, char** argv)
00041 {
00042
00043 if (argc < 3) { print_help(); exit(0); }
00044
00045 map<string,string> ret;
00046
00047 ret["name"] = argv[argc - 2];
00048 ret["inbox"] = argv[argc - 1];
00049
00050 for (int i = 0; i < (argc - 2); i++)
00051 {
00052 string arg = argv[i];
00053
00054
00055 if (arg == "-h" || arg == "--help")
00056 {
00057 print_help();
00058 exit(0);
00059 }
00060
00061 if ((arg == "-w" || arg == "--workdir") && (argc > i))
00062 {
00063 ret["workdir"] = argv[i + 1];
00064 }
00065 }
00066
00067 return ret;
00068 }
00069
00070
00071 bool _running = true;
00072
00073
00074 ibrcommon::tcpclient *_conn = NULL;
00075
00076 void term(int signal)
00077 {
00078 if (signal >= 1)
00079 {
00080 _running = false;
00081 if (_conn != NULL) _conn->close();
00082 }
00083 }
00084
00085
00086
00087
00088 int main(int argc, char** argv)
00089 {
00090
00091 signal(SIGINT, term);
00092 signal(SIGTERM, term);
00093
00094
00095 map<string,string> conf = readconfiguration(argc, argv);
00096
00097
00098 if (conf.find("workdir") != conf.end())
00099 {
00100 ibrcommon::BLOB::tmppath = File(conf["workdir"]);
00101 }
00102
00103
00104 size_t backoff = 2;
00105
00106
00107 File outbox(conf["outbox"]);
00108
00109
00110 while (_running)
00111 {
00112 try {
00113
00114 ibrcommon::tcpclient conn("127.0.0.1", 4550);
00115
00116
00117 _conn = &conn;
00118
00119
00120 dtn::api::Client client(conf["name"], conn);
00121
00122
00123
00124 client.connect();
00125
00126
00127 if (client.isConnected()) backoff = 2;
00128
00129
00130 while (client.isConnected() && _running)
00131 {
00132
00133 dtn::api::Bundle b = client.getBundle();
00134
00135
00136 ibrcommon::BLOB::Reference ref = b.getData();
00137
00138
00139 stringstream cmdstream; cmdstream << "tar -x -C " << conf["inbox"];
00140
00141
00142 appstreambuf extractor(cmdstream.str(), appstreambuf::MODE_WRITE);
00143 ostream stream(&extractor);
00144
00145
00146 {
00147 ibrcommon::MutexLock l(ref);
00148 stream << (*ref).rdbuf();
00149 }
00150
00151
00152 stream.flush();
00153 }
00154
00155
00156 client.close();
00157
00158
00159 conn.close();
00160
00161
00162 _conn = NULL;
00163 } catch (ibrcommon::tcpclient::SocketException ex) {
00164
00165 _conn = NULL;
00166
00167 if (_running)
00168 {
00169 cout << "Connection to bundle daemon failed. Retry in " << backoff << " seconds." << endl;
00170 sleep(backoff);
00171
00172
00173 if (backoff < 600)
00174 {
00175
00176 backoff = backoff * 2;
00177 }
00178 }
00179 } catch (ibrcommon::IOException ex) {
00180
00181 _conn = NULL;
00182
00183 if (_running)
00184 {
00185 cout << "Connection to bundle daemon failed. Retry in " << backoff << " seconds." << endl;
00186 sleep(backoff);
00187
00188
00189 if (backoff < 600)
00190 {
00191
00192 backoff = backoff * 2;
00193 }
00194 }
00195 } catch (ibrcommon::ConnectionClosedException ex) {
00196
00197 _conn = NULL;
00198 }
00199 }
00200
00201 return (EXIT_SUCCESS);
00202 }