All Classes Namespaces Functions Variables Enumerations Enumerator Pages
client_protocol.h
1 /*
2  Copyright (c) 2014 by Elvis Angelaccio
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21 */
22 
23 #ifndef CLIENT_PROTOCOL_H
24 #define CLIENT_PROTOCOL_H
25 
26 #include <string>
27 #include <vector>
28 #include <map>
29 #include <set>
30 
31 #include "protocol.h"
32 
33 namespace net
34 {
35 
41  {
42  public:
45 
46  ResponseCode getResponseCode() const;
47 
48  friend std::ostream& operator<<(std::ostream& os, const ResponseMessage& msg);
49 
50  protected:
51 
52  ResponseCode responseCode;
53  };
54 
55 
61  {
62  public:
64 
65  void setSequenceNumber(int sn);
66 
67  friend std::ostream& operator<<(std::ostream& os, const InsertResponse& msg);
68 
69  private:
70 
71  int sequenceNumber;
72  };
73 
79  {
80  public:
82 
83  void setSdSize(int s);
84  int getSdSize() const;
85  void setSequenceNumber(int sn);
86  int getSequenceNumber() const;
87  void setFilesNumber(int n);
88  int getFilesNumber() const;
89  const std::map<std::string, std::string>& getSdFields() const;
90  const std::map<std::string, std::string>& getDataItems() const;
91  std::map<std::string, std::string>& getDataItems();
92  void addSetDescriptorField(const std::string& key, const std::string& value);
93  void addDataItem(const std::string& itemName, const std::string& itemFile);
94 
95  friend std::ostream& operator<<(std::ostream& os, const GetResponse& msg);
96 
97  private:
98 
99  int sdSize;
100  int sequenceNumber;
101  int filesNumber;
102  std::map<std::string, std::string> setDescriptorFields;
103  std::map<std::string, std::string> dataItems;
104  };
105 
106 
112  {
113  public:
115 
116  void setFoundCount(int c);
117  int getFoundCount() const;
118  void addSdFields(int sn, const std::map<std::string, std::string>& fields);
119 
120  friend std::ostream& operator<<(std::ostream& os, const SearchResponse& msg);
121 
122  private:
123 
124  int foundCount;
126  std::map<int, std::map<std::string, std::string> > setDescriptorFields;
127  };
128 
135  {
136  public:
137 
139 
140  void setDssNumber(int n);
141  void addDssEntry(const std::string& dssEntry);
142 
143  friend std::ostream& operator<<(std::ostream& os, const SpeclistResponse& msg);
144 
145  private:
146 
147  int dssNumber;
148  std::vector<std::string> dssList;
149  };
150 
151 
158  {
159  public:
160 
161  /*
162  * Create the "raw" messages with the client requests, starting from a "structured" input (i.e. single strings and containters).
163  * The given input is formatted in a single string according to the protocol syntax.
164  */
165  std::string createInsertRequest(const std::string& dssName, std::map<std::string, std::string>& sdFields, const std::map<std::string, std::string>& dataItems);
166  std::string createRemoveRequest(const std::string& sequenceNumber, const std::string& dssName);
167  std::string createGetRequest(const std::string& sequenceNumber, const std::string& dssName);
168  std::string createSearchRequest(const std::string& dssName, std::map<std::string, std::string>& sdQueryFields);
169 
170 
171  /*
172  * Create the "raw" messages with the client requests, starting form a "raw, non-structured" input (i.e. one single string).
173  * The given string is parsed and a new string is formatted according to the protocol syntax, for simple REMOVE and GET requests.
174  * For INSERT and SEARCH requests the given string is parsed and the above functions are called.
175  */
176  std::string createInsertRequest(const std::string& input, std::map<std::string, std::string>& dataItems);
177  std::string createRemoveRequest(const std::string& input);
178  std::string createGetRequest(const std::string& input);
179  std::string createSearchRequest(const std::string& input);
180 
181 
182  /*
183  * Convenience functions to parse a server "raw"response into a much more handy ResponseMessage.
184  * @param response The raw server response message.
185  * @return The resulting ResponseMessage object.
186  */
187  ResponseMessage parseResponseMessage(const std::string& response);
188  InsertResponse parseInsertResponse(const std::string& response);
189  GetResponse parseGetResponse(const std::string& response);
190  SearchResponse parseSearchResponse(const std::string& response);
191  SpeclistResponse parseSpeclistResponse(const std::string& response);
192 
197  std::string getExtendedErrorMessage(ResponseCode code);
198  };
199 
200 
206  std::string responseCodeString(ResponseCode code);
207 
208 }
209 
210 #endif
A generic response message of the protocol. A response message is defined by a return code which spec...
Definition: client_protocol.h:40
A response message to SEARCH request. A response message to SEARCH requests is defined by the SetDesc...
Definition: client_protocol.h:111
A response message to INSERT requests. A response message to INSERT requests is simply defined throug...
Definition: client_protocol.h:60
Convenience class to simplify the protocol messages handling. This class is useful to parse the "raw"...
Definition: client_protocol.h:157
A response message to GET requests. A response message to GET requests is defined by the SetDescripto...
Definition: client_protocol.h:78
Definition: client_protocol.cpp:725
ResponseCode
Definition: protocol.h:45
A response message to SPECLIST requests. A reponse message to SPECLIST request may be negative...
Definition: client_protocol.h:134
std::string responseCodeString(ResponseCode code)
Definition: client_protocol.cpp:671
std::string getExtendedErrorMessage(ResponseCode code)
Definition: client_protocol.cpp:617