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/CMakeLists.txt | 9 ++++++ tests/Network/EchoServer.cpp | 19 +++++++++++++ tests/Network/Google.cpp | 23 +++++++++++++-- tests/Network/NameLookup.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 tests/Network/EchoServer.cpp create mode 100644 tests/Network/NameLookup.cpp (limited to 'tests/Network') diff --git a/tests/Network/CMakeLists.txt b/tests/Network/CMakeLists.txt index 8aae84590..a63ce7dfe 100644 --- a/tests/Network/CMakeLists.txt +++ b/tests/Network/CMakeLists.txt @@ -8,7 +8,16 @@ include_directories(${CMAKE_SOURCE_DIR}/lib/libevent/include) add_definitions(-DTEST_GLOBALS=1) add_library(Network ${CMAKE_SOURCE_DIR}/src/OSSupport/Network.cpp ${CMAKE_SOURCE_DIR}/src/OSSupport/Event.cpp ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp) target_link_libraries(Network event_core event_extra) +if (MSVC) + target_link_libraries(Network ws2_32.lib) +endif() add_executable(Google-exe Google.cpp) target_link_libraries(Google-exe Network) add_test(NAME Google-test COMMAND Google-exe) + +add_executable(EchoServer EchoServer.cpp) +target_link_libraries(EchoServer Network) + +add_executable(NameLookup NameLookup.cpp) +target_link_libraries(NameLookup Network) diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp new file mode 100644 index 000000000..def064083 --- /dev/null +++ b/tests/Network/EchoServer.cpp @@ -0,0 +1,19 @@ + +// EchoServer.cpp + +// Implements an Echo server using the LibEvent-based cNetwork API, as a test of that API + + + + + + +int main() +{ + // TODO + return 0; +} + + + + 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"); } + + + + diff --git a/tests/Network/NameLookup.cpp b/tests/Network/NameLookup.cpp new file mode 100644 index 000000000..daa72a3cb --- /dev/null +++ b/tests/Network/NameLookup.cpp @@ -0,0 +1,67 @@ + +// NameLookup.cpp + +// Implements a DNS name lookup using the LibEvent-based cNetwork API + +#include "Globals.h" +#include +#include "OSSupport/Event.h" +#include "OSSupport/Network.h" + + + + + +class cFinishLookupCallbacks: + public cNetwork::cResolveNameCallbacks +{ + cEvent & m_Event; + + virtual void OnNameResolved(const AString & a_Name, const AString & a_IP) override + { + LOGD("%s resolves to IP %s", a_Name.c_str(), a_IP.c_str()); + } + + virtual void OnError(int a_ErrorCode) override + { + LOGD("Error %d while performing lookup!", a_ErrorCode); + abort(); + } + + virtual void OnFinished(void) override + { + LOGD("Resolving finished."); + m_Event.Set(); + } + +public: + cFinishLookupCallbacks(cEvent & a_Event): + m_Event(a_Event) + { + } +}; + + + + + +int main() +{ + cEvent evtFinish; + + LOGD("Network test: Looking up google.com"); + if (!cNetwork::HostnameToIP("google.com", std::make_shared(evtFinish))) + { + LOGWARNING("Cannot resolve google.com"); + abort(); + } + LOGD("Name lookup has been successfully queued"); + + evtFinish.Wait(); + LOGD("Network test finished"); + return 0; +} + + + + -- cgit v1.2.3