Kinetic C/C++ Client
 All Classes Functions Variables Pages
nonblocking_packet.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_NONBLOCKING_PACKET_H_
22 #define KINETIC_CPP_CLIENT_NONBLOCKING_PACKET_H_
23 
24 #include <memory>
25 
26 #include "kinetic/common.h"
27 
28 #include "kinetic_client.pb.h"
29 #include "nonblocking_string.h"
30 
31 namespace kinetic {
32 
33 using std::shared_ptr;
34 using std::unique_ptr;
35 using std::string;
36 
37 using com::seagate::kinetic::client::proto::Message;
38 
39 enum State {
40  kMagic,
41  kMessageLength,
42  kValueLength,
43  kMessage,
44  kValue,
45  kFinished
46 };
47 
49  public:
51  virtual NonblockingStringStatus Write() = 0;
52 };
53 
55  public:
56  NonblockingPacketWriter(shared_ptr<SocketWrapperInterface> socket_wrapper, unique_ptr<const Message> message,
57  const shared_ptr<const string> value);
59  NonblockingStringStatus Write();
60 
61  private:
62  bool TransitionFromMagic();
63  void TransitionFromMessageLength();
64  void TransitionFromValueLength();
65  void TransitionFromMessage();
66  void TransitionFromValue();
67  shared_ptr<SocketWrapperInterface> socket_wrapper_;
68  unique_ptr<const Message> message_;
69  const shared_ptr<const string> value_;
70  State state_;
71  NonblockingStringWriter *writer_;
72  std::string serialized_message_;
73  DISALLOW_COPY_AND_ASSIGN(NonblockingPacketWriter);
74 };
75 
77  public:
78  NonblockingPacketReader(shared_ptr<SocketWrapperInterface> socket_wrapper, Message* response, unique_ptr<const string>& value);
80  NonblockingStringStatus Read();
81 
82  private:
83  bool TransitionFromMagic();
84  void TransitionFromMessageLength();
85  void TransitionFromValueLength();
86  void TransitionFromMessage();
87  bool TransitionFromValue();
88  shared_ptr<SocketWrapperInterface> socket_wrapper_;
89  Message* const response_;
90  State state_;
91  unique_ptr<const string>& value_;
92  unique_ptr<const string> magic_;
93  unique_ptr<const string> message_length_;
94  unique_ptr<const string> value_length_;
95  unique_ptr<const string> message_;
96  NonblockingStringReader* reader_;
97  DISALLOW_COPY_AND_ASSIGN(NonblockingPacketReader);
98 };
99 
101  public:
103  virtual unique_ptr<NonblockingPacketWriterInterface> CreateWriter(shared_ptr<SocketWrapperInterface> socket_wrapper,
104  unique_ptr<const Message> message, const shared_ptr<const string> value) = 0;
105 };
106 
108  public:
109  unique_ptr<NonblockingPacketWriterInterface> CreateWriter(shared_ptr<SocketWrapperInterface> socket_wrapper,
110  unique_ptr<const Message> message, const shared_ptr<const string> value);
111 };
112 
113 } // namespace kinetic
114 
115 #endif // KINETIC_CPP_CLIENT_NONBLOCKING_PACKET_H_