summaryrefslogtreecommitdiff
path: root/src/tests/rot13.cpp
blob: 309707ee13c3f18e5b4e3c20fec8c298c52a6ce1 (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
/*
 * Copyright (C) 2007-2008 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 size_t stop()
	{
		return 0;
	}

	virtual size_t read( void *pBuf, size_t nBytes )
	{
		return rNext.read( pBuf, nBytes );
	}
	
	virtual size_t write( const void *pBuf, size_t nBytes )
	{
		const char *cBuf = (const char *)pBuf;
		char *buf = new char[nBytes];
		for( size_t 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 )
	{
		pClient->write( pClient->getInput().getStr(), pClient->getInputSize() );
		pClient->seek( pClient->getInputSize() );
	}
};

int main()
{
	MultiServer xSrv;

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

	for(;;)
		xSrv.scan();

	return 0;
}