diff options
Diffstat (limited to 'src/tests/tcpsocket.cpp')
-rw-r--r-- | src/tests/tcpsocket.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/tests/tcpsocket.cpp b/src/tests/tcpsocket.cpp new file mode 100644 index 0000000..30dd22f --- /dev/null +++ b/src/tests/tcpsocket.cpp | |||
@@ -0,0 +1,73 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2010 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include <bu/tcpsocket.h> | ||
9 | #include <bu/sio.h> | ||
10 | |||
11 | #include <sys/time.h> | ||
12 | #include <time.h> | ||
13 | |||
14 | using namespace Bu; | ||
15 | |||
16 | bool isUp() | ||
17 | { | ||
18 | try | ||
19 | { | ||
20 | TcpSocket s("xagasoft.com", 9898, 1 ); | ||
21 | |||
22 | char buf[5]; | ||
23 | buf[s.read(buf, 2, 1, 0)] = '\0'; | ||
24 | |||
25 | if( !strcmp( buf, "hi" ) ) | ||
26 | return true; | ||
27 | else | ||
28 | return false; | ||
29 | } | ||
30 | catch(...) | ||
31 | { | ||
32 | return false; | ||
33 | } | ||
34 | } | ||
35 | |||
36 | int main() | ||
37 | { | ||
38 | uint32_t uUp = 0; | ||
39 | uint32_t uDown = 0; | ||
40 | uint32_t uTotal = 0; | ||
41 | struct timeval tv; | ||
42 | |||
43 | for(;;) | ||
44 | { | ||
45 | gettimeofday( &tv, NULL ); | ||
46 | time_t goal = ((tv.tv_sec/5)+1)*5; | ||
47 | tv.tv_sec = goal-tv.tv_sec; | ||
48 | tv.tv_usec = 0-tv.tv_usec; | ||
49 | if( tv.tv_usec < 0 ) | ||
50 | { | ||
51 | tv.tv_sec--; | ||
52 | tv.tv_usec = 1000000+tv.tv_usec; | ||
53 | } | ||
54 | select( 0, NULL, NULL, NULL, &tv ); | ||
55 | gettimeofday( &tv, NULL ); | ||
56 | if( isUp() ) | ||
57 | { | ||
58 | uUp++; | ||
59 | sio << "status: up "; | ||
60 | } | ||
61 | else | ||
62 | { | ||
63 | uDown++; | ||
64 | sio << "status: down "; | ||
65 | } | ||
66 | uTotal++; | ||
67 | |||
68 | sio << "(up=" << (uUp*5) << "s, down=" << (uDown*5) << ") up for " | ||
69 | << uUp*100/uTotal << "% of " << uTotal*5 << "s" << sio.nl | ||
70 | << sio.flush; | ||
71 | } | ||
72 | } | ||
73 | |||