| 1 | /************************************************************************* |
| 2 | * libjson-rpc-cpp |
| 3 | ************************************************************************* |
| 4 | * @file procedure.h |
| 5 | * @date 31.12.2012 |
| 6 | * @author Peter Spiess-Knafl <dev@spiessknafl.at> |
| 7 | * @license See attached LICENSE.txt |
| 8 | ************************************************************************/ |
| 9 | |
| 10 | #ifndef JSONRPC_CPP_PROCEDURE_H_ |
| 11 | #define JSONRPC_CPP_PROCEDURE_H_ |
| 12 | |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
| 16 | #include "jsonparser.h" |
| 17 | #include "specification.h" |
| 18 | |
| 19 | namespace jsonrpc { |
| 20 | typedef std::map<std::string, jsontype_t> parameterNameList_t; |
| 21 | typedef std::vector<jsontype_t> parameterPositionList_t; |
| 22 | |
| 23 | typedef enum { PARAMS_BY_NAME, PARAMS_BY_POSITION } parameterDeclaration_t; |
| 24 | |
| 25 | class Procedure { |
| 26 | public: |
| 27 | Procedure(); |
| 28 | |
| 29 | /** |
| 30 | * @brief Constructor for notificaiton with parameters as va_list. The last parameter must be NULL. |
| 31 | * If no parameters are passed, parameters either do not exist, or cannot be checked for type compliance by the library. |
| 32 | * @param name |
| 33 | */ |
| 34 | Procedure(const std::string &name, parameterDeclaration_t paramType, ...); |
| 35 | |
| 36 | /** |
| 37 | * @brief Constructor for method with parameters as va_list. The last parameter must be NULL. |
| 38 | * If no parameters are passed, parameters either do not exist, or cannot be checked for type compliance by the library. |
| 39 | * @param name |
| 40 | * @param returntype |
| 41 | */ |
| 42 | Procedure(const std::string &name, parameterDeclaration_t paramType, jsontype_t returntype, ...); |
| 43 | |
| 44 | /** |
| 45 | * This method is validating the incoming parameters for each procedure. |
| 46 | * @param parameters - should contain the parameter-object of an valid json-rpc 2.0 request |
| 47 | * @see http://groups.google.com/group/json-rpc/web/json-rpc-2-0 |
| 48 | * @return true on successful validation false otherwise. |
| 49 | * |
| 50 | * If the valid parameters are of Type JSON_ARRAY or JSON_OBJECT, they can only be checked for name and not for their structure. |
| 51 | */ |
| 52 | bool ValdiateParameters(const Json::Value ¶meters) const; |
| 53 | |
| 54 | // Various get methods. |
| 55 | const parameterNameList_t &GetParameters() const; |
| 56 | procedure_t GetProcedureType() const; |
| 57 | const std::string &GetProcedureName() const; |
| 58 | jsontype_t GetReturnType() const; |
| 59 | parameterDeclaration_t GetParameterDeclarationType() const; |
| 60 | |
| 61 | // Various set methods. |
| 62 | void SetProcedureName(const std::string &name); |
| 63 | void SetProcedureType(procedure_t type); |
| 64 | void SetReturnType(jsontype_t type); |
| 65 | void SetParameterDeclarationType(parameterDeclaration_t type); |
| 66 | |
| 67 | /** |
| 68 | * @brief AddParameter |
| 69 | * @param name describes the name of the parameter. In case of an positional parameters, this value can be anything. |
| 70 | * @param type describes the defined type for this parameter. |
| 71 | */ |
| 72 | void AddParameter(const std::string &name, jsontype_t type); |
| 73 | |
| 74 | bool ValidateNamedParameters(const Json::Value ¶meters) const; |
| 75 | bool ValidatePositionalParameters(const Json::Value ¶meters) const; |
| 76 | |
| 77 | private: |
| 78 | /** |
| 79 | * Each Procedure should have a name. |
| 80 | */ |
| 81 | std::string procedureName; |
| 82 | |
| 83 | /** |
| 84 | * This map represents all necessary Parameters of each Procedure. |
| 85 | * The string represents the name of each parameter and JsonType the type it should have. |
| 86 | */ |
| 87 | parameterNameList_t parametersName; |
| 88 | |
| 89 | /** |
| 90 | * This vector holds all parametertypes by position. |
| 91 | */ |
| 92 | parameterPositionList_t parametersPosition; |
| 93 | |
| 94 | /** |
| 95 | * @brief defines whether the procedure is a method or a notification |
| 96 | */ |
| 97 | procedure_t procedureType; |
| 98 | |
| 99 | /** |
| 100 | * @brief this field is only valid if procedure is of type method (not notification). |
| 101 | */ |
| 102 | jsontype_t returntype; |
| 103 | |
| 104 | /** |
| 105 | * @brief paramDeclaration this field defines if procedure uses named or positional parameters. |
| 106 | */ |
| 107 | parameterDeclaration_t paramDeclaration; |
| 108 | |
| 109 | bool ValidateSingleParameter(jsontype_t expectedType, const Json::Value &value) const; |
| 110 | }; |
| 111 | } /* namespace jsonrpc */ |
| 112 | #endif /* JSONRPC_CPP_PROCEDURE_H_ */ |
| 113 | |