00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <dirent.h>
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <unistd.h>
00024 #include <netinet/in.h>
00025
00026 #include <oasys/io/IO.h>
00027 #include <oasys/util/StringBuffer.h>
00028 #include <oasys/util/URL.h>
00029
00030 #include "FileConvergenceLayer.h"
00031 #include "bundling/Bundle.h"
00032 #include "bundling/BundleEvent.h"
00033 #include "bundling/BundleList.h"
00034 #include "bundling/BundleProtocol.h"
00035 #include "bundling/BundleDaemon.h"
00036
00037 namespace dtn {
00038
00039
00040
00041
00042
00043
00044 FileConvergenceLayer::FileConvergenceLayer()
00045 : ConvergenceLayer("FileConvergenceLayer", "file")
00046 {
00047 }
00048
00052 bool
00053 FileConvergenceLayer::extract_dir(const char* nexthop, std::string* dirp)
00054 {
00055
00056 PANIC("XXX/demmer fix this implementation");
00057
00058 oasys::URL url(nexthop);
00059
00060 if (! url.valid()) {
00061 log_err("extract_dir: next hop ssp '%s' not a valid url", nexthop);
00062 return false;
00063 }
00064
00065
00066
00067
00068
00069
00070 if (url.host_.length() != 0) {
00071 log_err("interface eid '%s' specifies a non-absolute path",
00072 nexthop);
00073 return false;
00074 }
00075
00076
00077 if (url.port_ != 0) {
00078 log_err("interface eid '%s' specifies a port", nexthop);
00079 return false;
00080 }
00081
00082 dirp->assign("/");
00083 dirp->append(url.path_);
00084 return true;
00085 }
00086
00091 bool
00092 FileConvergenceLayer::validate_dir(const std::string& dir)
00093 {
00094 struct stat st;
00095 if (stat(dir.c_str(), &st) != 0) {
00096 log_err("error running stat on %s: %s", dir.c_str(), strerror(errno));
00097 return false;
00098 }
00099
00100 if (!S_ISDIR(st.st_mode)) {
00101 log_err("error: %s not a directory", dir.c_str());
00102 return false;
00103 }
00104
00105
00106
00107 return true;
00108 }
00109
00113 bool
00114 FileConvergenceLayer::interface_up(Interface* iface,
00115 int argc, const char* argv[])
00116 {
00117 (void)iface;
00118 (void)argc;
00119 (void)argv;
00120
00121 NOTIMPLEMENTED;
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 return true;
00146 }
00147
00151 bool
00152 FileConvergenceLayer::interface_down(Interface* iface)
00153 {
00154 CLInfo *cli = iface->cl_info();
00155 Scanner *scanner = (Scanner *)cli;
00156 scanner->stop();
00157
00158
00159
00160
00161
00162 return true;
00163 }
00164
00168 bool
00169 FileConvergenceLayer::open_contact(const ContactRef& contact)
00170 {
00171 (void)contact;
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 return true;
00186 }
00187
00191 bool
00192 FileConvergenceLayer::close_contact(const ContactRef& contact)
00193 {
00194 (void)contact;
00195
00196 return true;
00197 }
00198
00202 void
00203 FileConvergenceLayer::send_bundle(const ContactRef& contact, Bundle* bundle)
00204 {
00205 (void)contact;
00206 (void)bundle;
00207
00208
00209 NOTIMPLEMENTED;
00210
00211 #ifdef notimplemented
00212 std::string dir;
00213 if (!extract_dir(contact->nexthop(), &dir)) {
00214 PANIC("contact should have already been validated");
00215 }
00216
00217 FileHeader filehdr;
00218 int iovcnt = BundleProtocol::MAX_IOVCNT + 2;
00219 struct iovec iov[iovcnt];
00220
00221 filehdr.version = CURRENT_VERSION;
00222
00223 oasys::StringBuffer fname("%s/bundle-XXXXXX", dir.c_str());
00224
00225 iov[0].iov_base = (char*)&filehdr;
00226 iov[0].iov_len = sizeof(FileHeader);
00227
00228
00229 u_int16_t header_len =
00230 BundleProtocol::format_header_blocks(bundle, &iov[1], &iovcnt);
00231
00232
00233 size_t payload_len = bundle->payload_.length();
00234 filehdr.header_length = htons(header_len);
00235 filehdr.bundle_length = htonl(header_len + payload_len);
00236
00237
00238
00239 iovcnt++;
00240 PANIC("XXX/demmer fix me");
00241
00242 iov[iovcnt].iov_len = payload_len;
00243 iovcnt++;
00244
00245
00246 int fd = mkstemp(fname.c_str());
00247 if (fd == -1) {
00248 log_err("error opening temp file in %s: %s",
00249 fname.c_str(), strerror(errno));
00250
00251 return;
00252 }
00253
00254 log_debug("opened temp file %s for bundle id %d "
00255 "fd %d header_length %zu payload_length %zu",
00256 fname.c_str(), bundle->bundleid_, fd,
00257 header_len, payload_len);
00258
00259
00260 int total = sizeof(FileHeader) + header_len + payload_len;
00261 int cc = oasys::IO::writevall(fd, iov, iovcnt, logpath_);
00262 if (cc != total) {
00263 log_err("error writing out bundle (wrote %d/%d): %s",
00264 cc, total, strerror(errno));
00265 }
00266
00267
00268 BundleProtocol::free_header_iovmem(bundle, &iov[1], iovcnt - 2);
00269
00270
00271 close(fd);
00272
00273
00274 bool acked = false;
00275
00276 BundleDaemon::post(
00277 new BundleTransmittedEvent(bundle, contact, total_len, acked));
00278
00279 log_debug("bundle id %d successfully transmitted", bundle->bundleid_);
00280 #endif // notimplemented
00281 }
00282
00283
00284
00285
00286
00287
00288 FileConvergenceLayer::Scanner::Scanner(int secs_per_scan,
00289 const std::string& dir)
00290 : Logger("FileConvergenceLayer::Scanner",
00291 "/dtn/cl/file/scanner"),
00292 Thread("FileConvergenceLayer::Scanner"),
00293 secs_per_scan_(secs_per_scan),
00294 dir_(dir),
00295 run_(true)
00296 {
00297 set_flag(DELETE_ON_EXIT);
00298 }
00299
00303 void
00304 FileConvergenceLayer::Scanner::run()
00305 {
00306
00307 NOTIMPLEMENTED;
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 log_info("exiting");
00429 }
00430
00434 void FileConvergenceLayer::Scanner::stop() {
00435 run_ = false;
00436 }
00437
00438 }