00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Dictionary.h"
00018
00019 namespace prophet
00020 {
00021
00022
00023 const u_int16_t Dictionary::INVALID_SID = 0xffff;
00024 const std::string Dictionary::NULL_STR;
00025
00026 Dictionary::Dictionary(const std::string& sender,
00027 const std::string& receiver)
00028 : sender_(sender), receiver_(receiver) {}
00029
00030 Dictionary::Dictionary(const Dictionary& d)
00031 : sender_(d.sender_), receiver_(d.receiver_),
00032 ribd_(d.ribd_), rribd_(d.rribd_) {}
00033
00034 u_int16_t
00035 Dictionary::find(const std::string& dest_id) const
00036 {
00037
00038 if (dest_id == "") return INVALID_SID;
00039
00040
00041 if (dest_id == sender_) return 0;
00042 if (dest_id == receiver_) return 1;
00043
00044
00045 ribd::const_iterator i = (ribd::const_iterator) ribd_.find(dest_id);
00046 if (i != ribd_.end()) return (*i).second;
00047
00048
00049 return INVALID_SID;
00050 }
00051
00052 const std::string&
00053 Dictionary::find(u_int16_t id) const
00054 {
00055
00056 if (id == 0) return sender_;
00057 if (id == 1) return receiver_;
00058
00059
00060 const_iterator i = (const_iterator) rribd_.find(id);
00061 if (i != rribd_.end()) return (*i).second;
00062
00063
00064 return NULL_STR;
00065 }
00066
00067 u_int16_t
00068 Dictionary::insert(const std::string& dest_id)
00069 {
00070
00071 if (dest_id == "") return INVALID_SID;
00072
00073
00074 if (find(dest_id) != INVALID_SID)
00075 return INVALID_SID;
00076
00077
00078
00079 u_int16_t sid = ribd_.size() + 2;
00080
00081
00082 if (sid == INVALID_SID) return INVALID_SID;
00083
00084 bool res = assign(dest_id,sid);
00085 while (res == false)
00086 {
00087 res = assign(dest_id,++sid);
00088 if (sid == INVALID_SID)
00089
00090 return INVALID_SID;
00091 }
00092 return sid;
00093 }
00094
00095 bool
00096 Dictionary::assign(const std::string& dest_id, u_int16_t sid)
00097 {
00098
00099 if (dest_id == "") return false;
00100
00101
00102 if (ribd_.size() != rribd_.size()) return false;
00103
00104
00105 if (sid == 0)
00106 {
00107 if (sender_ == "" && dest_id != "")
00108 {
00109 sender_.assign(dest_id);
00110 return true;
00111 }
00112 return false;
00113 }
00114 else if (sid == 1)
00115 {
00116 if (receiver_ == "" && dest_id != "")
00117 {
00118 receiver_.assign(dest_id);
00119 return true;
00120 }
00121 return false;
00122 }
00123 else if (sid == INVALID_SID)
00124 {
00125
00126 return false;
00127 }
00128
00129
00130 bool res = ribd_.insert(ribd::value_type(dest_id,sid)).second;
00131 if ( ! res ) return false;
00132
00133
00134 res = rribd_.insert(rribd::value_type(sid,dest_id)).second;
00135 if ( ! res )
00136 ribd_.erase(dest_id);
00137
00138
00139 return res && (ribd_.size() == rribd_.size());
00140 }
00141
00142 size_t
00143 Dictionary::guess_ribd_size(size_t RASsz) const
00144 {
00145 size_t retval = 0;
00146 for (const_iterator i = rribd_.begin(); i != rribd_.end(); i++)
00147 {
00148 retval += FOUR_BYTE_ALIGN( RASsz + (*i).second.length() );
00149 }
00150 return retval;
00151 }
00152
00153 void
00154 Dictionary::clear()
00155 {
00156
00157 sender_.assign("");
00158 receiver_.assign("");
00159 ribd_.clear();
00160 rribd_.clear();
00161 }
00162
00163 void
00164 Dictionary::dump(BundleCore* core,const char* file,u_int line) const
00165 {
00166 if (core == NULL) return;
00167 core->print_log("dictionary", BundleCore::LOG_DEBUG,
00168 "%s(%u): 0 -> %s", file, line, sender_.c_str());
00169 core->print_log("dictionary", BundleCore::LOG_DEBUG,
00170 "%s(%u): 1 -> %s", file, line, receiver_.c_str());
00171 for (const_iterator i = rribd_.begin(); i != rribd_.end(); i++)
00172 core->print_log("dictionary", BundleCore::LOG_DEBUG,
00173 "%s(%u): %u -> %s", file, line, (*i).first,
00174 (*i).second.c_str());
00175 }
00176
00177 };