RuntimeEnvironment.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_RUNTIMEENVIRONMENT_HPP
  28. #define _ZT_RUNTIMEENVIRONMENT_HPP
  29. #include <string>
  30. #include "Constants.hpp"
  31. #include "Identity.hpp"
  32. #include "Condition.hpp"
  33. namespace ZeroTier {
  34. class NodeConfig;
  35. class Logger;
  36. class Demarc;
  37. class Switch;
  38. class Topology;
  39. class SysEnv;
  40. class CMWC4096;
  41. class Service;
  42. class Node;
  43. class Multicaster;
  44. class Updater;
  45. /**
  46. * Holds global state for an instance of ZeroTier::Node
  47. *
  48. * I do not believe in mutable static variables, period, or in global static
  49. * instances of objects that don't basically represent constants. It makes
  50. * unit testing, embedding, threading, and other things hard and is poor
  51. * practice.
  52. *
  53. * So we put everything that we would want to be global, like Logger, here
  54. * and we give everybody this as _r. The Node creates and initializes this
  55. * on startup and deletes things on shutdown.
  56. */
  57. class RuntimeEnvironment
  58. {
  59. public:
  60. RuntimeEnvironment() :
  61. shutdownInProgress(false),
  62. log((Logger *)0),
  63. prng((CMWC4096 *)0),
  64. mc((Multicaster *)0),
  65. sw((Switch *)0),
  66. demarc((Demarc *)0),
  67. topology((Topology *)0),
  68. sysEnv((SysEnv *)0),
  69. nc((NodeConfig *)0),
  70. updater((Updater *)0)
  71. #ifndef __WINDOWS__
  72. ,netconfService((Service *)0)
  73. #endif
  74. {
  75. }
  76. // home of saved state, identity, etc.
  77. std::string homePath;
  78. // signal() to prematurely interrupt main loop wait to cause loop to run
  79. // again and detect some kind of change, exit, etc.
  80. Condition mainLoopWaitCondition;
  81. Identity identity;
  82. // hacky... want to get rid of this flag...
  83. volatile bool shutdownInProgress;
  84. // Order matters a bit here. These are constructed in this order
  85. // and then deleted in the opposite order on Node exit. The order ensures
  86. // that things that are needed are there before they're needed.
  87. Logger *log; // may be null
  88. CMWC4096 *prng;
  89. Multicaster *mc;
  90. Switch *sw;
  91. Demarc *demarc;
  92. Topology *topology;
  93. SysEnv *sysEnv;
  94. NodeConfig *nc;
  95. Node *node;
  96. Updater *updater; // may be null if updates are disabled
  97. #ifndef __WINDOWS__
  98. Service *netconfService; // may be null
  99. #endif
  100. };
  101. } // namespace ZeroTier
  102. #endif