From a9b8a530b1e082d2ab108947ef28e0a58c8fd838 Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 9 Jan 2015 14:07:59 +0000 Subject: Extracted Google connection test --- tests/Network/Google.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/Network/Google.cpp (limited to 'tests/Network/Google.cpp') diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp new file mode 100644 index 000000000..0aa52d025 --- /dev/null +++ b/tests/Network/Google.cpp @@ -0,0 +1,85 @@ + +#include "Globals.h" + +#include +#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 OnSuccess(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) override + { + LOGD("Error while connecting HTTP: %d", a_ErrorCode); + 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; + + virtual void OnReceivedData(cTCPLink & a_Link, const char * a_Data, size_t a_Size) override + { + // Log the incoming data size: + AString Hex; + CreateHexDump(Hex, a_Data, a_Size, 16); + LOGD("Incoming data: %u bytes:\n%s", static_cast(a_Size), Hex.c_str()); + } + + virtual void OnRemoteClosed(cTCPLink & a_Link) override + { + LOGD("Remote has closed the connection."); + m_Event.Set(); + } + + virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override + { + LOGD("Error in the cDumpCallbacks."); + 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(evtFinish), std::make_shared(evtFinish))) + { + LOGWARNING("Cannot queue connection to google.com"); + abort(); + } + LOGD("Connect request has been queued."); + + evtFinish.Wait(); + LOGD("Network test finished"); +} -- cgit v1.2.3 From fde44cba0815f626253c0d352cd0d782eec94328 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 11 Jan 2015 11:21:18 +0100 Subject: cNetwork: Implemented HostnameToIP lookups. --- tests/Network/Google.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'tests/Network/Google.cpp') diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp index 0aa52d025..de5f95e1f 100644 --- a/tests/Network/Google.cpp +++ b/tests/Network/Google.cpp @@ -1,11 +1,17 @@ -#include "Globals.h" +// Google.cpp + +// Implements a HTTP download of the google's front page using the LibEvent-based cNetwork API +#include "Globals.h" #include #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 @@ -35,6 +41,9 @@ public: }; + + + /** cTCPLink callbacks that dump everything it received to the log. */ class cDumpCallbacks: public cTCPLink::cCallbacks @@ -69,7 +78,11 @@ public: }; -int main() { + + + +int main() +{ cEvent evtFinish; LOGD("Network test: Connecting to google.com:80, reading front page via HTTP."); @@ -83,3 +96,7 @@ int main() { evtFinish.Wait(); LOGD("Network test finished"); } + + + + -- cgit v1.2.3 From d4682463a1d503c349ac95e275b11d67d402268c Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 18 Jan 2015 12:35:02 +0100 Subject: cNetwork: Fixed race conditions with lookups; proper shutdown. --- tests/Network/Google.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/Network/Google.cpp') diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp index de5f95e1f..8ee04f8ee 100644 --- a/tests/Network/Google.cpp +++ b/tests/Network/Google.cpp @@ -95,6 +95,7 @@ int main() evtFinish.Wait(); LOGD("Network test finished"); + return 0; } -- cgit v1.2.3 From 64855ed340e76779b99f37fbc866a7f5952e11db Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 20 Jan 2015 11:27:05 +0100 Subject: cNetwork: Added error message to error callbacks. --- tests/Network/Google.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/Network/Google.cpp') diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp index 8ee04f8ee..be08f179c 100644 --- a/tests/Network/Google.cpp +++ b/tests/Network/Google.cpp @@ -27,9 +27,9 @@ class cHTTPConnectCallbacks: LOGD("HTTP GET queued."); } - virtual void OnError(int a_ErrorCode) override + virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override { - LOGD("Error while connecting HTTP: %d", a_ErrorCode); + LOGD("Error while connecting HTTP: %d (%s)", a_ErrorCode, a_ErrorMsg.c_str()); m_Event.Set(); } @@ -64,9 +64,9 @@ class cDumpCallbacks: m_Event.Set(); } - virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override + virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) override { - LOGD("Error in the cDumpCallbacks."); + LOGD("Error %d (%s) in the cDumpCallbacks.", a_ErrorCode, a_ErrorMsg.c_str()); m_Event.Set(); } -- cgit v1.2.3 From dbf7f13bd414daea5e787da2543df186dc465c34 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 22 Jan 2015 13:00:32 +0100 Subject: cNetwork: Added link creation callback. This allows the callback classes to store the link inside them and use it internally later on, mainly for sending data. --- tests/Network/Google.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'tests/Network/Google.cpp') diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp index be08f179c..8985eef17 100644 --- a/tests/Network/Google.cpp +++ b/tests/Network/Google.cpp @@ -49,24 +49,39 @@ class cDumpCallbacks: public cTCPLink::cCallbacks { cEvent & m_Event; + cTCPLinkPtr m_Link; - virtual void OnReceivedData(cTCPLink & a_Link, const char * a_Data, size_t a_Size) override + 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(a_Size), Hex.c_str()); } - virtual void OnRemoteClosed(cTCPLink & a_Link) override + virtual void OnRemoteClosed(void) override { + ASSERT(m_Link != nullptr); + LOGD("Remote has closed the connection."); + m_Link.reset(); m_Event.Set(); } - virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) override + 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(); } -- cgit v1.2.3 From 9014bdfa3233dac70274b27eb40df3739b6f49eb Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 22 Jan 2015 22:49:37 +0100 Subject: cNetwork: Renamed callback to OnConnected() --- tests/Network/Google.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/Network/Google.cpp') diff --git a/tests/Network/Google.cpp b/tests/Network/Google.cpp index 8985eef17..2b8830c24 100644 --- a/tests/Network/Google.cpp +++ b/tests/Network/Google.cpp @@ -17,7 +17,7 @@ class cHTTPConnectCallbacks: public cNetwork::cConnectCallbacks { cEvent & m_Event; - virtual void OnSuccess(cTCPLink & a_Link) override + 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")) -- cgit v1.2.3