diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-01-10 21:04:17 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-01-10 21:04:17 +0000 |
commit | 2ba3f84ab559da02a11aa000b3cecb3b3668af61 (patch) | |
tree | 266f450b512f607ec54d54af4fa8c13fdbe7ef91 /src/tests/tcpsocket.cpp | |
parent | ea18007633b31901f2ae275cc0576c3f7ce99fc9 (diff) | |
parent | 3611f253f6fdfa4954d374ab85ddaa7f799c130c (diff) | |
download | libbu++-2ba3f84ab559da02a11aa000b3cecb3b3668af61.tar.gz libbu++-2ba3f84ab559da02a11aa000b3cecb3b3668af61.tar.bz2 libbu++-2ba3f84ab559da02a11aa000b3cecb3b3668af61.tar.xz libbu++-2ba3f84ab559da02a11aa000b3cecb3b3668af61.zip |
Merged in the core branch. This is a major update that fixes many things, and
changes many others, including source files that were deleted and renamed.
Before doing this update, I reccomend a full clean, or even a fresh checkout.
Things to note, most outstanding about this update:
- Bu::Socket was changed to Bu::TcpSocket and the default mode is blocking.
- All templatized container classes are SharedCore now, which is good, but
SharedCore is inherently non-reentrant safe. However, all SharedCore classes
have a "clone" function that return a non-shared copy of the object, safe for
passing into a reentrant safe function accessing shared memory.
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 | |||