DAQ  1.0.0
Data Acquisition System (basic demo)
session.h
Go to the documentation of this file.
1 /*! @brief This file have the interface for Session class.
2  @file session.h
3  @author Alvaro Denis <denisacostaq@gmail.com>
4  @date 6/29/2019
5 
6  @copyright
7  @attention <h1><center><strong>COPYRIGHT &copy; 2019 </strong>
8  [<strong>denisacostaq</strong>][denisacostaq-URL].
9  All rights reserved.</center></h1>
10  @attention This file is part of [<strong>DAQs</strong>][DAQs-URL].
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions
14  are met:
15  - 1. Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  - 2. Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20  - 3. Neither the name of the University nor the names of its contributors
21  may be used to endorse or promote products derived from this software
22  without specific prior written permission.
23 
24  THIS PRODUCT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  THIS PRODUCT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35  [denisacostaq-URL]: https://about.me/denisacostaq "Alvaro Denis Acosta"
36  [DAQs-URL]: https://github.com/denisacostaq/DAQs "DAQs"
37  */
38 #ifndef DAQS_DATABASESERVER_SESSION_H
39 #define DAQS_DATABASESERVER_SESSION_H
40 
41 #include <cstdlib>
42 #include <functional>
43 #include <iostream>
44 #include <memory>
45 #include <utility>
46 
47 #include <messages.pb.h>
48 #include <boost/asio.hpp>
49 
50 #include "src/database-server/data-access/idataaccess.h"
51 
53  public:
54  Session(boost::asio::ip::tcp::socket socket, IDataAccess* da);
55 
56  void start() { do_read(); }
57 
58  private:
59  void read_header();
60 
61  void read_save_value_request(std::size_t b_size);
62 
63  void read_get_values_request(std::size_t b_size);
64 
65  void read_get_variables_request(std::size_t b_size);
66 
67  void read_add_variable_request(std::size_t b_size);
68 
69  void read_body(message::MessageType msg_type, std::size_t b_size);
70 
71  void do_read() { read_header(); }
72 
74 
78 
82 
86 
89 
92 
93  void send_status_response(const std::string& msg,
94  message::ResponseStatus status,
95  message::MessageType* prev_msg = nullptr);
96 
97  void send_values_response(std::vector<VarValue>&& values);
98 
99  void send_variables_response(std::vector<Variable>&& variables);
100 
101  boost::asio::ip::tcp::socket socket_;
103 
104  static constexpr size_t max_values_ammount{120};
105 };
106 
107 #endif // DAQS_DATABASESERVER_SESSION_H
void send_variables_response(std::vector< Variable > &&variables)
Definition: session.cc:112
Session(boost::asio::ip::tcp::socket socket, IDataAccess *da)
Definition: session.cc:43
void read_get_variables_request(std::size_t b_size)
Definition: session.cc:340
IDataAccess * da_
Definition: session.h:102
virtual std::tuple< std::vector< VarValue >, Err > fetch_variable_values(const std::string &var_name, const std::chrono::system_clock::time_point &start_date, const std::chrono::system_clock::time_point &end_date, size_t max_len=std::numeric_limits< size_t >::infinity()) noexcept=0
fetch_variable_values get values for a variable in a date range.
std::unique_ptr< std::uint8_t[]> build_b_response(std::vector< Variable > &&vars, std::size_t *out_b_size)
Definition: session.cc:153
boost::asio::ip::tcp::socket socket_
Definition: session.h:101
void read_body(message::MessageType msg_type, std::size_t b_size)
Definition: session.cc:218
void send_status_response(const std::string &msg, message::ResponseStatus status, message::MessageType *prev_msg=nullptr)
Definition: session.cc:90
void start()
Definition: session.h:56
void send_msg(std::shared_ptr< uint8_t > f_buf, std::size_t f_size)
Definition: session.cc:201
void send_values_response(std::vector< VarValue > &&values)
Definition: session.cc:102
void read_add_variable_request(std::size_t b_size)
Definition: session.cc:241
void read_get_values_request(std::size_t b_size)
Definition: session.cc:305
void read_header()
Definition: session.cc:46
void do_read()
Definition: session.h:71
void read_save_value_request(std::size_t b_size)
Definition: session.cc:273