The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. //==============================================================================
  20. class NetworkingDemo : public Component,
  21. private Button::Listener,
  22. private Thread
  23. {
  24. public:
  25. NetworkingDemo()
  26. : Thread ("Network Demo"),
  27. resultsBox (resultsDocument, nullptr)
  28. {
  29. setOpaque (true);
  30. addAndMakeVisible (urlBox);
  31. urlBox.setText ("http://www.google.com");
  32. addAndMakeVisible (fetchButton);
  33. fetchButton.setButtonText ("Download URL Contents");
  34. fetchButton.addListener (this);
  35. addAndMakeVisible (resultsBox);
  36. }
  37. ~NetworkingDemo()
  38. {
  39. fetchButton.removeListener (this);
  40. }
  41. void paint (Graphics& g) override
  42. {
  43. fillTiledBackground (g);
  44. }
  45. void resized() override
  46. {
  47. Rectangle<int> area (getLocalBounds());
  48. {
  49. Rectangle<int> topArea (area.removeFromTop (40));
  50. fetchButton.setBounds (topArea.removeFromRight (180).reduced (8));
  51. urlBox.setBounds (topArea.reduced (8));
  52. }
  53. resultsBox.setBounds (area.reduced (8));
  54. }
  55. void run() override
  56. {
  57. String result (getResultText (urlBox.getText()));
  58. MessageManagerLock mml (this);
  59. if (mml.lockWasGained())
  60. resultsBox.loadContent (result);
  61. }
  62. String getResultText (const URL& url)
  63. {
  64. StringPairArray responseHeaders;
  65. int statusCode = 0;
  66. ScopedPointer<InputStream> stream (url.createInputStream (false, nullptr, nullptr, String(),
  67. 10000, // timeout in millisecs
  68. &responseHeaders, &statusCode));
  69. if (stream != nullptr)
  70. return (statusCode != 0 ? "Status code: " + String (statusCode) + newLine : String())
  71. + "Response headers: " + newLine
  72. + responseHeaders.getDescription() + newLine
  73. + "----------------------------------------------------" + newLine
  74. + stream->readEntireStreamAsString();
  75. if (statusCode != 0)
  76. return "Failed to connect, status code = " + String (statusCode);
  77. return "Failed to connect!";
  78. }
  79. private:
  80. TextEditor urlBox;
  81. TextButton fetchButton;
  82. CodeDocument resultsDocument;
  83. CodeEditorComponent resultsBox;
  84. void buttonClicked (Button* button) override
  85. {
  86. if (button == &fetchButton)
  87. startThread();
  88. }
  89. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NetworkingDemo)
  90. };
  91. // This static object will register this demo type in a global list of demos..
  92. static JuceDemoType<NetworkingDemo> demo ("40 HTTP");