00001 #include "ibrdtn/utils/Utils.h" 00002 #include "ibrcommon/data/BLOB.h" 00003 #include "ibrdtn/data/Exceptions.h" 00004 #include <math.h> 00005 00006 namespace dtn 00007 { 00008 namespace utils 00009 { 00010 vector<string> Utils::tokenize(string token, string data) 00011 { 00012 vector<string> l; 00013 string value; 00014 00015 // Skip delimiters at beginning. 00016 string::size_type lastPos = data.find_first_not_of(token, 0); 00017 // Find first "non-delimiter". 00018 string::size_type pos = data.find_first_of(token, lastPos); 00019 00020 while (string::npos != pos || string::npos != lastPos) 00021 { 00022 // Found a token, add it to the vector. 00023 value = data.substr(lastPos, pos - lastPos); 00024 l.push_back(value); 00025 // Skip delimiters. Note the "not_of" 00026 lastPos = data.find_first_not_of(token, pos); 00027 // Find next "non-delimiter" 00028 pos = data.find_first_of(token, lastPos); 00029 } 00030 00031 return l; 00032 } 00033 00037 double Utils::distance(double lat1, double lon1, double lat2, double lon2) 00038 { 00039 const double r = 6371; //km 00040 00041 double dLat = toRad( (lat2-lat1) ); 00042 double dLon = toRad( (lon2-lon1) ); 00043 00044 double a = sin(dLat/2) * sin(dLat/2) + 00045 cos(toRad(lat1)) * cos(toRad(lat2)) * 00046 sin(dLon/2) * sin(dLon/2); 00047 double c = 2 * atan2(sqrt(a), sqrt(1-a)); 00048 return r * c; 00049 } 00050 00051 const double Utils::pi = 3.14159; 00052 00053 double Utils::toRad(double value) 00054 { 00055 return value * pi / 180; 00056 } 00057 00058 // dtn::data::CustodySignalBlock* Utils::getCustodySignalBlock(const dtn::data::Bundle &bundle) 00059 // { 00060 // const std::list<dtn::data::Block*> blocks = bundle.getBlocks(); 00061 // std::list<dtn::data::Block*>::const_iterator iter = blocks.begin(); 00062 // 00063 // while (iter != blocks.end()) 00064 // { 00065 // dtn::data::CustodySignalBlock *block = dynamic_cast<dtn::data::CustodySignalBlock*>(*iter); 00066 // if (block != NULL) return block; 00067 // } 00068 // 00069 // return NULL; 00070 // } 00071 // 00072 // dtn::data::StatusReportBlock* Utils::getStatusReportBlock(const dtn::data::Bundle &bundle) 00073 // { 00074 // const list<dtn::data::Block*> blocks = bundle.getBlocks(); 00075 // list<dtn::data::Block*>::const_iterator iter = blocks.begin(); 00076 // 00077 // while (iter != blocks.end()) 00078 // { 00079 // dtn::data::StatusReportBlock *block = dynamic_cast<dtn::data::StatusReportBlock*>(*iter); 00080 // if (block != NULL) return block; 00081 // } 00082 // 00083 // return NULL; 00084 // } 00085 // 00086 // dtn::data::PayloadBlock* Utils::getPayloadBlock(const dtn::data::Bundle &bundle) 00087 // { 00088 // const list<dtn::data::Block*> blocks = bundle.getBlocks(); 00089 // list<dtn::data::Block*>::const_iterator iter = blocks.begin(); 00090 // 00091 // while (iter != blocks.end()) 00092 // { 00093 // dtn::data::PayloadBlock *payload = dynamic_cast<dtn::data::PayloadBlock*>(*iter); 00094 // if (payload != NULL) return payload; 00095 // } 00096 // 00097 // return NULL; 00098 // } 00099 00100 // pair<dtn::data::PayloadBlock*, dtn::data::PayloadBlock*> Utils::split(dtn::data::PayloadBlock *block, size_t split_position) 00101 // { 00102 // const ibrcommon::BLOB::Reference b = block->getBLOB::Reference(); 00103 // pair<ibrcommon::BLOB::Reference, ibrcommon::BLOB::Reference> refpair = b.split(split_position); 00104 // 00105 // dtn::data::PayloadBlock *p1 = new dtn::data::PayloadBlock(refpair.first); 00106 // dtn::data::PayloadBlock *p2 = new dtn::data::PayloadBlock(refpair.second); 00107 // 00108 // return make_pair(p1, p2); 00109 // } 00110 // 00111 // bool Utils::compareFragments(const dtn::data::Bundle &first, const dtn::data::Bundle &second) 00112 // { 00113 // // if the offset of the first bundle is lower than the second... 00114 // if (first._fragmentoffset < second._fragmentoffset) 00115 // { 00116 // return true; 00117 // } 00118 // else 00119 // { 00120 // return false; 00121 // } 00122 // } 00123 00124 // dtn::data::Bundle Utils::merge(dtn::data::Bundle &destination, const dtn::data::Bundle &source) 00125 // { 00126 // if (!(destination._procflags & dtn::data::Bundle::FRAGMENT)) 00127 // { 00128 // throw dtn::exceptions::FragmentationException("At least one of the bundles isn't a fragment."); 00129 // } 00130 // 00131 // if (!(source._procflags & dtn::data::Bundle::FRAGMENT)) 00132 // { 00133 // throw dtn::exceptions::FragmentationException("At least one of the bundles isn't a fragment."); 00134 // } 00135 // 00136 // // check that they belongs together 00137 // if (( destination._timestamp != source._timestamp ) || 00138 // ( destination._sequencenumber != source._sequencenumber ) || 00139 // ( destination._lifetime != source._lifetime ) || 00140 // ( destination._appdatalength != source._appdatalength ) || 00141 // ( destination._source != source._source ) ) 00142 // { 00143 // // exception 00144 // throw dtn::exceptions::FragmentationException("This fragments don't belongs together."); 00145 // } 00146 // 00147 // // checks complete, now merge the blocks 00148 // dtn::data::PayloadBlock *payload1 = Utils::getPayloadBlock( destination ); 00149 // dtn::data::PayloadBlock *payload2 = Utils::getPayloadBlock( source ); 00150 // 00151 // // TODO: copy blocks other than the payload block! 00152 // 00153 // unsigned int endof1 = destination._fragmentoffset + payload1->getBLOBReference().getSize(); 00154 // 00155 // if (endof1 < source._fragmentoffset) 00156 // { 00157 // // this aren't adjacency fragments 00158 // throw dtn::exceptions::FragmentationException("This aren't adjacency fragments and can't be merged."); 00159 // } 00160 // 00161 // // relative offset of payload1 to payload2 00162 // unsigned int p2offset = source._fragmentoffset - destination._fragmentoffset; 00163 // 00164 // // append the payload of fragment2 at the end of fragment1 00165 // ibrcommon::BLOBReference ref = payload2->getBLOBReference(); 00166 // payload1->getBLOBReference().append( ref ); 00167 // 00168 // size_t payload_s = payload1->getBLOBReference().getSize(); 00169 // 00170 // // if the bundle is complete return a non-fragmented bundle instead of the fragment 00171 // if (payload_s == destination._appdatalength) 00172 // { 00173 // // remove the fragment fields of the bundle data 00174 // destination._procflags &= ~(dtn::data::Bundle::FRAGMENT); 00175 // } 00176 // 00177 // return destination; 00178 // } 00179 00180 // dtn::data::Bundle Utils::merge(std::list<dtn::data::Bundle> &bundles) 00181 // { 00182 // // no bundle, raise a exception 00183 // if (bundles.size() <= 1) 00184 // { 00185 // throw dtn::exceptions::FragmentationException("None or only one item in the list."); 00186 // } 00187 // 00188 // // sort the fragments 00189 // bundles.sort(Utils::compareFragments); 00190 // 00191 // // take a copy of the first bundle as base and merge it with the others 00192 // dtn::data::Bundle first = bundles.front(); 00193 // bundles.pop_front(); 00194 // dtn::data::Bundle second = bundles.front(); 00195 // dtn::data::Bundle bundle; 00196 // 00197 // try { 00198 // // the first merge creates a new bundle object 00199 // bundle = merge(first, second); 00200 // bundles.pop_front(); 00201 // } catch (const exceptions::FragmentationException&) { 00202 // bundles.push_front(first); 00203 // throw ex; 00204 // } 00205 // 00206 // if (bundle._procflags & dtn::data::Bundle::FRAGMENT) 00207 // { 00208 // // put the new fragment into the list 00209 // bundles.push_back(bundle); 00210 // 00211 // // call merge recursive 00212 // return merge(bundles); 00213 // } 00214 // else 00215 // { 00216 // return bundle; 00217 // } 00218 // } 00219 } 00220 }
1.7.1