7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for the ui network Connection class"
12 #include <boost/assign/list_of.hpp>
13 #include <boost/test/unit_test.hpp>
22 #define NETWORK_PORT 62784
23 #define NETWORK_HOST "127.0.0.1"
25 using namespace boost;
52 virtual void error(
const std::string &message)
54 messages.push_back( message );
65 SignalFrame frame(
"message",
"cpath:/",
"cpath:/" );
67 frame.
set_option(
"text", cf3::common::class_name<std::string>(), message );
91 init_connect( endpoint );
98 connection = TCPConnection::create( io_service );
99 tcp::socket& socket = connection->socket();
101 connection->set_error_handler(error_handler);
103 socket.async_connect( endpoint,
106 asio::placeholders::error )
114 connection->read( args,
117 asio::placeholders::error )
125 connection->send( frame,
128 asio::placeholders::error
137 last_callback_info.error_raised = error;
145 last_callback_info.error_raised = error;
152 connection->socket().close();
159 last_callback_info.error_raised = error;
198 m_acceptor( io_service, tcp::endpoint( tcp::v4(),
NETWORK_PORT ) )
207 m_acceptor.async_accept( conn->socket(),
211 asio::placeholders::error ) );
222 asio::placeholders::error )
234 asio::placeholders::error
243 last_callback_info.error_raised = error;
245 last_callback_info.connection = conn;
252 info.
id = m_clients.size() - 1;
259 last_callback_info.error_raised = error;
261 last_callback_info.connection = conn;
264 if( error == boost::asio::error::eof )
265 m_clients.erase(conn);
272 last_callback_info.error_raised = error;
274 last_callback_info.connection = conn;
277 if( error == boost::asio::error::eof )
278 m_clients.erase(conn);
283 std::map<TCPConnection::ConstPtr, ClientInfo>
m_clients;
292 BOOST_AUTO_TEST_SUITE( uiNetworkConnectionSuite )
298 asio::io_service ios;
307 BOOST_CHECK_EQUAL ( info.
error_raised, asio::error::connection_refused );
314 asio::io_service ios_server;
315 Server server ( ios_server );
317 std::vector<Client*> clients;
318 std::vector<asio::io_service*> clients_ios(3);
319 std::vector<std::string> client_msgs(3);
321 client_msgs[0] =
"I am client ONE: the first one that has been created!";
322 client_msgs[1] =
"I am client TWO: the second one that has been created!";
323 client_msgs[2] =
"I am client THREE: the last one that has been created!";
325 clients_ios[0] =
new asio::io_service();
326 clients_ios[1] =
new asio::io_service();
327 clients_ios[2] =
new asio::io_service();
333 for (
int i = 0 ; i < 3 ; ++i )
335 asio::io_service & ios = *clients_ios[i];
343 ios_server.run_one();
345 clients.push_back( client );
354 std::map<TCPConnection::ConstPtr, Server::ClientInfo>::iterator it = server.
m_clients.begin();
357 for( ; it != server.
m_clients.end() ; ++it )
358 server.
init_read( it->second.connection, it->second.buffer );
361 for(
int i = 0 ; i < 3 ; ++i )
364 clients[i]->init_send( frame );
370 for (
int i = 0 ; i < 3 ; ++i )
372 int cnt = ios_server.run_one();
376 BOOST_CHECK_EQUAL ( info_server.
error_raised, boost::system::errc::success );
380 int client_id = client.
id;
384 std::cout << msg << std::endl;
388 BOOST_CHECK_EQUAL( msg, client_msgs[client_id] );
402 asio::io_service ios_server;
403 Server server ( ios_server );
405 std::vector<Client*> clients;
406 std::vector<asio::io_service*> clients_ios(3);
407 std::vector<std::string> client_msgs(3);
408 std::vector<std::string> server_msgs(3);
410 client_msgs[0] =
"I am client ONE: the first one that has been created!";
411 client_msgs[1] =
"I am client TWO: the second one that has been created!";
412 client_msgs[2] =
"I am client THREE: the last one that has been created!";
414 server_msgs[0] =
"Hello client ONE, how is it going?";
415 server_msgs[1] =
"Hello client TWO, is the weather nice at your place?";
416 server_msgs[2] =
"Hello client THREE, it is quite cold here!";
418 clients_ios[0] =
new asio::io_service();
419 clients_ios[1] =
new asio::io_service();
420 clients_ios[2] =
new asio::io_service();
426 for (
int i = 0 ; i < 3 ; ++i )
428 asio::io_service & ios = *clients_ios[i];
436 ios_server.run_one();
440 BOOST_CHECK_EQUAL ( info.
error_raised, boost::system::errc::success );
443 BOOST_CHECK_EQUAL ( info_server.
error_raised, boost::system::errc::success );
445 clients.push_back( client );
460 std::map<TCPConnection::ConstPtr, Server::ClientInfo>::iterator it = server.
m_clients.begin();
464 for( ; it != server.
m_clients.end() ; ++it )
465 server.
init_read( it->second.connection, it->second.buffer );
467 for(
int i = 0 ; i < 3 ; ++i )
470 clients[i]->init_send( frame );
475 for (
int i = 0 ; i < 3 ; ++i )
477 int cnt = ios_server.run_one();
479 std::cout << cnt << std::endl;
482 BOOST_CHECK_EQUAL ( info_server.
error_raised, boost::system::errc::success );
490 int client_id = client.
id;
494 std::cout << msg << std::endl;
498 BOOST_CHECK_EQUAL( msg, client_msgs[client_id] );
533 BOOST_AUTO_TEST_SUITE_END()
SignalFrame generate_message_frame(const std::string &message)
void to_string(const XmlNode &node, std::string &str)
virtual void error(const std::string &message)
boost::shared_ptr< XmlDoc > xml_doc
void init_read(TCPConnection::Ptr conn, SignalFrame &buffer)
external boost library namespace
Network layer for XML communications.
Classes that implement the XML protocol for use in COOLFluiD.
XmlNode set_option(const std::string &value_key, const std::string type_name, const std::string &value_str, const std::string &descr=std::string())
boost::shared_ptr< TCPConnection const > ConstPtr
TCPConnection::ConstPtr connection
BOOST_AUTO_TEST_CASE(connect_failure)
Client(asio::io_service &ios)
void callback_read(TCPConnection::Ptr conn, const boost::system::error_code &error)
boost::shared_ptr< TCPConnection > Ptr
void callback_read(const boost::system::error_code &error)
void callback_accept(TCPConnection::Ptr conn, const boost::system::error_code &error)
boost::shared_ptr< ErrorHandler > error_handler
boost::shared_ptr< ErrorHandler > error_handler
asio::io_service & io_service
void init_connect(tcp::endpoint &endpoint)
std::map< TCPConnection::ConstPtr, ClientInfo > m_clients
void callback_send(TCPConnection::Ptr conn, const boost::system::error_code &error)
const TYPE value(const std::string &opt_name) const
Get the value of the option with given name.
void init_send(SignalFrame &frame)
void callback_connect(const boost::system::error_code &error)
std::vector< std::string > messages
std::string get_message(const SignalFrame &frame)
LastCallbackInfo last_callback_info
void init_send(TCPConnection::Ptr conn, SignalFrame &buffer)
SignalOptions & options(const std::string &name=std::string())
LastCallbackInfo last_callback_info
unsigned int Uint
typedef for unsigned int
void callback_send(const boost::system::error_code &error)
Server(asio::io_service &io_service)
boost::system::error_code error_raised
TCPConnection::Ptr connection
TCPConnection::Ptr connection