00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <dirent.h>
00024 #include <errno.h>
00025 #include <fcntl.h>
00026 #include <unistd.h>
00027 #include <netinet/in.h>
00028
00029 #include <oasys/io/IO.h>
00030 #include <oasys/util/StringBuffer.h>
00031 #include <oasys/util/URI.h>
00032
00033 #include "FileConvergenceLayer.h"
00034 #include "bundling/Bundle.h"
00035 #include "bundling/BundleEvent.h"
00036 #include "bundling/BundleList.h"
00037 #include "bundling/BundleProtocol.h"
00038 #include "bundling/BundleDaemon.h"
00039
00040 namespace dtn {
00041
00042
00043
00044
00045
00046
00047 FileConvergenceLayer::FileConvergenceLayer()
00048 : ConvergenceLayer("FileConvergenceLayer", "file")
00049 {
00050 }
00051
00055 bool
00056 FileConvergenceLayer::extract_dir(const char* nexthop, std::string* dirp)
00057 {
00058
00059 PANIC("XXX/demmer fix this implementation");
00060
00061 oasys::URI uri(nexthop);
00062
00063 if (!uri.valid()) {
00064 log_err("FileConvergenceLayer::extract_dir: "
00065 "next hop ssp '%s' not a valid uri", nexthop);
00066 return false;
00067 }
00068
00069
00070
00071
00072
00073
00074 if (uri.host().length() != 0) {
00075 log_err("interface eid '%s' specifies a non-absolute path",
00076 nexthop);
00077 return false;
00078 }
00079
00080
00081 if (!uri.port().empty()) {
00082 log_err("interface eid '%s' specifies a port", nexthop);
00083 return false;
00084 }
00085
00086 dirp->assign("/");
00087 dirp->append(uri.path());
00088 return true;
00089 }
00090
00095 bool
00096 FileConvergenceLayer::validate_dir(const std::string& dir)
00097 {
00098 struct stat st;
00099 if (stat(dir.c_str(), &st) != 0) {
00100 log_err("error running stat on %s: %s", dir.c_str(), strerror(errno));
00101 return false;
00102 }
00103
00104 if (!S_ISDIR(st.st_mode)) {
00105 log_err("error: %s not a directory", dir.c_str());
00106 return false;
00107 }
00108
00109
00110
00111 return true;
00112 }
00113
00117 bool
00118 FileConvergenceLayer::interface_up(Interface* iface,
00119 int argc, const char* argv[])
00120 {
00121 (void)iface;
00122 (void)argc;
00123 (void)argv;
00124
00125 NOTIMPLEMENTED;
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 return true;
00150 }
00151
00155 bool
00156 FileConvergenceLayer::interface_down(Interface* iface)
00157 {
00158 CLInfo *cli = iface->cl_info();
00159 Scanner *scanner = (Scanner *)cli;
00160 scanner->stop();
00161
00162
00163
00164
00165
00166 return true;
00167 }
00168
00172 bool
00173 FileConvergenceLayer::open_contact(const ContactRef& contact)
00174 {
00175 LinkRef link = contact->link();
00176 ASSERT(link != NULL);
00177 ASSERT(!link->isdeleted());
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 return true;
00193 }
00194
00198 bool
00199 FileConvergenceLayer::close_contact(const ContactRef& contact)
00200 {
00201 (void)contact;
00202
00203 return true;
00204 }
00205
00209 void
00210 FileConvergenceLayer::send_bundle(const ContactRef& contact, Bundle* bundle)
00211 {
00212 (void)contact;
00213 (void)bundle;
00214
00215
00216 NOTIMPLEMENTED;
00217
00218 #ifdef notimplemented
00219 std::string dir;
00220 if (!extract_dir(contact->nexthop(), &dir)) {
00221 PANIC("contact should have already been validated");
00222 }
00223
00224 FileHeader filehdr;
00225 int iovcnt = BundleProtocol::MAX_IOVCNT + 2;
00226 struct iovec iov[iovcnt];
00227
00228 filehdr.version = CURRENT_VERSION;
00229
00230 oasys::StringBuffer fname("%s/bundle-XXXXXX", dir.c_str());
00231
00232 iov[0].iov_base = (char*)&filehdr;
00233 iov[0].iov_len = sizeof(FileHeader);
00234
00235
00236 u_int16_t header_len =
00237 BundleProtocol::format_header_blocks(bundle, &iov[1], &iovcnt);
00238
00239
00240 size_t payload_len = bundle->payload_.length();
00241 filehdr.header_length = htons(header_len);
00242 filehdr.bundle_length = htonl(header_len + payload_len);
00243
00244
00245
00246 iovcnt++;
00247 PANIC("XXX/demmer fix me");
00248
00249 iov[iovcnt].iov_len = payload_len;
00250 iovcnt++;
00251
00252
00253 int fd = mkstemp(fname.c_str());
00254 if (fd == -1) {
00255 log_err("error opening temp file in %s: %s",
00256 fname.c_str(), strerror(errno));
00257
00258 return;
00259 }
00260
00261 log_debug("opened temp file %s for bundle id %d "
00262 "fd %d header_length %zu payload_length %zu",
00263 fname.c_str(), bundle->bundleid_, fd,
00264 header_len, payload_len);
00265
00266
00267 int total = sizeof(FileHeader) + header_len + payload_len;
00268 int cc = oasys::IO::writevall(fd, iov, iovcnt, logpath_);
00269 if (cc != total) {
00270 log_err("error writing out bundle (wrote %d/%d): %s",
00271 cc, total, strerror(errno));
00272 }
00273
00274
00275 BundleProtocol::free_header_iovmem(bundle, &iov[1], iovcnt - 2);
00276
00277
00278 close(fd);
00279
00280
00281 bool acked = false;
00282
00283 BundleDaemon::post(
00284 new BundleTransmittedEvent(bundle, contact, total_len, acked));
00285
00286 log_debug("bundle id %d successfully transmitted", bundle->bundleid_);
00287 #endif // notimplemented
00288 }
00289
00290
00291
00292
00293
00294
00295 FileConvergenceLayer::Scanner::Scanner(int secs_per_scan,
00296 const std::string& dir)
00297 : Logger("FileConvergenceLayer::Scanner",
00298 "/dtn/cl/file/scanner"),
00299 Thread("FileConvergenceLayer::Scanner"),
00300 secs_per_scan_(secs_per_scan),
00301 dir_(dir),
00302 run_(true)
00303 {
00304 set_flag(DELETE_ON_EXIT);
00305 }
00306
00310 void
00311 FileConvergenceLayer::Scanner::run()
00312 {
00313
00314 NOTIMPLEMENTED;
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
00429
00430
00431
00432
00433
00434
00435 log_info("exiting");
00436 }
00437
00441 void FileConvergenceLayer::Scanner::stop() {
00442 run_ = false;
00443 }
00444
00445 }