summaryrefslogtreecommitdiffstats
path: root/tests/Network/Google.cpp
blob: 8985eef17298015e13ea33807f906dde60aa0a58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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 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, 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;
}