Kinetic C/C++ Client
 All Classes Functions Variables Pages
kinetic_connection_factory.cc
1 /*
2  * kinetic-cpp-client
3  * Copyright (C) 2014 Seagate Technology.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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 this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #include "kinetic/kinetic_connection_factory.h"
22 #include "socket_wrapper.h"
23 #include "nonblocking_packet_service.h"
24 #include <exception>
25 #include <stdexcept>
26 
27 namespace kinetic {
28 
29 KineticConnectionFactory NewKineticConnectionFactory() {
30  HmacProvider hmac_provider;
31  return KineticConnectionFactory(hmac_provider);
32 }
33 
34 KineticConnectionFactory::KineticConnectionFactory(
35  HmacProvider hmac_provider)
36  : hmac_provider_(hmac_provider) {}
37 
38 
40  const ConnectionOptions& options,
41  unique_ptr<NonblockingKineticConnection>& connection) {
42  return doNewConnection(options, connection);
43 }
44 
46  const ConnectionOptions& options,
47  shared_ptr<NonblockingKineticConnection>& connection) {
48  unique_ptr<NonblockingKineticConnection> nbc(nullptr);
49  Status status = doNewConnection(options, nbc);
50  if (status.ok())
51  connection.reset(nbc.release());
52  return status;
53 }
54 
56  const ConnectionOptions& options,
57  unique_ptr<ThreadsafeNonblockingKineticConnection>& connection) {
58  unique_ptr<NonblockingKineticConnection> nbc(nullptr);
59  Status status = doNewConnection(options, nbc);
60  if(status.ok())
61  connection.reset(new ThreadsafeNonblockingKineticConnection(std::move(nbc)));
62  return status;
63 }
64 
66  const ConnectionOptions& options,
67  shared_ptr<ThreadsafeNonblockingKineticConnection>& connection) {
68  unique_ptr<NonblockingKineticConnection> nbc(nullptr);
69  Status status = doNewConnection(options, nbc);
70  if(status.ok())
71  connection.reset(new ThreadsafeNonblockingKineticConnection(std::move(nbc)));
72  return status;
73 }
74 
76  const ConnectionOptions& options,
77  unique_ptr<BlockingKineticConnection>& connection,
78  unsigned int network_timeout_seconds) {
79  unique_ptr<NonblockingKineticConnection> nbc(nullptr);
80  Status status = doNewConnection(options, nbc);
81  if(status.ok())
82  connection.reset(new BlockingKineticConnection(std::move(nbc), network_timeout_seconds));
83  return status;
84 }
85 
87  const ConnectionOptions& options,
88  shared_ptr<BlockingKineticConnection>& connection,
89  unsigned int network_timeout_seconds) {
90  unique_ptr<NonblockingKineticConnection> nbc(nullptr);
91  Status status = doNewConnection(options, nbc);
92  if(status.ok())
93  connection.reset(new BlockingKineticConnection(std::move(nbc), network_timeout_seconds));
94  return status;
95 }
96 
98  const ConnectionOptions& options,
99  unique_ptr<ThreadsafeBlockingKineticConnection>& connection,
100  unsigned int network_timeout_seconds) {
101  unique_ptr<BlockingKineticConnection> bc(nullptr);
102  Status status = NewBlockingConnection(options, bc, network_timeout_seconds);
103  if(status.ok())
104  connection.reset(new ThreadsafeBlockingKineticConnection(std::move(bc)));
105  return status;
106 }
107 
109  const ConnectionOptions& options,
110  shared_ptr<ThreadsafeBlockingKineticConnection>& connection,
111  unsigned int network_timeout_seconds) {
112  unique_ptr<BlockingKineticConnection> bc(nullptr);
113  Status status = NewBlockingConnection(options, bc, network_timeout_seconds);
114  if(status.ok())
115  connection.reset(new ThreadsafeBlockingKineticConnection(std::move(bc)));
116  return status;
117 }
118 
119 Status KineticConnectionFactory::doNewConnection(
120  ConnectionOptions const& options,
121  unique_ptr <NonblockingKineticConnection>& connection) {
122  try{
123  auto socket_wrapper = make_shared<SocketWrapper>(options.host, options.port, options.use_ssl, true);
124  if (!socket_wrapper->Connect())
125  throw std::runtime_error("Could not connect to socket.");
126 
127  shared_ptr<NonblockingReceiverInterface> receiver;
128  receiver = shared_ptr<NonblockingReceiverInterface>(new NonblockingReceiver(socket_wrapper, hmac_provider_, options));
129 
130  auto writer_factory =
131  unique_ptr<NonblockingPacketWriterFactoryInterface>(new NonblockingPacketWriterFactory());
132  auto sender = unique_ptr<NonblockingSenderInterface>(new NonblockingSender(socket_wrapper,
133  receiver, move(writer_factory), hmac_provider_, options));
134 
135  NonblockingPacketService *service = new NonblockingPacketService(socket_wrapper, move(sender), receiver);
136  connection.reset(new NonblockingKineticConnection(service));
137 
138  } catch(std::exception& e){
139  return Status::makeInternalError("Connection error: "+std::string(e.what()));
140  }
141  return Status::makeOk();
142 }
143 } // namespace kinetic
static Status makeInternalError(std::string error_message)
Helper for easily making a Status object indicating a generic error.
Definition: status.h:49
Kinetic connection class variant that synchronizes concurrent access and allows non-blocking IO...
Status NewThreadsafeNonblockingConnection(const ConnectionOptions &options, unique_ptr< ThreadsafeNonblockingKineticConnection > &connection)
Like NewNonblockingConnection, except the connection is safe for use by multiple threads.
Status NewNonblockingConnection(const ConnectionOptions &options, unique_ptr< NonblockingKineticConnection > &connection)
Creates and opens a new nonblocking connection using the given options. If the returned Status indica...
Status NewBlockingConnection(const ConnectionOptions &options, unique_ptr< BlockingKineticConnection > &connection, unsigned int network_timeout_seconds)
Creates and opens a new blocking connection using the given options. If the returned Status indicates...
static Status makeOk()
Helper for easily making a Status object indicating success.
Definition: status.h:44
bool ok() const
True if the operation succeeded.
Definition: status.h:54
Status NewThreadsafeBlockingConnection(const ConnectionOptions &options, unique_ptr< ThreadsafeBlockingKineticConnection > &connection, unsigned int network_timeout_seconds)
Like NewBlockingConnection, except the connection is safe for use by multiple threads.
Use this struct to pass all connection options to the KineticConnectionFactory.
Indicates the success/failure of an operation. Frequently when calling a Kinetic client method you'll...
Definition: status.h:41