root/examples/socket_server.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. BOOST_AUTO_TEST_SUITE
  2. BOOST_AUTO_TEST_CASE_TEMPLATE
  3. BOOST_AUTO_TEST_CASE_TEMPLATE
  4. BOOST_AUTO_TEST_CASE_TEMPLATE

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/examples/socket_server.cpp 2300 2018-08-04 19:07:32Z jmmcg $
   3 **
   4 ** Copyright (C) 2015 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include "stdafx.h"
  22 
  23 #define BOOST_TEST_MODULE libjmmcg_tests
  24 #include <boost/test/included/unit_test.hpp>
  25 
  26 #include <boost/test/test_case_template.hpp>
  27 #include <boost/mpl/list.hpp>
  28 
  29 #include "core/max_min.hpp"
  30 #include "core/socket_client_manager.hpp"
  31 #include "core/socket_server.hpp"
  32 
  33 const boost::asio::ip::address localhost(boost::asio::ip::address_v4::loopback());
  34 const unsigned short unused_port=12347u;
  35 
  36 struct msgs_t {
  37         struct Header_t {
  38                 enum : bool {
  39                         has_static_size=true
  40                 };
  41 
  42                 const std::size_t length_;
  43 
  44                 explicit constexpr Header_t(std::size_t l) noexcept(true) : length_(l) {}
  45 
  46                 constexpr std::size_t length() const noexcept(true) {
  47                         return length_;
  48                 }
  49         };
  50         struct message1 : Header_t {
  51                 enum : std::size_t {
  52                         header_t_size=sizeof(Header_t)
  53                 };
  54 
  55                 const uint8_t c=42;
  56 
  57                 constexpr message1() noexcept(true) : Header_t(sizeof(message1)) {}
  58         };
  59         struct message2 : Header_t {
  60                 enum : std::size_t {
  61                         header_t_size=sizeof(Header_t)
  62                 };
  63 
  64                 const uint64_t c=69;
  65 
  66                 constexpr message2() noexcept(true) : Header_t(sizeof(message2)) {}
  67         };
  68         enum : std::size_t {
  69                 min_msg_size=jmmcg::min<std::size_t, sizeof(message1), sizeof(message2)>::value,
  70                 max_msg_size=jmmcg::max<std::size_t, sizeof(message1), sizeof(message2)>::value,
  71                 header_t_size=sizeof(Header_t)
  72         };
  73         using msg_buffer_t=std::array<std::uint8_t, max_msg_size>;
  74 };
  75 
  76 struct just_connect {
  77         template<class Fn>
  78         void operator()(Fn const &f) const {
  79                 const boost::asio::ip::tcp::endpoint endpoint(localhost, unused_port);
  80                 f(endpoint);
  81         }
  82 };
  83 
  84 template<class SktT>
  85 struct simple_reflect {
  86         using socket_t=SktT;
  87         using src_msg_details_t=msgs_t;
  88         struct heartbeats {
  89                 template<class Skt>
  90                 explicit constexpr heartbeats(Skt const &) noexcept(true) {}
  91         };
  92 
  93         template<class Buff>
  94         bool process_msg(Buff &buff, socket_t &, socket_t &client_skt) const {
  95                 client_skt.write(buff);
  96                 return false;
  97         }
  98 };
  99 
 100 typedef boost::mpl::list<
 101         std::pair<
 102                 jmmcg::socket::asio::client_manager,
 103                 jmmcg::socket::svr<
 104                         simple_reflect<jmmcg::socket::asio::client_manager::socket_t>,
 105                         jmmcg::socket::server_manager::loopback<
 106                                 simple_reflect<jmmcg::socket::asio::client_manager::socket_t>::heartbeats,
 107                                 jmmcg::socket::asio::client_manager::socket_t
 108                         >
 109                 >
 110         >,
 111         std::pair<
 112                 jmmcg::socket::glibc::client_manager,
 113                 jmmcg::socket::svr<
 114                         simple_reflect<jmmcg::socket::glibc::client_manager::socket_t>,
 115                         jmmcg::socket::server_manager::loopback<
 116                                 simple_reflect<jmmcg::socket::glibc::client_manager::socket_t>::heartbeats,
 117                                 jmmcg::socket::glibc::client_manager::socket_t
 118                         >
 119                 >
 120         >
 121 > cxns_types;
 122 
 123 BOOST_AUTO_TEST_SUITE(socket_tests)
 124 
 125 BOOST_AUTO_TEST_SUITE(server)
 126 
 127 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor, cxns_t, cxns_types) {
 128         using svr_t=typename cxns_t::second_type;
 129 
 130         simple_reflect<typename cxns_t::first_type::socket_t> proc_rules;
 131         BOOST_CHECK_NO_THROW(svr_t svr(boost::asio::ip::address(), unused_port, proc_rules));
 132 }
 133 
 134 BOOST_AUTO_TEST_CASE_TEMPLATE(single_server_and_client_one_message, cxns_t, cxns_types) {
 135         using client_t=typename cxns_t::first_type;
 136         using svr_t=typename cxns_t::second_type;
 137 
 138         simple_reflect<typename client_t::socket_t> proc_rules;
 139         svr_t svr(boost::asio::ip::address(), unused_port, proc_rules);
 140         client_t skt(msgs_t::min_msg_size, msgs_t::max_msg_size, just_connect());
 141         msgs_t::message1 msg;
 142         BOOST_CHECK_NO_THROW(skt.write(msg));
 143         msgs_t::message1 response;
 144         BOOST_CHECK_NO_THROW(skt.read(response));
 145         BOOST_CHECK_EQUAL(response.c, msg.c);
 146 }
 147 
 148 BOOST_AUTO_TEST_CASE_TEMPLATE(single_server_and_client_two_messages, cxns_t, cxns_types) {
 149         using client_t=typename cxns_t::first_type;
 150         using svr_t=typename cxns_t::second_type;
 151 
 152         simple_reflect<typename client_t::socket_t> proc_rules;
 153         svr_t svr(boost::asio::ip::address(), unused_port, proc_rules);
 154         client_t skt(msgs_t::min_msg_size, msgs_t::max_msg_size, just_connect());
 155         msgs_t::message1 msg1;
 156         BOOST_CHECK_NO_THROW(skt.write(msg1));
 157         msgs_t::message1 response1;
 158         BOOST_CHECK_NO_THROW(skt.read(response1));
 159         BOOST_CHECK_EQUAL(response1.c, msg1.c);
 160         msgs_t::message2 msg2;
 161         BOOST_CHECK_NO_THROW(skt.write(msg2));
 162         msgs_t::message2 response2;
 163         BOOST_CHECK_NO_THROW(skt.read(response2));
 164         BOOST_CHECK_EQUAL(response2.c, msg2.c);
 165 }
 166 
 167 BOOST_AUTO_TEST_CASE_TEMPLATE(two_repeated_servers_and_clients, cxns_t, cxns_types) {
 168         using client_t=typename cxns_t::first_type;
 169         using svr_t=typename cxns_t::second_type;
 170 
 171         simple_reflect<typename client_t::socket_t> proc_rules;
 172         {
 173                 svr_t svr(boost::asio::ip::address(), unused_port, proc_rules);
 174                 client_t skt(msgs_t::min_msg_size, msgs_t::max_msg_size, just_connect());
 175                 msgs_t::message1 msg;
 176                 BOOST_CHECK_NO_THROW(skt.write(msg));
 177                 msgs_t::message1 response;
 178                 BOOST_CHECK_NO_THROW(skt.read(response));
 179                 BOOST_CHECK_EQUAL(response.c, msg.c);
 180         }
 181         {
 182                 svr_t svr(boost::asio::ip::address(), unused_port, proc_rules);
 183                 client_t skt(msgs_t::min_msg_size, msgs_t::max_msg_size, just_connect());
 184                 msgs_t::message2 msg;
 185                 BOOST_CHECK_NO_THROW(skt.write(msg));
 186                 msgs_t::message2 response;
 187                 BOOST_CHECK_NO_THROW(skt.read(response));
 188                 BOOST_CHECK_EQUAL(response.c, msg.c);
 189         }       
 190 }
 191 
 192 BOOST_AUTO_TEST_SUITE_END()
 193 
 194 BOOST_AUTO_TEST_SUITE_END()

/* [<][>][^][v][top][bottom][index][help] */