1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #ifndef DATASTAX_INTERNAL_SESSION_HPP
18 #define DATASTAX_INTERNAL_SESSION_HPP
19 
20 #include "allocated.hpp"
21 #include "metrics.hpp"
22 #include "mpmc_queue.hpp"
23 #include "request_processor.hpp"
24 #include "session_base.hpp"
25 
26 #include <uv.h>
27 
28 namespace datastax { namespace internal { namespace core {
29 
30 class RequestProcessorInitializer;
31 class Statement;
32 
33 class Session
34     : public Allocated
35     , public SessionBase
36     , public RequestProcessorListener {
37 public:
38   Session();
39   ~Session();
40 
41   Future::Ptr prepare(const char* statement, size_t length);
42 
43   Future::Ptr prepare(const Statement* statement);
44 
45   Future::Ptr execute(const Request::ConstPtr& request);
46 
47 private:
48   void execute(const RequestHandler::Ptr& request_handler);
49 
50   void join();
51 
52 private:
53   // Session base methods
54 
55   virtual void on_connect(const Host::Ptr& connected_host, ProtocolVersion protocol_version,
56                           const HostMap& hosts, const TokenMap::Ptr& token_map,
57                           const String& local_dc);
58 
59   virtual void on_close();
60 
61   using SessionBase::on_close; // Intentional overload
62 
63 private:
64   // Cluster listener methods
65 
66   virtual void on_host_up(const Host::Ptr& host);
67 
68   virtual void on_host_down(const Host::Ptr& host);
69 
70   virtual void on_host_added(const Host::Ptr& host);
71 
72   virtual void on_host_removed(const Host::Ptr& host);
73 
74   virtual void on_token_map_updated(const TokenMap::Ptr& token_map);
75 
76   virtual void on_host_maybe_up(const Host::Ptr& host);
77 
78   virtual void on_host_ready(const Host::Ptr& host);
79 
80 private:
81   // Request processor listener methods
82 
83   virtual void on_pool_up(const Address& address);
84 
85   virtual void on_pool_down(const Address& address);
86 
87   virtual void on_pool_critical_error(const Address& address, Connector::ConnectionError code,
88                                       const String& message);
89 
90   virtual void on_keyspace_changed(const String& keyspace,
91                                    const KeyspaceChangedHandler::Ptr& handler);
92 
93   virtual void on_prepared_metadata_changed(const String& id,
94                                             const PreparedMetadata::Entry::Ptr& entry);
95 
96   virtual void on_close(RequestProcessor* processor);
97 
98   using RequestProcessorListener::on_connect; // Intentional overload
99 
100 private:
101   friend class SessionInitializer;
102 
103 private:
104   ScopedPtr<RoundRobinEventLoopGroup> event_loop_group_;
105   uv_mutex_t mutex_;
106   RequestProcessor::Vec request_processors_;
107   size_t request_processor_count_;
108   bool is_closing_;
109 };
110 
111 }}} // namespace datastax::internal::core
112 
113 EXTERNAL_TYPE(datastax::internal::core::Session, CassSession)
114 
115 #endif
116