diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-06-30 04:28:47 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-06-30 04:28:47 +0000 |
commit | 89df61f21e525c7a36846ce1a960ffba5501cdca (patch) | |
tree | 0789e81c229b8f55521ca1c14e01c74165bae99b /src/tests | |
parent | 4556c5f625ed143a2334acc26505b658b719b451 (diff) | |
download | libbu++-89df61f21e525c7a36846ce1a960ffba5501cdca.tar.gz libbu++-89df61f21e525c7a36846ce1a960ffba5501cdca.tar.bz2 libbu++-89df61f21e525c7a36846ce1a960ffba5501cdca.tar.xz libbu++-89df61f21e525c7a36846ce1a960ffba5501cdca.zip |
Bu::Client now gives you the option to add additional filters to the filter
chain on the base stream, which for the moment is a socket.
I also demonstrate this in the new rot13 test, with a rot13 filter, and a simple
echo protocol.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/rot13.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/tests/rot13.cpp b/src/tests/rot13.cpp new file mode 100644 index 0000000..e677440 --- /dev/null +++ b/src/tests/rot13.cpp | |||
@@ -0,0 +1,80 @@ | |||
1 | #include "bu/protocol.h" | ||
2 | #include "bu/multiserver.h" | ||
3 | #include "bu/client.h" | ||
4 | #include "bu/filter.h" | ||
5 | |||
6 | using namespace Bu; | ||
7 | |||
8 | class Rot13Filter : public Filter | ||
9 | { | ||
10 | public: | ||
11 | Rot13Filter( Stream &next ) : | ||
12 | Filter( next ) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | virtual ~Rot13Filter() | ||
17 | { | ||
18 | } | ||
19 | |||
20 | virtual void start() | ||
21 | { | ||
22 | } | ||
23 | |||
24 | virtual size_t stop() | ||
25 | { | ||
26 | return 0; | ||
27 | } | ||
28 | |||
29 | virtual size_t read( void *pBuf, size_t nBytes ) | ||
30 | { | ||
31 | return rNext.read( pBuf, nBytes ); | ||
32 | } | ||
33 | |||
34 | virtual size_t write( const void *pBuf, size_t nBytes ) | ||
35 | { | ||
36 | const char *cBuf = (const char *)pBuf; | ||
37 | char *buf = new char[nBytes]; | ||
38 | for( size_t j = 0; j < nBytes; j++ ) | ||
39 | { | ||
40 | if( cBuf[j] >= 'a' && cBuf[j] <= 'z' ) | ||
41 | buf[j] = (cBuf[j]-'a'+13)%26+'a'; | ||
42 | else if( cBuf[j] >= 'A' && cBuf[j] <= 'Z' ) | ||
43 | buf[j] = (cBuf[j]-'A'+13)%26+'A'; | ||
44 | else | ||
45 | buf[j] = cBuf[j]; | ||
46 | } | ||
47 | rNext.write( buf, nBytes ); | ||
48 | delete[] buf; | ||
49 | return nBytes; | ||
50 | } | ||
51 | }; | ||
52 | |||
53 | class Rot13Protocol : public Protocol | ||
54 | { | ||
55 | public: | ||
56 | void onNewConnection( Bu::Client *pClient ) | ||
57 | { | ||
58 | pClient->pushFilter<Rot13Filter>(); | ||
59 | } | ||
60 | |||
61 | void onNewData( Bu::Client *pClient ) | ||
62 | { | ||
63 | pClient->write( pClient->getInput().getStr(), pClient->getInputSize() ); | ||
64 | pClient->seek( pClient->getInputSize() ); | ||
65 | } | ||
66 | }; | ||
67 | |||
68 | int main() | ||
69 | { | ||
70 | MultiServer xSrv; | ||
71 | |||
72 | xSrv.setTimeout( 5 ); | ||
73 | xSrv.addProtocol( genProtocol<Rot13Protocol>, 9999 ); | ||
74 | |||
75 | for(;;) | ||
76 | xSrv.scan(); | ||
77 | |||
78 | return 0; | ||
79 | } | ||
80 | |||