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 #ifndef _TCA_CONTROLBUNDLE_H_ 00040 #define _TCA_CONTROLBUNDLE_H_ 00041 00042 00043 // Warning: This file is included by both the dtnd and the tca_admin app. 00044 // Any changes here must be okay for both apps. 00045 00046 #include <string> 00047 #include <vector> 00048 00049 00050 // TODO: Make individual subclass for each TcaControlBundle type 00051 // and add strong argument checking (number of args, syntax, even semantic 00052 // correctness in some cases) 00053 00054 // Note: args can be appended manually, using args_.push_back(arg) 00055 00056 class TcaControlBundle 00057 { 00058 public: 00059 00060 enum TypeCode { 00061 CB_NULL, 00062 CB_ADV, 00063 CB_ADV_SENT, 00064 CB_ASK, 00065 CB_ASK_RECEIVED, 00066 CB_ASK_SENT, 00067 CB_COA, 00068 CB_COA_SENT, 00069 CB_REG_RECEIVED, 00070 CB_ROUTES, 00071 CB_UNB, 00072 CB_LINK_ANNOUNCE, 00073 CB_LINK_AVAILABLE, 00074 CB_LINK_UNAVAILABLE, 00075 CB_CONTACT_UP, 00076 CB_CONTACT_DOWN, 00077 CB_UNKNOWN // error: unrecognized code 00078 }; 00079 00080 TypeCode type_; 00081 std::string code_; 00082 std::vector<std::string> args_; 00083 00084 TcaControlBundle() : type_(CB_NULL), code_(), args_() { }; 00085 00086 // construct from Bundle payload, parsing out code and args 00087 // you can also just give a code here to construct an argless bundle 00088 TcaControlBundle(const std::string& payload); 00089 00090 virtual ~TcaControlBundle() { } 00091 00092 // retrieve as string (suitable for sending as bundle payload) 00093 virtual std::string str() const; 00094 00095 void dump(const std::string& intro) const; 00096 00097 // todo: remove this from public interface someday 00098 static std::string eat_to_tab(std::string& s); 00099 00100 protected: 00101 00102 static bool parse_payload(const std::string& payload, 00103 TypeCode& type, 00104 std::string& code, std::string& body); 00105 00106 }; 00107 00108 00109 // A TcaWrappedBundle is a TcaControlBundle that includes a source and 00110 // dest field as args_[0] and args_[1] respectively. This is useful for 00111 // "tunnelling" regular addressed bundles over the control API, preserving 00112 // their source and dest fields. 00113 00114 00115 class TcaWrappedBundle : public TcaControlBundle 00116 { 00117 public: 00118 00119 // construct directly from string 00120 TcaWrappedBundle(const std::string& payload) : TcaControlBundle(payload) {} 00121 00122 // construct from existing ControlBundle 00123 TcaWrappedBundle(const TcaControlBundle& cb) : TcaControlBundle(cb) {} 00124 00125 // construct empty but addressed bundle 00126 TcaWrappedBundle(const std::string& code, 00127 const std::string& src, const std::string& dest); 00128 00129 const std::string source() const; 00130 const std::string dest() const; 00131 00132 void append_arg(const std::string& arg) { args_.push_back(arg); } 00133 }; 00134 00135 00136 #endif /* _TCA_CONTROLBUNDLE_H_ */