All Classes Namespaces Functions Variables Enumerations Enumerator Pages
server_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 SERVER_PROTOCOL_H
24 #define SERVER_PROTOCOL_H
25 
26 #include <string>
27 #include <vector>
28 #include <map>
29 
30 #include "protocol.h"
31 #include "../entity/DataItem.h"
32 #include "../entity/SetDescriptor.h"
33 
34 namespace net
35 {
36 
43  {
44  public:
47 
48  Command getCommand();
49  void setDssName(const std::string& name);
50  std::string getDssName() const;
51 
52  friend std::ostream& operator<<(std::ostream& os, const RequestMessage& msg);
53 
54  protected:
55 
56  Command command;
57  std::string dssName;
58  };
59 
60 
61 
68  {
69  public:
70  InsertRequest();
71 
72  void setSdSize(int s);
73  const std::map<std::string, std::string>& getSdFields() const;
74  const std::vector<entity::DataItem>& getDataItems() const;
75  void addSetDescriptorField(const std::string& key, const std::string& value);
76  void addDataItem(const entity::DataItem& dataItem);
77  void addItemOriginalSize(const std::string& itemName, size_t s);
78 
79  friend std::ostream& operator<<(std::ostream& os, const InsertRequest& msg);
80 
81  private:
82 
83  int sdSize;
84  std::map<std::string, std::string> setDescriptorFields;
85  std::vector<entity::DataItem> dataItems;
86  };
87 
88 
95  {
96  public:
98 
99  void setSequenceNumber(int sn);
100  int getSequenceNumber() const;
101 
102  friend std::ostream& operator<<(std::ostream& os, const DataSetRequest& msg);
103 
104  private:
105 
106  int sequenceNumber;
107  };
108 
115  {
116 
117  public:
118 
119  SearchRequest();
120 
121  void setSdQuerySize(int s);
122  void addSdQueryField(const std::string& key, const std::string& value);
123  const std::map<std::string, std::string>& getQueryFields() const;
124 
125  friend std::ostream& operator<<(std::ostream& os, const SearchRequest& msg);
126 
127  private:
128 
129  int sdQuerySize;
130  std::map<std::string, std::string> sdQueryFields;
131 
132  };
133 
134 
140  class ProtocolHandler
141  {
142 
143  public:
144 
149  RequestMessage *parseRequest(const std::string& request);
150 
151  /* Create response messages to GET and SEARCH request messaes. */
152  std::string createGetResponse(int sequenceNumber, const std::map<std::string, std::string>& sdFields, const std::map<std::string, std::string>& dataItems);
153  std::string createSearchResponse(const std::vector<entity::SetDescriptor>& descriptorList);
154 
155  private:
156 
157  /* Convenience functions for the parseRequest() main function. */
158  InsertRequest *parseInsertRequest(std::istringstream& parser);
159  DataSetRequest *parseDataSetRequest(std::istringstream& parser, Command cmd);
160  SearchRequest *parseSearchRequest(std::istringstream& parser);
161  };
162 
167  std::string commandString(Command cmd);
168 }
169 
170 #endif
A DataItem (DI) is an object which represents a single file and is the basic unit of the SimDB system...
Definition: DataItem.h:43
An INSERT request message.
Definition: server_protocol.h:67
A SEARCH request message.
Definition: server_protocol.h:114
RequestMessage * parseRequest(const std::string &request)
Definition: server_protocol.cpp:126
Convenience class to simplify the protocol messages handling. This class is useful to parse the "raw"...
Definition: client_protocol.h:157
Definition: client_protocol.cpp:725
Command
Definition: protocol.h:33
A generic request message of the protocol.
Definition: server_protocol.h:42
A GET or REMOVE request message.
Definition: server_protocol.h:94
std::string commandString(Command cmd)
Definition: server_protocol.cpp:329