Extracted Google connection test
This commit is contained in:
parent
b8b3409b74
commit
a9b8a530b1
@ -298,6 +298,16 @@ void inline LOGD(const char* a_Format, ...)
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
void inline LOG(const char* a_Format, ...) FORMATSTRING(1, 2);
|
||||
|
||||
void inline LOG(const char* a_Format, ...)
|
||||
{
|
||||
va_list argList;
|
||||
va_start(argList, a_Format);
|
||||
vprintf(a_Format, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -5,12 +5,13 @@
|
||||
|
||||
#include "Globals.h"
|
||||
#include "Network.h"
|
||||
#include "Event.h"
|
||||
#include <event2/event.h>
|
||||
#include <event2/thread.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/dns.h>
|
||||
|
||||
|
||||
#include <thread>
|
||||
|
||||
|
||||
|
||||
@ -20,38 +21,6 @@
|
||||
class cNetworkTest
|
||||
{
|
||||
public:
|
||||
/** 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<unsigned>(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)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** cTCPLink callbacks that echo everything they receive back to the remote peer. */
|
||||
@ -114,36 +83,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** 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)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** Listen callbacks that send a simple welcome message to all connecting peers. */
|
||||
class cListenCallbacks:
|
||||
public cNetwork::cListenCallbacks
|
||||
@ -157,15 +96,6 @@ public:
|
||||
|
||||
cNetworkTest(void)
|
||||
{
|
||||
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.");
|
||||
|
||||
/*
|
||||
LOGD("Creating a server on port 33033");
|
||||
@ -182,8 +112,7 @@ public:
|
||||
Server->Close();
|
||||
*/
|
||||
|
||||
evtFinish.Wait();
|
||||
LOGD("Network test finished");
|
||||
|
||||
}
|
||||
} g_NetworkTest;
|
||||
|
||||
|
@ -5,3 +5,4 @@ enable_testing()
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
add_subdirectory(ChunkData)
|
||||
add_subdirectory(Network)
|
||||
|
14
tests/Network/CMakeLists.txt
Normal file
14
tests/Network/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
|
||||
enable_testing()
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/src/)
|
||||
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)
|
||||
|
||||
add_executable(Google-exe Google.cpp)
|
||||
target_link_libraries(Google-exe Network)
|
||||
add_test(NAME Google-test COMMAND Google-exe)
|
85
tests/Network/Google.cpp
Normal file
85
tests/Network/Google.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
|
||||
#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 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<unsigned>(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<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");
|
||||
}
|
Loading…
Reference in New Issue
Block a user