summaryrefslogtreecommitdiff
path: root/src/tests/socket.cpp
blob: 1cf63e44e8cfe3549cec3cce166dd9fb76f7b1d5 (plain)
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
#include <bu/socket.h>
#include <bu/sio.h>

#include <sys/time.h>
#include <time.h>

using namespace Bu;

bool isUp()
{
	try
	{
		Socket s("xagasoft.com", 9898, 1 );

		char buf[5];
		buf[s.read(buf, 2, 1, 0)] = '\0';
		
		if( !strcmp( buf, "hi" ) )
			return true;
		else
			return false;
	}
	catch(...)
	{
		return false;
	}
}

int main()
{
	uint32_t uUp = 0;
	uint32_t uDown = 0;
	uint32_t uTotal = 0;
	struct timeval tv;

	for(;;)
	{
		gettimeofday( &tv, NULL );
		time_t goal = ((tv.tv_sec/5)+1)*5;
		tv.tv_sec = goal-tv.tv_sec;
		tv.tv_usec = 0-tv.tv_usec;
		if( tv.tv_usec < 0 )
		{
			tv.tv_sec--;
			tv.tv_usec = 1000000+tv.tv_usec;
		}
		select( 0, NULL, NULL, NULL, &tv );
		gettimeofday( &tv, NULL );
		if( isUp() )
		{
			uUp++;
			sio << "status: up   ";
		}
		else
		{
			uDown++;
			sio << "status: down ";
		}
		uTotal++;

		sio << "(up=" << (uUp*5) << "s, down=" << (uDown*5) << ") up for "
			<< uUp*100/uTotal << "% of " << uTotal*5 << "s" << sio.nl
			<< sio.flush;
	}
}