TcaControlBundle.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * University of Waterloo Open Source License
00008  * 
00009  * Copyright (c) 2005 University of Waterloo. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  *  
00022  *   Neither the name of the University of Waterloo nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00030  * OF WATERLOO OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00033  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00034  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00035  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00036  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 
00040 #include "TcaControlBundle.h"
00041 #include "../../servlib/bundling/Bundle.h"
00042 
00043 
00044 namespace dtn {
00045 
00046 
00048 // class TcaControlBundle
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         // no colon -- assume it's a zero-operand code
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     // return the first part of s, up to the first tab char or end of string
00139     // consuming it (ie. removing it and the tab from the front of s
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 // class TcaControlBundle
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 } // namespace dtn

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1