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