ccsession.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #ifndef __CCSESSION_H
  16. #define __CCSESSION_H 1
  17. #include <string>
  18. #include <config/module_spec.h>
  19. #include <cc/session.h>
  20. #include <cc/data.h>
  21. namespace isc {
  22. namespace config {
  23. ///
  24. /// \brief A standard cc session exception that is thrown if a function
  25. /// is there is a problem with one of the messages
  26. ///
  27. // todo: include types and called function in the exception
  28. class CCSessionError : public isc::Exception {
  29. public:
  30. CCSessionError(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) {}
  32. };
  33. ///
  34. /// \brief This modules keeps a connection to the command channel,
  35. /// holds configuration information, and handles messages from
  36. /// the command channel
  37. ///
  38. class ModuleCCSession {
  39. public:
  40. /**
  41. * Initialize a config/command session
  42. * @param module_name: The name of this module. This is not a
  43. * reference because we expect static strings
  44. * to be passed here.
  45. * @param spec_file_name: The name of the file containing the
  46. * module specification.
  47. */
  48. ModuleCCSession(std::string spec_file_name,
  49. isc::data::ElementPtr(*config_handler)(isc::data::ElementPtr new_config) = NULL,
  50. isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command) = NULL
  51. ) throw (isc::cc::SessionError);
  52. int getSocket();
  53. /**
  54. * Check if there is a command or config change on the command
  55. * session. If so, the appropriate handler is called if set.
  56. * If not set, a default answer is returned.
  57. * This is a non-blocking read; if there is nothing this function
  58. * will return 0.
  59. */
  60. int check_command();
  61. /**
  62. * The config handler function should expect an ElementPtr containing
  63. * the full configuration where non-default values have been set.
  64. * Later we might want to think about more granular control
  65. * (i.e. this does not scale to for instance lists containing
  66. * 100000 zones, where the whole list is passed every time a single
  67. * thing changes)
  68. */
  69. void set_config_handler(isc::data::ElementPtr(*config_handler)(isc::data::ElementPtr new_config)) { config_handler_ = config_handler; };
  70. /**
  71. * Set a command handler; the function that is passed takes an
  72. * ElementPtr, pointing to a list element, containing
  73. * [ module_name, command_name, arg1, arg2, ... ]
  74. * The returned ElementPtr should look like
  75. * { "result": [ return_value, result_value ] }
  76. * result value here is optional and depends on the command
  77. *
  78. * This protocol is very likely to change.
  79. */
  80. void set_command_handler(isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command)) { command_handler_ = command_handler; };
  81. const ElementPtr getConfig() { return config_; }
  82. private:
  83. void read_module_specification(const std::string& filename);
  84. std::string module_name_;
  85. isc::cc::Session session_;
  86. ModuleSpec module_specification_;
  87. isc::data::ElementPtr config_;
  88. ElementPtr handleConfigUpdate(ElementPtr new_config);
  89. isc::data::ElementPtr(*config_handler_)(isc::data::ElementPtr new_config);
  90. isc::data::ElementPtr(*command_handler_)(isc::data::ElementPtr command);
  91. };
  92. ElementPtr createAnswer(const int rcode);
  93. ElementPtr createAnswer(const int rcode, const ElementPtr arg);
  94. ElementPtr createAnswer(const int rcode, const std::string& arg);
  95. }
  96. }
  97. #endif // __CCSESSION_H
  98. // Local Variables:
  99. // mode: c++
  100. // End: