ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TCPServer.h
Go to the documentation of this file.
1 
8 // g++ -Wall -g -I/usr/local/boost_1_53_0 -c data/TCPServer.cpp -o data/TCPServer.o
9 //
10 // First implementation come from tutorial boost. TCP server async.\n
11 
12 // Not so easy to run and stop the server in the same thread
13 // Should use some loop to check at some internal bool variable (is_running for instance)
14 // certainly condition variable
15 // http://en.cppreference.com/w/cpp/thread/condition_variable
16 // something like in this example
17 // http://www.cplusplus.com/forum/general/87747
18 
19 
20 #ifndef TCPSERVER_H_
21 #define TCPSERVER_H_
22 
23 #include <string>
24 
25 // from the example, need perror (not used now)
26 //#include <stdio.h>
27 //#include <stdlib.h>
28 //#include <ctime>
29 
30 #include <boost/shared_ptr.hpp>
31 #include <boost/enable_shared_from_this.hpp>
32 #include <boost/asio.hpp>
33 #include <boost/thread/thread.hpp>
34 
35 //c++11 thread
36 #include <thread>
37 #include <mutex>
38 #include <condition_variable>
39 
40 using boost::asio::ip::tcp;
41 
42 class StockManager;
43 
44 
59  : public boost::enable_shared_from_this<tcp_connection>
60 {
61 public:
62  typedef boost::shared_ptr<tcp_connection> pointer;
63 
65  virtual ~tcp_connection() {
66 #ifdef DEBUG_CPP
67  std::cout << "Call public destructor tcp_connection " << std::endl;
68 #endif
69 }
72  static pointer create(boost::asio::io_service& io_service);
75  tcp::socket& socket() { return socket_; }
76 
77  // to modify for my purpose, in RealTime case, nothing to send back ! or just ok, no error !
81  void start();
82 
83  tcp::socket& GetSocket() {return socket_;}
84 
85 
86 private:
88  tcp_connection(boost::asio::io_service& io_service)
89  : socket_(io_service) {
90 #ifdef DEBUG_CPP
91  std::cout << "Call private constructor tcp_connection " << std::endl;
92 #endif
93  }
95  void handle_write(const boost::system::error_code& error,
96  size_t bytes_transferred);
97 
99  tcp::socket socket_;
103  std::string message_;
104 };
105 
122 class TCPServer {
123 
124  public :
138  TCPServer ( StockManager *sm, unsigned int iport );
139 
149  TCPServer ( StockManager *sm, unsigned int iport, boost::asio::io_service& io_service );
151  virtual ~TCPServer();
153 
155  //{@
158  void StartServer();
160  // trick to not have the TCP recreating when deleting
161  // connection is still alive : 1- check is running, 2- delete connection
162  void StopServer();
164  bool IsRunning() {return is_running;}
166 
167  // need to store error in connection and other
168  // a global error message not bad... to send to StockManager
169  // see c++ book exception and threads
170  /*
171  void error(const char *msg) {
172  perror(msg);
173  exit(0);
174  }
175  */
176 
177  private:
179 
180  void start_accept();
187  void handle_accept(tcp_connection::pointer new_connection,
188  const boost::system::error_code& error);
190  void LoopThread();
191 
193 
198  unsigned int port;
200  // seems important to be declared and created before acceptor_
201  boost::asio::io_service my_io_service;
202  // example acceptor contains io_service ?? for async call ?
204  // seems important to be declared in initializer list. Or I never found the correct syntax...
205  tcp::acceptor acceptor_;
211  bool want_exit;
213 
214  std::thread t_;
215  // try to own and delete the connection, not promising
216  //tcp_connection::pointer _connection;
219  std::mutex loop_mutex;
220  std::condition_variable loop_cv;
222 };
223 
224 #endif /* TCPSERVER_H_ */
Singleton class, stores all loaded stocks.
Definition: StockManager.h:42
std::condition_variable loop_cv
Definition: TCPServer.h:220
tcp::acceptor acceptor_
...
Definition: TCPServer.h:205
void start_accept()
create a new tcp_connection to receive message
Definition: TCPServer.cpp:259
tcp_connection(boost::asio::io_service &io_service)
Private constructor.
Definition: TCPServer.h:88
std::mutex loop_mutex
Definition: TCPServer.h:219
void StopServer()
Stop the server.
Definition: TCPServer.cpp:157
std::string message_
Message received by the socket.
Definition: TCPServer.h:103
void StartServer()
Start the server.
Definition: TCPServer.cpp:111
TCPServer(StockManager *sm, unsigned int iport)
Constructor, io_service is a private data member (my_io_service).
Definition: TCPServer.cpp:75
boost::asio::io_service my_io_service
io_service needed for asimc.
Definition: TCPServer.h:201
bool is_running
Indicate if the server is running.
Definition: TCPServer.h:209
static pointer create(boost::asio::io_service &io_service)
Static function to create a new socket.
Definition: TCPServer.cpp:20
bool IsRunning()
Return the status of the server.
Definition: TCPServer.h:164
void LoopThread()
Function run by the internal thread.
Definition: TCPServer.cpp:215
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.
Definition: TCPServer.cpp:285
virtual ~TCPServer()
Destructor.
Definition: TCPServer.cpp:89
Use with RealTime to receive messages from python ServerPortfolio.
Definition: TCPServer.h:122
tcp::socket & GetSocket()
Definition: TCPServer.h:83
unsigned int port
Port where the server listens.
Definition: TCPServer.h:198
std::thread t_
Internal thread.
Definition: TCPServer.h:214
void handle_write(const boost::system::error_code &error, size_t bytes_transferred)
Function called async.
Definition: TCPServer.cpp:44
StockManager * f_sm
Pointer to the StockManager singleton, will receive and treat the messages.
Definition: TCPServer.h:196
tcp::socket & socket()
Return the socket.
Definition: TCPServer.h:75
boost::shared_ptr< tcp_connection > pointer
Definition: TCPServer.h:62
void start()
Function to be executed when a message must be send back.
Definition: TCPServer.cpp:29
bool want_exit
Indicate the thread must exit, set in destructor only.
Definition: TCPServer.h:211
Define one connection of the server.
Definition: TCPServer.h:58
tcp::socket socket_
Store an io_service.
Definition: TCPServer.h:99
virtual ~tcp_connection()
Destructor.
Definition: TCPServer.h:65