Kinetic C/C++ Client
 All Classes Functions Variables Pages
message_stream.h
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 #ifndef KINETIC_CPP_CLIENT_MESSAGE_STREAM_H_
22 #define KINETIC_CPP_CLIENT_MESSAGE_STREAM_H_
23 
24 #include "google/protobuf/message.h"
25 #include "openssl/ssl.h"
26 
27 #include "byte_stream.h"
28 #include "common.h"
29 #include "incoming_value.h"
30 
31 namespace kinetic {
32 
34  public:
35  typedef enum {
36  MessageStreamReadStatus_SUCCESS,
37  MessageStreamReadStatus_INTERNAL_ERROR,
38  MessageStreamReadStatus_TOO_LARGE
39  } MessageStreamReadStatus;
40 
41  virtual ~MessageStreamInterface() {}
42  virtual MessageStreamReadStatus ReadMessage(::google::protobuf::Message *message,
43  IncomingValueInterface** value) = 0;
44  virtual int WriteMessage(const ::google::protobuf::Message &message,
45  const OutgoingValueInterface& value, int* err) = 0;
46 };
47 
49  public:
50  explicit MessageStream(uint32_t max_message_size_bytes, ByteStreamInterface *byte_stream);
51  ~MessageStream();
52  MessageStreamReadStatus ReadMessage(::google::protobuf::Message *message,
53  IncomingValueInterface** value);
54  int WriteMessage(const ::google::protobuf::Message &message,
55  const OutgoingValueInterface& value, int* err);
56 
57  private:
58  bool ReadHeader(uint32_t *message_size, uint32_t *value_size);
59  bool WriteHeader(uint32_t message_size, uint32_t value_size);
60  uint32_t max_message_size_bytes_;
61  ByteStreamInterface *byte_stream_;
62  DISALLOW_COPY_AND_ASSIGN(MessageStream);
63 };
64 
66  public:
67  virtual bool NewMessageStream(int fd, bool use_ssl, SSL *ssl, uint32_t max_message_size_bytes,
68  MessageStreamInterface **message_stream) = 0;
69  virtual ~MessageStreamFactoryInterface() {}
70 };
71 
73  public:
74  MessageStreamFactory(SSL_CTX *ssl_context, IncomingValueFactoryInterface &value_factory);
75  bool NewMessageStream(int fd, bool use_ssl, SSL *ssl, uint32_t max_message_size_bytes,
76  MessageStreamInterface **message_stream);
78 
79  private:
80  SSL_CTX *ssl_context_;
81  SSL *ssl_;
82  bool ssl_created_;
83  IncomingValueFactoryInterface &value_factory_;
84  DISALLOW_COPY_AND_ASSIGN(MessageStreamFactory);
85 };
86 
87 } // namespace kinetic
88 
89 #endif // KINETIC_CPP_CLIENT_MESSAGE_STREAM_H_