aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-08-25 20:54:47 +0000
committerMike Buland <eichlan@xagasoft.com>2006-08-25 20:54:47 +0000
commit0a700ced28520be170c0965191f2450a2e4a82ac (patch)
tree6858e7178d9ddd30113824da4728729b06d018b5
parent0c2d075e795858779af102e932a881498e2268ae (diff)
downloadlibbu++-0a700ced28520be170c0965191f2450a2e4a82ac.tar.gz
libbu++-0a700ced28520be170c0965191f2450a2e4a82ac.tar.bz2
libbu++-0a700ced28520be170c0965191f2450a2e4a82ac.tar.xz
libbu++-0a700ced28520be170c0965191f2450a2e4a82ac.zip
Added tests and exception codes, so you're program can tell just how bad things
really are.
-rw-r--r--src/connection.cpp37
-rw-r--r--src/connection.h2
-rw-r--r--src/exceptions.h8
-rw-r--r--src/test/clistress/main.cpp39
-rw-r--r--src/test/srvstress/main.cpp86
5 files changed, 152 insertions, 20 deletions
diff --git a/src/connection.cpp b/src/connection.cpp
index 0e7f111..045ea17 100644
--- a/src/connection.cpp
+++ b/src/connection.cpp
@@ -196,7 +196,7 @@ bool Connection::open( const char *sAddr, int nPort )
196 return true; 196 return true;
197} 197}
198 198
199bool Connection::readInput() 199int Connection::readInput()
200{ 200{
201 char buffer[2048]; 201 char buffer[2048];
202 int nbytes; 202 int nbytes;
@@ -204,39 +204,33 @@ bool Connection::readInput()
204 204
205 for(;;) 205 for(;;)
206 { 206 {
207 memset( buffer, 0, 2048 ); 207 //memset( buffer, 0, 2048 );
208 208
209 nbytes = read( nSocket, buffer, 2048 ); 209 nbytes = read( nSocket, buffer, 2048 );
210 if (nbytes < 0) 210 if (nbytes < 0)
211 { 211 {
212 /* Read error. */ 212 /* Read error. */
213 //perror("readInput"); 213 //perror("readInput");
214 return false; 214 throw ConnectionException( excodeReadError, "Read error");
215 }
216 else if (nbytes == 0)
217 {
218 /* End-of-file. */
219 //perror("readInput");
220 return false;
221 } 215 }
222 else 216 else
223 { 217 {
224 nTotalRead += nbytes; 218 nTotalRead += nbytes;
225 appendInput( buffer, nbytes ); 219 appendInput( buffer, nbytes );
226 /* Data read. */ 220 /* Data read. */
227 if( nbytes < 2047 ) 221 if( nbytes < 2048 )
228 { 222 {
229 if( pProtocol != NULL && nTotalRead > 0 ) 223 break;
230 {
231 pProtocol->onNewData();
232 }
233
234 return true;
235 } 224 }
236 } 225 }
237 } 226 }
238 227
239 return true; 228 if( pProtocol != NULL && nTotalRead > 0 )
229 {
230 pProtocol->onNewData();
231 }
232
233 return nTotalRead;
240} 234}
241 235
242bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack ) 236bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack )
@@ -259,12 +253,17 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack
259 if( retval == -1 ) 253 if( retval == -1 )
260 { 254 {
261 // Oh my god!!! some kind of horrible problem!!!! 255 // Oh my god!!! some kind of horrible problem!!!!
256 throw ConnectionException( excodeBadReadError, "Bad Read error");
262 return false; 257 return false;
263 } 258 }
264 else if( retval ) 259 else if( retval )
265 { 260 {
266 // None of them have data, but the connection is still active. 261 // None of them have data, but the connection is still active.
267 return readInput(); 262 if( readInput() == 0 )
263 {
264 this->close();
265 throw ConnectionException( excodeConnectionClosed, "Connection closed");
266 }
268 } 267 }
269 else 268 else
270 { 269 {
@@ -283,7 +282,7 @@ void Connection::waitForInput( int nBytesIn, int nSec, int nUSec )
283 { 282 {
284 if( nSec == 0 && nUSec == 0 ) 283 if( nSec == 0 && nUSec == 0 )
285 { 284 {
286 throw ConnectionException("Socket Timeout"); 285 throw ConnectionException( excodeSocketTimeout, "Socket Timeout");
287 } 286 }
288 readInput( nSec, nUSec, &nSec, &nUSec ); 287 readInput( nSec, nUSec, &nSec, &nUSec );
289 rlen = getInputAmnt(); 288 rlen = getInputAmnt();
diff --git a/src/connection.h b/src/connection.h
index 5e86ff4..b5b121b 100644
--- a/src/connection.h
+++ b/src/connection.h
@@ -266,7 +266,7 @@ public:
266 * readInput function to control blocking time. 266 * readInput function to control blocking time.
267 *@returns True socket is still connected, otherwise false. 267 *@returns True socket is still connected, otherwise false.
268 */ 268 */
269 bool readInput(); 269 int readInput();
270 270
271 /** 271 /**
272 * Reads all pending input from the connection, blocking up to nSec 272 * Reads all pending input from the connection, blocking up to nSec
diff --git a/src/exceptions.h b/src/exceptions.h
index 27aec3c..b4126b7 100644
--- a/src/exceptions.h
+++ b/src/exceptions.h
@@ -8,4 +8,12 @@ subExceptionDecl( XmlException )
8subExceptionDecl( FileException ) 8subExceptionDecl( FileException )
9subExceptionDecl( ConnectionException ) 9subExceptionDecl( ConnectionException )
10 10
11enum eConnectionException
12{
13 excodeReadError,
14 excodeBadReadError,
15 excodeConnectionClosed,
16 excodeSocketTimeout
17};
18
11#endif 19#endif
diff --git a/src/test/clistress/main.cpp b/src/test/clistress/main.cpp
new file mode 100644
index 0000000..3f5038b
--- /dev/null
+++ b/src/test/clistress/main.cpp
@@ -0,0 +1,39 @@
1#include "connection.h"
2
3void _waitForLength( Connection &con, int len)
4{
5 int rlen = con.getInputAmnt();
6
7 if (rlen >= len)
8 return;
9
10 int time_left = 5;
11 int mic_left = 0;
12
13 while (rlen < len)
14 {
15 if (time_left == 0 && mic_left == 0)
16 {
17 throw "Socket Timeout";
18 }
19 con.readInput(time_left, mic_left, &time_left, &mic_left);
20 rlen = con.getInputAmnt();
21 }
22}
23
24int main()
25{
26 Connection c;
27
28 c.open("localhost", 4001 );
29
30 c.appendOutput("d");
31 c.writeOutput();
32
33 _waitForLength( c, 40 );
34
35 c.close();
36
37 return 0;
38}
39
diff --git a/src/test/srvstress/main.cpp b/src/test/srvstress/main.cpp
new file mode 100644
index 0000000..c7795e4
--- /dev/null
+++ b/src/test/srvstress/main.cpp
@@ -0,0 +1,86 @@
1#include "connectionmanager.h"
2#include "programlink.h"
3#include "linkedlist.h"
4#include "protocol.h"
5
6class StressProtocol : public Protocol
7{
8public:
9 bool onNewData()
10 {
11 switch( getConnection()->getInput()[0] )
12 {
13 case 'd':
14 throw "Hello";
15 break;
16 };
17
18 return true;
19 }
20
21 bool onNewConnection()
22 {
23 return true;
24 }
25};
26
27class StressMonitor : public ConnectionMonitor, public ProgramLink
28{
29public:
30 bool init()
31 {
32 return true;
33 }
34
35 bool deInit()
36 {
37 return true;
38 }
39
40 bool timeSlice()
41 {
42 }
43
44 bool onNewConnection( Connection *pCon, int nPort )
45 {
46 StressProtocol *sp = new StressProtocol();
47 pCon->setProtocol( sp );
48
49 printf(" sys: New connection: socket(%d), port(%d)\n",
50 pCon->getSocket(), nPort );
51
52 return true;
53 }
54
55 bool onClosedConnection( Connection *pCon )
56 {
57 printf(" sys: Closed connection: socket(%d)\n",
58 pCon->getSocket() );
59
60 return true;
61 }
62
63 LinkMessage *processIRM( LinkMessage *pMsg )
64 {
65 return NULL;
66 }
67};
68
69int main()
70{
71 printf("Starting server...\n");
72
73 ConnectionManager srv;
74 StressMonitor telnet;
75
76 srv.setConnectionMonitor( &telnet );
77
78 srv.startServer( 4001 );
79
80 for(;;)
81 {
82 srv.scanConnections( 5000, false );
83 }
84
85 return 0;
86}