summaryrefslogtreecommitdiffstats
path: root/tests/Network/Google.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-01-24 09:27:55 +0100
committerMattes D <github@xoft.cz>2015-01-24 09:27:55 +0100
commitf91de20ca14725a41ea082b61a98477427c78528 (patch)
tree7925214f6faec943d0bdffc3c2706eed8fe744e9 /tests/Network/Google.cpp
parentMerge pull request #1712 from UltraCoderRU/fix_coverity (diff)
parentcNetwork: Added self pointers to keep objects alive for callbacks. (diff)
downloadcuberite-f91de20ca14725a41ea082b61a98477427c78528.tar
cuberite-f91de20ca14725a41ea082b61a98477427c78528.tar.gz
cuberite-f91de20ca14725a41ea082b61a98477427c78528.tar.bz2
cuberite-f91de20ca14725a41ea082b61a98477427c78528.tar.lz
cuberite-f91de20ca14725a41ea082b61a98477427c78528.tar.xz
cuberite-f91de20ca14725a41ea082b61a98477427c78528.tar.zst
cuberite-f91de20ca14725a41ea082b61a98477427c78528.zip
Diffstat (limited to 'tests/Network/Google.cpp')
-rw-r--r--tests/Network/Google.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp
new file mode 100644
index 000000000..2b8830c24
--- /dev/null
+++ b/tests/Network/Google.cpp
@@ -0,0 +1,118 @@
+
+// Google.cpp
+
+// Implements a HTTP download of the google's front page using the LibEvent-based cNetwork API
+
+#include "Globals.h"
+#include <thread>
+#include "OSSupport/Event.h"
+#include "OSSupport/Network.h"
+
+
+
+
+
+/** Connect callbacks that send a HTTP GET request for google.com when connected. */
+class cHTTPConnectCallbacks:
+ public cNetwork::cConnectCallbacks
+{
+ cEvent & m_Event;
+ virtual void OnConnected(cTCPLink & a_Link) override
+ {
+ LOGD("Connected, sending HTTP GET");
+ if (!a_Link.Send("GET / HTTP/1.0\r\nHost:google.com\r\n\r\n"))
+ {
+ LOGWARNING("Sending HTTP GET failed");
+ }
+ LOGD("HTTP GET queued.");
+ }
+
+ virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
+ {
+ LOGD("Error while connecting HTTP: %d (%s)", a_ErrorCode, a_ErrorMsg.c_str());
+ m_Event.Set();
+ }
+
+public:
+ cHTTPConnectCallbacks(cEvent & a_Event):
+ m_Event(a_Event)
+ {
+ }
+};
+
+
+
+
+
+/** cTCPLink callbacks that dump everything it received to the log. */
+class cDumpCallbacks:
+ public cTCPLink::cCallbacks
+{
+ cEvent & m_Event;
+ cTCPLinkPtr m_Link;
+
+ virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
+ {
+ ASSERT(m_Link == nullptr);
+ m_Link = a_Link;
+ }
+
+ virtual void OnReceivedData(const char * a_Data, size_t a_Size) override
+ {
+ ASSERT(m_Link != nullptr);
+
+ // Log the incoming data size:
+ AString Hex;
+ CreateHexDump(Hex, a_Data, a_Size, 16);
+ LOGD("Incoming data: %u bytes:\n%s", static_cast<unsigned>(a_Size), Hex.c_str());
+ }
+
+ virtual void OnRemoteClosed(void) override
+ {
+ ASSERT(m_Link != nullptr);
+
+ LOGD("Remote has closed the connection.");
+ m_Link.reset();
+ m_Event.Set();
+ }
+
+ virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
+ {
+ ASSERT(m_Link != nullptr);
+
+ LOGD("Error %d (%s) in the cDumpCallbacks.", a_ErrorCode, a_ErrorMsg.c_str());
+ m_Link.reset();
+ m_Event.Set();
+ }
+
+public:
+ cDumpCallbacks(cEvent & a_Event):
+ m_Event(a_Event)
+ {
+ }
+};
+
+
+
+
+
+int main()
+{
+ cEvent evtFinish;
+
+ LOGD("Network test: Connecting to google.com:80, reading front page via HTTP.");
+ if (!cNetwork::Connect("google.com", 80, std::make_shared<cHTTPConnectCallbacks>(evtFinish), std::make_shared<cDumpCallbacks>(evtFinish)))
+ {
+ LOGWARNING("Cannot queue connection to google.com");
+ abort();
+ }
+ LOGD("Connect request has been queued.");
+
+ evtFinish.Wait();
+ LOGD("Network test finished");
+ return 0;
+}
+
+
+
+