00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "TcaControlBundle.h"
00041 #include "../../servlib/bundling/Bundle.h"
00042
00043
00044 namespace dtn {
00045
00046
00048
00049
00050
00051 TcaControlBundle::TcaControlBundle(const std::string& payload)
00052 {
00053 std::string s;
00054 parse_payload(payload, type_, code_, s);
00055
00056 while (s.length() > 0)
00057 {
00058 args_.push_back(TcaControlBundle::eat_to_tab(s));
00059 }
00060 }
00061
00062
00063 std::string
00064 TcaControlBundle::str() const
00065 {
00066 std::string s;
00067 s = code_;
00068 s += ":";
00069
00070 int n = args_.size();
00071 if (n>=1) s += args_[0];
00072 for (int i=1; i<n; ++i)
00073 {
00074 s += "\t";
00075 s += args_[i];
00076 }
00077
00078 return s;
00079 }
00080
00081
00082 bool
00083 TcaControlBundle::parse_payload(const std::string& payload, TypeCode& type,
00084 std::string& code, std::string& body)
00085 {
00086 code = "";
00087
00088 if (payload.length() == 0) return false;
00089
00090 std::string::size_type colon = payload.substr().find(':');
00091 if (colon == std::string::npos)
00092 {
00093
00094 code = payload;
00095 }
00096 else
00097 {
00098 code = payload.substr(0,colon);
00099 body = payload.substr(colon+1, payload.length());
00100 }
00101
00102 if (code == "adv") type = CB_ADV;
00103 else if (code == "adv_sent") type = CB_ADV_SENT;
00104 else if (code == "ask") type = CB_ASK;
00105 else if (code == "ask_received") type = CB_ASK_RECEIVED;
00106 else if (code == "ask_sent") type = CB_ASK_SENT;
00107 else if (code == "coa") type = CB_COA;
00108 else if (code == "coa_sent") type = CB_COA_SENT;
00109 else if (code == "reg_received") type = CB_REG_RECEIVED;
00110 else if (code == "routes") type = CB_ROUTES;
00111 else if (code == "unb") type = CB_UNB;
00112 else if (code == "link_announce") type = CB_LINK_ANNOUNCE;
00113 else if (code == "link_available") type = CB_LINK_AVAILABLE;
00114 else if (code == "link_unavailable") type = CB_LINK_UNAVAILABLE;
00115 else if (code == "contact_up") type = CB_CONTACT_UP;
00116 else if (code == "contact_down") type = CB_CONTACT_DOWN;
00117 else type = CB_UNKNOWN;
00118
00119 return true;
00120 }
00121
00122
00123 void
00124 TcaControlBundle::dump(const std::string& intro) const
00125 {
00126 printf("%s code='%s', args=%u\n", intro.c_str(), code_.c_str(),
00127 (u_int)args_.size());
00128 for (unsigned int i=0; i<args_.size(); ++i)
00129 {
00130 printf(" '%s'\n", args_[i].c_str());
00131 }
00132 }
00133
00134
00135 std::string
00136 TcaControlBundle::eat_to_tab(std::string& s)
00137 {
00138
00139
00140
00141 std::string::size_type tab = s.find('\t');
00142 if (tab == std::string::npos)
00143 {
00144 std::string left = s;
00145 s = "";
00146 return left;
00147 }
00148
00149 else
00150 {
00151 std::string left = s.substr(0,tab);
00152 s = s.substr(tab+1, s.length());
00153 return left;
00154 }
00155 }
00156
00157
00159
00160
00161
00162 TcaWrappedBundle::TcaWrappedBundle(const std::string& code,
00163 const std::string& src,
00164 const std::string& dest)
00165 : TcaControlBundle(code)
00166 {
00167 args_.push_back(src);
00168 args_.push_back(dest);
00169 }
00170
00171
00172 const std::string
00173 TcaWrappedBundle::source() const
00174 {
00175 if (args_.size() < 1) return "";
00176 return args_[0];
00177 }
00178
00179
00180 const std::string
00181 TcaWrappedBundle::dest() const
00182 {
00183 if (args_.size() < 2) return "";
00184 return args_[1];
00185 }
00186
00187
00188 }