PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/tests/commtest.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 145 lines | 90 code | 12 blank | 43 comment | 10 complexity | 669d1f7ace6eded59dd3924286eb0ab7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file commtest.h
  3. * @author Nat Goodspeed
  4. * @date 2009-01-09
  5. * @brief
  6. *
  7. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #if ! defined(LL_COMMTEST_H)
  29. #define LL_COMMTEST_H
  30. #include "networkio.h"
  31. #include "llevents.h"
  32. #include "llsd.h"
  33. #include "llhost.h"
  34. #include "stringize.h"
  35. #include <map>
  36. #include <string>
  37. #include <stdexcept>
  38. #include <boost/lexical_cast.hpp>
  39. struct CommtestError: public std::runtime_error
  40. {
  41. CommtestError(const std::string& what): std::runtime_error(what) {}
  42. };
  43. static bool query_verbose()
  44. {
  45. const char* cbose = getenv("INTEGRATION_TEST_VERBOSE");
  46. if (! cbose)
  47. {
  48. cbose = "1";
  49. }
  50. std::string strbose(cbose);
  51. return (! (strbose == "0" || strbose == "off" ||
  52. strbose == "false" || strbose == "quiet"));
  53. }
  54. bool verbose()
  55. {
  56. // This should only be initialized once.
  57. static bool vflag = query_verbose();
  58. return vflag;
  59. }
  60. static int query_port(const std::string& var)
  61. {
  62. const char* cport = getenv(var.c_str());
  63. if (! cport)
  64. {
  65. throw CommtestError(STRINGIZE("missing environment variable" << var));
  66. }
  67. // This will throw, too, if the value of PORT isn't numeric.
  68. int port(boost::lexical_cast<int>(cport));
  69. if (verbose())
  70. {
  71. std::cout << "getport('" << var << "') = " << port << std::endl;
  72. }
  73. return port;
  74. }
  75. static int getport(const std::string& var)
  76. {
  77. typedef std::map<std::string, int> portsmap;
  78. static portsmap ports;
  79. // We can do this with a single map lookup with map::insert(). Either it
  80. // returns an existing entry and 'false' (not newly inserted), or it
  81. // inserts the specified value and 'true'.
  82. std::pair<portsmap::iterator, bool> inserted(ports.insert(portsmap::value_type(var, 0)));
  83. if (inserted.second)
  84. {
  85. // We haven't yet seen this var. Remember its value.
  86. inserted.first->second = query_port(var);
  87. }
  88. // Return the (existing or new) iterator's value.
  89. return inserted.first->second;
  90. }
  91. /**
  92. * This struct is shared by a couple of standalone comm tests (ADD_COMM_BUILD_TEST).
  93. */
  94. struct commtest_data
  95. {
  96. NetworkIO& netio;
  97. LLEventPumps& pumps;
  98. LLEventStream replyPump, errorPump;
  99. LLSD result;
  100. bool success;
  101. LLHost host;
  102. std::string server;
  103. commtest_data():
  104. netio(NetworkIO::instance()),
  105. pumps(LLEventPumps::instance()),
  106. replyPump("reply"),
  107. errorPump("error"),
  108. success(false),
  109. host("127.0.0.1", getport("PORT")),
  110. server(STRINGIZE("http://" << host.getString() << "/"))
  111. {
  112. replyPump.listen("self", boost::bind(&commtest_data::outcome, this, _1, true));
  113. errorPump.listen("self", boost::bind(&commtest_data::outcome, this, _1, false));
  114. }
  115. static int getport(const std::string& var)
  116. {
  117. // We have a couple consumers of commtest_data::getport(). But we've
  118. // since moved it out to the global namespace. So this is just a
  119. // facade.
  120. return ::getport(var);
  121. }
  122. bool outcome(const LLSD& _result, bool _success)
  123. {
  124. // std::cout << "commtest_data::outcome(" << _result << ", " << _success << ")\n";
  125. result = _result;
  126. success = _success;
  127. // Break the wait loop in NetworkIO::pump(), otherwise devs get
  128. // irritated at making the big monolithic test executable take longer
  129. pumps.obtain("done").post(success);
  130. return false;
  131. }
  132. };
  133. #endif /* ! defined(LL_COMMTEST_H) */