aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-03-20 03:21:59 +0000
committerMike Buland <eichlan@xagasoft.com>2008-03-20 03:21:59 +0000
commit68f4dfa0e2996211a9439809df07e36614618a63 (patch)
tree7e86ff3cdb8863629c77aa50b6b922678ce2cb4c
parent6c27b9b4024331558e6a719293f2b44a6ae77c28 (diff)
downloadlibbu++-68f4dfa0e2996211a9439809df07e36614618a63.tar.gz
libbu++-68f4dfa0e2996211a9439809df07e36614618a63.tar.bz2
libbu++-68f4dfa0e2996211a9439809df07e36614618a63.tar.xz
libbu++-68f4dfa0e2996211a9439809df07e36614618a63.zip
This is a preliminary test of my new server simplification subclass, don't use
it yet, the name will change. I really, really, really want the name to change.
Diffstat (limited to '')
-rw-r--r--src/multiserver.cpp36
-rw-r--r--src/multiserver.h46
-rw-r--r--src/tests/multiserver.cpp62
3 files changed, 144 insertions, 0 deletions
diff --git a/src/multiserver.cpp b/src/multiserver.cpp
new file mode 100644
index 0000000..56284b0
--- /dev/null
+++ b/src/multiserver.cpp
@@ -0,0 +1,36 @@
1#include "bu/multiserver.h"
2#include "bu/protocol.h"
3#include "bu/client.h"
4
5Bu::MultiServer::MultiServer()
6{
7}
8
9Bu::MultiServer::~MultiServer()
10{
11}
12
13void Bu::MultiServer::addProtocol( Bu::Protocol *(*proc)(), int iPort,
14 int nPoolSize )
15{
16 hProtos[iPort] = proc;
17 addPort( iPort, nPoolSize );
18}
19
20void Bu::MultiServer::addProtocol( Protocol *(*proc)(), const FString &sAddr,
21 int iPort, int nPoolSize )
22{
23 hProtos[iPort] = proc;
24 addPort( sAddr, iPort, nPoolSize );
25}
26
27void Bu::MultiServer::onNewConnection( Bu::Client *pClient, int nPort )
28{
29 pClient->setProtocol( hProtos.get( nPort )() );
30}
31
32void Bu::MultiServer::onClosedConnection( Bu::Client *pClient )
33{
34 delete pClient->getProtocol();
35}
36
diff --git a/src/multiserver.h b/src/multiserver.h
new file mode 100644
index 0000000..cf71203
--- /dev/null
+++ b/src/multiserver.h
@@ -0,0 +1,46 @@
1#ifndef BU_MULTI_SERVER_H
2#define BU_MULTI_SERVER_H
3
4#include "bu/server.h"
5#include "bu/hash.h"
6
7namespace Bu
8{
9 class Protocol;
10 class Client;
11
12 template<class T>
13 Protocol *genProtocol()
14 {
15 return new T;
16 }
17
18 class MultiServer : protected Server
19 {
20 public:
21 MultiServer();
22 virtual ~MultiServer();
23
24 void addProtocol( Protocol *(*proc)(), int iPort, int nPoolSize=40 );
25 void addProtocol( Protocol *(*proc)(), const FString &sAddr, int iPort,
26 int nPoolSize=40 );
27
28 void scan()
29 {
30 Server::scan();
31 }
32
33 void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 )
34 {
35 Server::setTimeout( nTimeoutSec, nTimeoutUSec );
36 }
37
38 virtual void onNewConnection( Client *pClient, int nPort );
39 virtual void onClosedConnection( Client *pClient );
40
41 private:
42 Bu::Hash<int, Protocol *(*)()> hProtos;
43 };
44}
45
46#endif
diff --git a/src/tests/multiserver.cpp b/src/tests/multiserver.cpp
new file mode 100644
index 0000000..f22147d
--- /dev/null
+++ b/src/tests/multiserver.cpp
@@ -0,0 +1,62 @@
1#include "bu/multiserver.h"
2#include "bu/protocol.h"
3#include "bu/client.h"
4
5class ProtocolRaw : public Bu::Protocol
6{
7public:
8 virtual void onNewConnection( Bu::Client *pClient )
9 {
10 pClient->write("Raw echo\n");
11 }
12
13 virtual void onNewData( Bu::Client *pClient )
14 {
15 pClient->write( pClient->getInput() );
16 pClient->seek( pClient->getInputSize() );
17 }
18};
19
20class ProtocolRot13 : public Bu::Protocol
21{
22public:
23 virtual void onNewConnection( Bu::Client *pClient )
24 {
25 pClient->write("Rot13 echo\n");
26 }
27
28 virtual void onNewData( Bu::Client *pClient )
29 {
30 Bu::FString sTmp = pClient->getInput();
31 for( int j = 0; j < sTmp.getSize(); j++ )
32 {
33 if( sTmp[j] >= 'a' && sTmp[j] <= 'z' )
34 {
35 sTmp[j] = ((sTmp[j]-'a'+13)%26) + 'a';
36 }
37 else if( sTmp[j] >= 'A' && sTmp[j] <= 'Z' )
38 {
39 sTmp[j] = ((sTmp[j]-'A'+13)%26) + 'A';
40 }
41 }
42 pClient->write( sTmp );
43 pClient->seek( pClient->getInputSize() );
44 }
45};
46
47int main()
48{
49 Bu::MultiServer msMain;
50
51 msMain.addProtocol( Bu::genProtocol<ProtocolRaw>, 5550 );
52 msMain.addProtocol( Bu::genProtocol<ProtocolRot13>, 5551 );
53 msMain.setTimeout( 5, 0 );
54
55 for(;;)
56 {
57 msMain.scan();
58 }
59
60 return 0;
61}
62