Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // copyright 2011 t. schneider tes@mit.edu 00002 // 00003 // this file is part of goby-acomms, a collection of libraries for acoustic underwater networking 00004 // 00005 // This program is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This software is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00017 00018 #ifndef AcommsHelpers20110208H 00019 #define AcommsHelpers20110208H 00020 00021 #include <string> 00022 #include <limits> 00023 #include <bitset> 00024 00025 #ifdef CRYPTOPP_PATH_USES_PLUS_SIGN 00026 #include <crypto++/filters.h> 00027 #include <crypto++/hex.h> 00028 #else 00029 #include <cryptopp/filters.h> 00030 #include <cryptopp/hex.h> 00031 #endif 00032 00033 #include "goby/protobuf/modem_message.pb.h" 00034 #include "goby/protobuf/xml_config.pb.h" 00035 #include "goby/core/core_constants.h" 00036 00037 namespace goby 00038 { 00039 00040 namespace acomms 00041 { 00042 inline void hex_decode(const std::string& in, std::string* out) 00043 { 00044 CryptoPP::HexDecoder hex(new CryptoPP::StringSink(*out)); 00045 hex.Put((byte*)in.c_str(), in.size()); 00046 hex.MessageEnd(); 00047 } 00048 00049 inline std::string hex_decode(const std::string& in) 00050 { 00051 std::string out; 00052 hex_decode(in, &out); 00053 return out; 00054 } 00055 00056 inline void hex_encode(const std::string& in, std::string* out) 00057 { 00058 const bool uppercase = false; 00059 CryptoPP::HexEncoder hex(new CryptoPP::StringSink(*out), uppercase); 00060 hex.Put((byte*)in.c_str(), in.size()); 00061 hex.MessageEnd(); 00062 } 00063 00064 inline std::string hex_encode(const std::string& in) 00065 { 00066 std::string out; 00067 hex_encode(in, &out); 00068 return out; 00069 } 00070 00071 // provides stream output operator for Google Protocol Buffers Message 00072 inline std::ostream& operator<<(std::ostream& out, const google::protobuf::Message& msg) 00073 { 00074 return (out << "[[" << msg.GetDescriptor()->name() <<"]] " << msg.ShortDebugString()); 00075 } 00076 00077 00078 class ManipulatorManager 00079 { 00080 public: 00081 void add(unsigned id, goby::acomms::protobuf::MessageFile::Manipulator manip) 00082 { 00083 manips_.insert(std::make_pair(id, manip)); 00084 } 00085 00086 bool has(unsigned id, goby::acomms::protobuf::MessageFile::Manipulator manip) const 00087 { 00088 typedef std::multimap<unsigned, goby::acomms::protobuf::MessageFile::Manipulator>::const_iterator iterator; 00089 std::pair<iterator,iterator> p = manips_.equal_range(id); 00090 00091 for(iterator it = p.first; it != p.second; ++it) 00092 { 00093 if(it->second == manip) 00094 return true; 00095 } 00096 00097 return false; 00098 } 00099 00100 void clear() 00101 { 00102 manips_.clear(); 00103 } 00104 00105 private: 00106 // manipulator multimap (no_encode, no_decode, etc) 00107 // maps DCCL ID (unsigned) onto Manipulator enumeration (xml_config.proto) 00108 std::multimap<unsigned, goby::acomms::protobuf::MessageFile::Manipulator> manips_; 00109 00110 }; 00111 00112 00113 00114 00115 } 00116 } 00117 00118 #endif