2015-01-09 09:07:59 -05:00
|
|
|
|
2015-01-11 05:21:18 -05:00
|
|
|
// Google.cpp
|
|
|
|
|
|
|
|
// Implements a HTTP download of the google's front page using the LibEvent-based cNetwork API
|
2015-01-09 09:07:59 -05:00
|
|
|
|
2015-01-11 05:21:18 -05:00
|
|
|
#include "Globals.h"
|
2015-01-09 09:07:59 -05:00
|
|
|
#include <thread>
|
|
|
|
#include "OSSupport/Event.h"
|
|
|
|
#include "OSSupport/Network.h"
|
2015-01-26 08:46:20 -05:00
|
|
|
#include "OSSupport/NetworkSingleton.h"
|
2015-01-09 09:07:59 -05:00
|
|
|
|
2015-01-11 05:21:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-09 09:07:59 -05:00
|
|
|
/** Connect callbacks that send a HTTP GET request for google.com when connected. */
|
|
|
|
class cHTTPConnectCallbacks:
|
|
|
|
public cNetwork::cConnectCallbacks
|
|
|
|
{
|
|
|
|
cEvent & m_Event;
|
2015-01-22 16:49:37 -05:00
|
|
|
virtual void OnConnected(cTCPLink & a_Link) override
|
2015-01-09 09:07:59 -05:00
|
|
|
{
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2015-01-20 05:27:05 -05:00
|
|
|
virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
|
2015-01-09 09:07:59 -05:00
|
|
|
{
|
2015-01-20 05:27:05 -05:00
|
|
|
LOGD("Error while connecting HTTP: %d (%s)", a_ErrorCode, a_ErrorMsg.c_str());
|
2015-01-09 09:07:59 -05:00
|
|
|
m_Event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
cHTTPConnectCallbacks(cEvent & a_Event):
|
|
|
|
m_Event(a_Event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-11 05:21:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-09 09:07:59 -05:00
|
|
|
/** cTCPLink callbacks that dump everything it received to the log. */
|
|
|
|
class cDumpCallbacks:
|
|
|
|
public cTCPLink::cCallbacks
|
|
|
|
{
|
|
|
|
cEvent & m_Event;
|
2015-01-22 07:00:32 -05:00
|
|
|
cTCPLinkPtr m_Link;
|
2015-01-09 09:07:59 -05:00
|
|
|
|
2015-01-22 07:00:32 -05:00
|
|
|
virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
|
2015-01-09 09:07:59 -05:00
|
|
|
{
|
2015-01-22 07:00:32 -05:00
|
|
|
ASSERT(m_Link == nullptr);
|
|
|
|
m_Link = a_Link;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnReceivedData(const char * a_Data, size_t a_Size) override
|
|
|
|
{
|
|
|
|
ASSERT(m_Link != nullptr);
|
|
|
|
|
2015-01-09 09:07:59 -05:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2015-01-22 07:00:32 -05:00
|
|
|
virtual void OnRemoteClosed(void) override
|
2015-01-09 09:07:59 -05:00
|
|
|
{
|
2015-01-22 07:00:32 -05:00
|
|
|
ASSERT(m_Link != nullptr);
|
|
|
|
|
2015-01-09 09:07:59 -05:00
|
|
|
LOGD("Remote has closed the connection.");
|
2015-01-22 07:00:32 -05:00
|
|
|
m_Link.reset();
|
2015-01-09 09:07:59 -05:00
|
|
|
m_Event.Set();
|
|
|
|
}
|
|
|
|
|
2015-01-22 07:00:32 -05:00
|
|
|
virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
|
2015-01-09 09:07:59 -05:00
|
|
|
{
|
2015-01-22 07:00:32 -05:00
|
|
|
ASSERT(m_Link != nullptr);
|
|
|
|
|
2015-01-20 05:27:05 -05:00
|
|
|
LOGD("Error %d (%s) in the cDumpCallbacks.", a_ErrorCode, a_ErrorMsg.c_str());
|
2015-01-22 07:00:32 -05:00
|
|
|
m_Link.reset();
|
2015-01-09 09:07:59 -05:00
|
|
|
m_Event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
cDumpCallbacks(cEvent & a_Event):
|
|
|
|
m_Event(a_Event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-11 05:21:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-20 10:08:21 -05:00
|
|
|
static void DoTest(void)
|
2015-01-11 05:21:18 -05:00
|
|
|
{
|
2015-01-09 09:07:59 -05:00
|
|
|
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();
|
2015-01-26 08:46:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
DoTest();
|
|
|
|
|
|
|
|
cNetworkSingleton::Get().Terminate();
|
2015-01-09 09:07:59 -05:00
|
|
|
LOGD("Network test finished");
|
2015-01-26 08:46:20 -05:00
|
|
|
|
2015-01-18 06:35:02 -05:00
|
|
|
return 0;
|
2015-01-09 09:07:59 -05:00
|
|
|
}
|
2015-01-11 05:21:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|