aboutsummaryrefslogtreecommitdiff
path: root/src/tests/rot13.cpp
blob: 1530af3c1992e81f10d2fe4a38932ae15fc5ce1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/protocol.h"
#include "bu/multiserver.h"
#include "bu/client.h"
#include "bu/filter.h"

using namespace Bu;

class Rot13Filter : public Filter
{
public:
	Rot13Filter( Stream &next ) :
		Filter( next )
	{
	}

	virtual ~Rot13Filter()
	{
	}

	virtual void start()
	{
	}

	virtual Bu::size stop()
	{
		return 0;
	}

	virtual Bu::size read( void *pBuf, Bu::size nBytes )
	{
		return rNext.read( pBuf, nBytes );
	}
	
	virtual Bu::size write( const void *pBuf, Bu::size nBytes )
	{
		const char *cBuf = (const char *)pBuf;
		char *buf = new char[nBytes];
		for( Bu::size j = 0; j < nBytes; j++ )
		{
			if( cBuf[j] >= 'a' && cBuf[j] <= 'z' )
				buf[j] = (cBuf[j]-'a'+13)%26+'a';
			else if( cBuf[j] >= 'A' && cBuf[j] <= 'Z' )
				buf[j] = (cBuf[j]-'A'+13)%26+'A';
			else
				buf[j] = cBuf[j];
		}
		rNext.write( buf, nBytes );
		delete[] buf;
		return nBytes;
	}
};

class Rot13Protocol : public Protocol
{
public:
	void onNewConnection( Bu::Client *pClient )
	{
		pClient->pushFilter<Rot13Filter>();
	}

	void onNewData( Bu::Client *pClient )
	{
		char buf[1024];
		while( pClient->hasInput() )
		{
			int iAmnt = pClient->read( buf, 1024 );
			pClient->write( buf, iAmnt );
		}
	}
};

int main()
{
	MultiServer xSrv;

	xSrv.setTimeout( 5 );
	xSrv.addProtocol( genProtocol<Rot13Protocol>, 9999 );

	for(;;)
		xSrv.scan();

	return 0;
}