OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be found | |
3 // in the LICENSE file. | |
4 | |
5 #ifndef EXPERIMENTAL_WEBGTT_PARSER_H_ | |
6 #define EXPERIMENTAL_WEBGTT_PARSER_H_ | |
7 | |
8 /// @fileoverview This file provides a helper class with functions that parse | |
9 /// and validate the message received by the NaCl module, decode the same, and | |
10 /// obtain the appropriate response to be sent back to the browser. | |
11 /// | |
12 /// @author ragad@google.com (Raga Gopalakrishnan) | |
13 | |
14 #include <string> | |
15 #include <vector> | |
16 | |
17 #include "webgtt/taskmap.h" | |
18 | |
19 namespace graph { | |
20 class Graph; | |
21 } | |
22 | |
23 namespace webgtt { | |
24 | |
25 /// The integer value that represents an invalid value. | |
26 const int kInvalidValue = -1; | |
27 | |
28 /// The Parser class. This class provides an interface for validating a given | |
29 /// message and parsing it to decode the message into its constituent entities. | |
30 /// In addition, it also provides a function that obtains the appropriate | |
31 /// response to be sent back to the browser. | |
32 class Parser { | |
33 public: | |
34 /// The constructor takes in a message, and sets default values to its class | |
35 /// members. | |
36 /// | |
37 /// @param[in] message The input message to be parsed/validated. | |
38 /// @constructor | |
39 explicit Parser(const std::string& message); | |
40 | |
41 /// This function starts decoding the message into its constituent components | |
42 /// (adjacency matrix, task ID, list of arguments). During this process, if | |
43 /// the message is found to be invalid, parsing is aborted, and the is_valid_ | |
44 /// bit would contain false. Upon successful completion, the is_valid_ bit is | |
45 /// set to true. | |
46 /// | |
47 /// @return false if an error was encountered, true otherwise. | |
48 bool DecodeMessage(); | |
49 | |
50 /// This function returns the response string to be sent back to the browser. | |
51 /// | |
52 /// This function should be used only when is_valid_ is true. | |
53 /// | |
54 /// @return The response string to be sent back to the browser. | |
55 std::string GetResponse() const; | |
56 | |
57 private: | |
58 std::string message_; | |
59 bool is_valid_; | |
60 std::vector< std::vector<int> > adjacency_matrix_; | |
61 int task_ID_; | |
62 std::vector<int> args_; | |
63 | |
64 /// The information about the function to be called, corresponding to a given | |
65 /// task ID. | |
66 std::vector<FunctionInfo> task_map_; | |
67 /// This disallows usage of copy and assignment constructors. | |
68 Parser(const Parser&); | |
69 void operator=(const Parser&); | |
70 }; | |
71 | |
72 /// This helper function converts a string in CSV format into a vector of the | |
73 /// integer equivalents (using strtoi below) of its component elements. | |
74 /// | |
75 /// @param[in] message The input string in CSV format. | |
76 /// @return The vector of integer equivalents of the component elements. | |
77 std::vector<int> DecodeCSV(const std::string& message); | |
78 | |
79 /// This helper function returns the next chunk of the message to be processed, | |
80 /// starting from parse_position, until the sentinel is encountered. It also | |
81 /// updates the position to continue parsing from. | |
82 /// | |
83 /// @param[in] message The message to take the next chunk out of. | |
84 /// @param[in,out] parse_position The starting position of the chunk. | |
85 /// @return The substring of message starting at parse_position, until the | |
86 /// sentinel is encountered. Returns the sentinel itself to indicate an | |
87 /// error if the sentinel is not encountered. | |
88 std::string GetNextChunk(const std::string& message, int* parse_position); | |
89 | |
90 /// This helper function returns the positions where a comma occurs in the given | |
91 /// message. | |
92 /// | |
93 /// @param[in] message The message to look for commas. | |
94 /// @return A vector of positions where a comma occurs in the message. | |
95 std::vector<int> GetCommaPositions(const std::string& message); | |
96 | |
97 | |
98 /// This helper function converts a string to an integer, internally using atoi. | |
99 /// | |
100 /// @param[in] message The string to be converted. | |
101 /// @return kInvalidValue if the string is empty or doesn't begin with a digit. | |
102 /// Otherwise, the integer value that would be returned by atoi. | |
103 int StringToInteger(const std::string& message); | |
104 | |
105 } // namespace webgtt | |
106 | |
107 #endif // EXPERIMENTAL_WEBGTT_PARSER_H_ | |
OLD | NEW |