ztsdb
net_client.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef NET_CLIENT_HPP
20 #define NET_CLIENT_HPP
21 
22 
23 #include <poll.h>
24 #include "encode.hpp"
25 #include "net_handler.hpp"
26 
27 
28 namespace client {
29 
30  enum IncomingRspState {
31  RSP_IDLE,
32  RSP,
33  RSP_DONE
34  };
35 
36 
37  struct RspState {
38  RspState() : state(RSP_IDLE), valstack_idx(0), reqid(0) { }
39  RspState(Global::reqid_t reqid_p) : state(RSP_IDLE), valstack_idx(0), reqid(reqid_p) { }
40 
41  IncomingRspState state;
42 
43  size_t valstack_idx;
44  vector<zcore::ValState> valstack;
45 
46  Global::reqid_t reqid;
47  };
48 
49 
50  struct Client {
51 
52  Client(net::NetHandler& com_p,
53  int fd_read_data_p,
54  int fd_read_sig_p);
55 
56  int run();
57 
58  // this one will create a new connection, and hence a new RspState
59  Global::conn_id_t connect(const string& ip, int port);
60 
61  size_t sendReq(Global::conn_id_t peerid,
62  Global::conn_id_t sourceid,
63  const E* e,
64  const val::SpVList& boundvars);
65 
66  int readRspData(Global::reqid_t reqid,
67  Global::reqid_t sourceid,
68  const char* buf,
69  size_t len);
70 
71  const val::Value& getVal() const;
72 
73  private:
74  net::NetHandler& com;
75 
76  RspState rsp; // incoming response
77  nfds_t nfds;
78 
79  int fd_read_data;
80  int fd_read_sig;
81  static const nfds_t nfds_max = 2;
82  struct pollfd fds[nfds_max];
83  };
84 
85 }
86 
87 
88 #endif
89 
E
Definition: ast.hpp:76
arr::cow_ptr
Definition: cow_ptr.hpp:42
client::Client
Definition: net_client.hpp:50
net::NetHandler
Definition: net_handler.hpp:187
client::RspState
Definition: net_client.hpp:37
state
Definition: tz.hpp:51