00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __GLOVE_COMM_CONNECTION_HPP_
00028 #define __GLOVE_COMM_CONNECTION_HPP_
00029
00030 #include "../config.hpp"
00031
00032
00033
00034 #include <string>
00035 #include <vector>
00036
00037 #include <boost/foreach.hpp>
00038 #include <boost/exception.hpp>
00039 #include <boost/bind.hpp>
00040 #include <boost/asio.hpp>
00041 #include <boost/algorithm/string.hpp>
00042 #include <boost/algorithm/string_regex.hpp>
00043
00044 #include "msg.hpp"
00045
00046 namespace glove {
00047 namespace comm {
00048
00055 class API illegal_response: public std::runtime_error {
00056 public:
00061 illegal_response(const std::string& response) :
00062 runtime_error("Illegal response: " + response) {
00063 }
00064 };
00065
00075 class API Connection {
00076 boost::asio::ip::tcp::socket& socket;
00077
00078 public:
00083 Connection(boost::asio::ip::tcp::socket& s);
00087 virtual ~Connection();
00088
00100 void write(const std::string msg);
00112 std::string read();
00113
00125 template<typename T>
00126 T get() {
00127 T t;
00128 std::string str = read();
00129 std::istringstream response(str);
00130 try {
00131 response >> t;
00132 } catch (std::ios_base::failure&) {
00133 boost::throw_exception(illegal_response(str));
00134 }
00135 return t;
00136 }
00137
00150 template<typename T>
00151 const std::vector<T> getN(const char separator = ',') {
00152 std::vector<std::string> v;
00153 std::string response = read();
00154 boost::algorithm::split(v, response, boost::is_any_of(std::string(1, separator)));
00155
00156 std::vector<T> result;
00157 BOOST_FOREACH(std::string str, v)
00158 {
00159 T t;
00160 std::istringstream response(str);
00161 try {
00162 response >> t;
00163 } catch (std::ios_base::failure&) {
00164 boost::throw_exception(illegal_response(str));
00165 }
00166 result.push_back(t);
00167 }
00168 return result;
00169 }
00170
00179 void getACK();
00190 std::string getString();
00191
00206 const std::vector<std::string> getStringList();
00207 };
00208
00209 }
00210 }
00211
00212 #endif