TcaControlBundle.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-2006 University of Waterloo
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 
00019 #include "TcaControlBundle.h"
00020 #include "../../servlib/bundling/Bundle.h"
00021 
00022 
00023 namespace dtn {
00024 
00025 
00027 // class TcaControlBundle
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         // no colon -- assume it's a zero-operand code
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     // return the first part of s, up to the first tab char or end of string
00118     // consuming it (ie. removing it and the tab from the front of s
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 // class TcaControlBundle
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 } // namespace dtn

Generated on Thu Jun 7 12:54:29 2007 for DTN Reference Implementation by  doxygen 1.5.1