IBR-DTNSuite  0.8
ibrdtn/ibrdtn/api/Bundle.cpp
Go to the documentation of this file.
00001 /*
00002  * Bundle.cpp
00003  *
00004  *  Created on: 24.07.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/api/Bundle.h"
00009 #include "ibrdtn/data/PayloadBlock.h"
00010 #include "ibrdtn/data/Exceptions.h"
00011 
00012 namespace dtn
00013 {
00014         namespace api
00015         {
00016                 Bundle::Bundle()
00017                 {
00018                         setPriority(PRIO_MEDIUM);
00019                         _b._source = dtn::data::EID("api:me");
00020                 }
00021 
00022                 Bundle::Bundle(const dtn::data::Bundle &b)
00023                  : _b(b)
00024                 {
00025                 }
00026 
00027                 Bundle::Bundle(const dtn::data::EID &destination)
00028                 {
00029                         setDestination(destination);
00030                         setPriority(PRIO_MEDIUM);
00031                         _b._source = dtn::data::EID("api:me");
00032                 }
00033 
00034                 Bundle::~Bundle()
00035                 {
00036                 }
00037 
00038                 void Bundle::setSingleton(bool val)
00039                 {
00040                         _b.set(dtn::data::PrimaryBlock::DESTINATION_IS_SINGLETON, val);
00041                 }
00042 
00043                 void Bundle::setLifetime(unsigned int lifetime)
00044                 {
00045                         _b._lifetime = lifetime;
00046                 }
00047 
00048                 unsigned int Bundle::getLifetime() const
00049                 {
00050                         return _b._lifetime;
00051                 }
00052 
00053                 time_t Bundle::getTimestamp() const
00054                 {
00055                         return _b._timestamp;
00056                 }
00057 
00058                 void Bundle::requestDeliveredReport()
00059                 {
00060                         _b.set(dtn::data::PrimaryBlock::REQUEST_REPORT_OF_BUNDLE_DELIVERY, true);
00061                 }
00062 
00063                 void Bundle::requestForwardedReport()
00064                 {
00065                         _b.set(dtn::data::PrimaryBlock::REQUEST_REPORT_OF_BUNDLE_FORWARDING, true);
00066                 }
00067 
00068                 void Bundle::requestDeletedReport()
00069                 {
00070                         _b.set(dtn::data::PrimaryBlock::REQUEST_REPORT_OF_BUNDLE_DELETION, true);
00071                 }
00072 
00073                 void Bundle::requestReceptionReport()
00074                 {
00075                         _b.set(dtn::data::PrimaryBlock::REQUEST_REPORT_OF_BUNDLE_RECEPTION, true);
00076                 }
00077 
00078                 void Bundle::requestCustodyTransfer()
00079                 {
00080                         _b.set(dtn::data::PrimaryBlock::CUSTODY_REQUESTED, true);
00081                         _b._custodian = dtn::data::EID("api:me");
00082                 }
00083 
00084                 void Bundle::requestEncryption()
00085                 {
00086                         _b.set(dtn::data::PrimaryBlock::DTNSEC_REQUEST_ENCRYPT, true);
00087                 }
00088 
00089                 void Bundle::requestSigned()
00090                 {
00091                         _b.set(dtn::data::PrimaryBlock::DTNSEC_REQUEST_SIGN, true);
00092                 }
00093 
00094                 void Bundle::requestCompression()
00095                 {
00096                         _b.set(dtn::data::PrimaryBlock::IBRDTN_REQUEST_COMPRESSION, true);
00097                 }
00098 
00099                 bool Bundle::statusVerified()
00100                 {
00101                         return _b.get(dtn::data::PrimaryBlock::DTNSEC_STATUS_VERIFIED);
00102                 }
00103 
00104                 Bundle::BUNDLE_PRIORITY Bundle::getPriority() const
00105                 {
00106                         if (_b.get(dtn::data::Bundle::PRIORITY_BIT1))
00107                         {
00108                                 return PRIO_MEDIUM;
00109                         }
00110 
00111                         if (_b.get(dtn::data::Bundle::PRIORITY_BIT2))
00112                         {
00113                                 return PRIO_HIGH;
00114                         }
00115 
00116                         return PRIO_LOW;
00117                 }
00118 
00119                 void Bundle::setPriority(Bundle::BUNDLE_PRIORITY p)
00120                 {
00121                         // set the priority to the real bundle
00122                         switch (p)
00123                         {
00124                         case PRIO_LOW:
00125                                 _b.set(dtn::data::Bundle::PRIORITY_BIT1, false);
00126                                 _b.set(dtn::data::Bundle::PRIORITY_BIT2, false);
00127                                 break;
00128 
00129                         case PRIO_HIGH:
00130                                 _b.set(dtn::data::Bundle::PRIORITY_BIT1, false);
00131                                 _b.set(dtn::data::Bundle::PRIORITY_BIT2, true);
00132                                 break;
00133 
00134                         case PRIO_MEDIUM:
00135                                 _b.set(dtn::data::Bundle::PRIORITY_BIT1, true);
00136                                 _b.set(dtn::data::Bundle::PRIORITY_BIT2, false);
00137                                 break;
00138                         }
00139                 }
00140 
00141                 void Bundle::setDestination(const dtn::data::EID &eid, const bool singleton)
00142                 {
00143                         if (singleton)
00144                                 _b.set(dtn::data::Bundle::DESTINATION_IS_SINGLETON, true);
00145                         else
00146                                 _b.set(dtn::data::Bundle::DESTINATION_IS_SINGLETON, false);
00147 
00148                         _b._destination = eid;
00149                 }
00150 
00151                 void Bundle::setReportTo(const dtn::data::EID &eid)
00152                 {
00153                         _b._reportto = eid;
00154                 }
00155 
00156                 dtn::data::EID Bundle::getReportTo() const
00157                 {
00158                         return _b._reportto;
00159                 }
00160 
00161                 dtn::data::EID Bundle::getDestination() const
00162                 {
00163                         return _b._destination;
00164                 }
00165 
00166                 dtn::data::EID Bundle::getSource() const
00167                 {
00168                         return _b._source;
00169                 }
00170 
00171                 dtn::data::EID Bundle::getCustodian() const
00172                 {
00173                         return _b._custodian;
00174                 }
00175 
00176                 ibrcommon::BLOB::Reference Bundle::getData() throw (dtn::MissingObjectException)
00177                 {
00178                         try {
00179                                 dtn::data::PayloadBlock &p = _b.getBlock<dtn::data::PayloadBlock>();
00180                                 return p.getBLOB();
00181                         } catch(const dtn::data::Bundle::NoSuchBlockFoundException&) {
00182                                 throw dtn::MissingObjectException("No payload block exists!");
00183                         }
00184                 }
00185 
00186                 bool Bundle::operator<(const Bundle& other) const
00187                 {
00188                         return (_b < other._b);
00189                 }
00190 
00191                 bool Bundle::operator>(const Bundle& other) const
00192                 {
00193                         return (_b > other._b);
00194                 }
00195         }
00196 }