14 #include <boost/bind.hpp>
15 #include <boost/asio/streambuf.hpp>
22 std::cout <<
"tcp_connection::create return a new tcp_connection" << std::endl;
32 std::cout <<
" tcp_connection::start() will associate handle_write" << std::endl;
39 boost::asio::placeholders::error,
40 boost::asio::placeholders::bytes_transferred));
48 std::cout <<
"Entry connection::handle_write, do nothing " << std::endl;
55 std::cout <<
"tcp_connection::handle_write get error " << error << std::endl;
64 f_sm( sm ), port( iport ), acceptor_( io_service, tcp::endpoint(tcp::v4(), iport)) {
66 std::cout <<
"Constructor TCPServer port with stockmanager " <<
port <<
" " <<
f_sm << std::endl;
76 f_sm( sm ), port( iport ), my_io_service(), acceptor_( my_io_service, tcp::endpoint(tcp::v4(), iport)) {
78 std::cout <<
"Constructor TCPServer port with stockmanager " <<
port <<
" " <<
f_sm << std::endl;
92 std::cout <<
"Destructor TCPServer " << std::endl;
114 std::cout <<
"Entry TCPServer::StartServer() TCPServer port " <<
port << std::endl;
146 std::unique_lock<std::mutex> lock(
loop_mutex );
159 std::cout <<
"TCPServer::StopServer is_running " <<
is_running << std::endl;
165 std::unique_lock<std::mutex> lock(
loop_mutex );
217 std::cout <<
"TCPServer::LoopThread " << std::endl;
228 std::unique_lock<std::mutex> lock(
loop_mutex );
232 std::cout <<
"TCPServer::LoopThread will wait a notify " << std::endl;
239 std::cout <<
"LoopThread will exit and terminate" << std::endl;
245 std::cout <<
"TCPServer::LoopThread() before acceptor_.get_io_service().run() " << std::endl;
262 std::cout <<
"TCPServer::start_accept create connection & async_accept" << std::endl;
277 acceptor_.async_accept( new_connection->socket(),
279 boost::asio::placeholders::error));
286 const boost::system::error_code& error)
289 std::cout <<
"TCPServer::handle_accept, new_connection->socket().is_open() " << new_connection->socket().is_open() << std::endl;
290 std::cout <<
"is_running " <<
is_running << std::endl;
293 boost::asio::streambuf message;
294 std::string tmp_string;
298 std::cout <<
" TCPServer::handle_accept() error: " << error << std::endl;
302 boost::asio::read_until( new_connection->socket(), message,
" END");
310 std::istream is(&message);
311 std::getline(is, tmp_string);
313 tmp_string.erase(tmp_string.size()-4);
320 std::cout <<
"before SendDataCSV" << std::endl;
323 }
catch (boost::system::system_error &e) {
324 std::cout <<
"Got exception boost::system::system_error from boost:asio::read_until() " << e.what() << std::endl;
328 std::cout <<
"\nTCPServer, got a StockException " << e.
what() << std::endl;
338 }
catch (std::exception &e ) {
339 std::cout <<
"Got base std::exception " << e.what() << std::endl;
341 std::cout <<
"Got unknow ... exception " << std::endl;
365 std::cout <<
"End TCPServer::handle_accept()" << std::endl;
Singleton class, stores all loaded stocks.
std::condition_variable loop_cv
tcp::acceptor acceptor_
...
virtual const char * what() const noexcept
void start_accept()
create a new tcp_connection to receive message
tcp_connection(boost::asio::io_service &io_service)
Private constructor.
void StopServer()
Stop the server.
Base Exception for Stock and StockManager, all src_cpp code.
std::string message_
Message received by the socket.
void StartServer()
Start the server.
TCPServer(StockManager *sm, unsigned int iport)
Constructor, io_service is a private data member (my_io_service).
bool is_running
Indicate if the server is running.
static pointer create(boost::asio::io_service &io_service)
Static function to create a new socket.
void LoopThread()
Function run by the internal thread.
void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code &error)
handle the received message: parse and send string data to StockManager.
void SendDatadCSV(const std::string &message)
Extract info from the message coming from TCPServer, update the Lists and notify observers (GUIMainCo...
virtual ~TCPServer()
Destructor.
Stores all stocks and defines convenient functions.
unsigned int port
Port where the server listens.
std::thread t_
Internal thread.
void handle_write(const boost::system::error_code &error, size_t bytes_transferred)
Function called async.
StockManager * f_sm
Pointer to the StockManager singleton, will receive and treat the messages.
boost::shared_ptr< tcp_connection > pointer
void start()
Function to be executed when a message must be send back.
bool want_exit
Indicate the thread must exit, set in destructor only.
tcp::socket socket_
Store an io_service.
Server to receive new Instantaneous data from ServerPortfolio.