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