| 1 | |
|---|
| 2 | #include "Box.h" |
|---|
| 3 | |
|---|
| 4 | #ifdef HAVE_SYSLOG_H |
|---|
| 5 | #include <syslog.h> |
|---|
| 6 | #endif |
|---|
| 7 | |
|---|
| 8 | #include "autogen_TestProtocol.h" |
|---|
| 9 | #include "CollectInBufferStream.h" |
|---|
| 10 | |
|---|
| 11 | #include "MemLeakFindOn.h" |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | std::auto_ptr<TestProtocolMessage> TestProtocolHello::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 15 | { |
|---|
| 16 | if(mNumber32 != 41 || mNumber16 != 87 || mNumber8 != 11 || mText != "pingu") |
|---|
| 17 | { |
|---|
| 18 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolError(0, 0)); |
|---|
| 19 | } |
|---|
| 20 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolHello(12,89,22,std::string("Hello world!"))); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | std::auto_ptr<TestProtocolMessage> TestProtocolLists::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 24 | { |
|---|
| 25 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolListsReply(mLotsOfText.size())); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | std::auto_ptr<TestProtocolMessage> TestProtocolQuit::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 29 | { |
|---|
| 30 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolQuit); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | std::auto_ptr<TestProtocolMessage> TestProtocolSimple::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 34 | { |
|---|
| 35 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolSimpleReply(mValue+1)); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | class UncertainBufferStream : public CollectInBufferStream |
|---|
| 39 | { |
|---|
| 40 | public: |
|---|
| 41 | // make the collect in buffer stream pretend not to know how many bytes are left |
|---|
| 42 | pos_type BytesLeftToRead() |
|---|
| 43 | { |
|---|
| 44 | return IOStream::SizeOfStreamUnknown; |
|---|
| 45 | } |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | std::auto_ptr<TestProtocolMessage> TestProtocolGetStream::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 49 | { |
|---|
| 50 | // make a new stream object |
|---|
| 51 | CollectInBufferStream *pstream = mUncertainSize?(new UncertainBufferStream):(new CollectInBufferStream); |
|---|
| 52 | |
|---|
| 53 | // Data. |
|---|
| 54 | int values[24273]; |
|---|
| 55 | int v = mStartingValue; |
|---|
| 56 | for(int l = 0; l < 3; ++l) |
|---|
| 57 | { |
|---|
| 58 | for(int x = 0; x < 24273; ++x) |
|---|
| 59 | { |
|---|
| 60 | values[x] = v++; |
|---|
| 61 | } |
|---|
| 62 | pstream->Write(values, sizeof(values)); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // Finished |
|---|
| 66 | pstream->SetForReading(); |
|---|
| 67 | |
|---|
| 68 | // Get it to be sent |
|---|
| 69 | rProtocol.SendStreamAfterCommand(pstream); |
|---|
| 70 | |
|---|
| 71 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolGetStream(mStartingValue, mUncertainSize)); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | std::auto_ptr<TestProtocolMessage> TestProtocolSendStream::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 75 | { |
|---|
| 76 | if(mValue != 0x73654353298ffLL) |
|---|
| 77 | { |
|---|
| 78 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolError(0, 0)); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | // Get a stream |
|---|
| 82 | std::auto_ptr<IOStream> stream(rProtocol.ReceiveStream()); |
|---|
| 83 | bool uncertain = (stream->BytesLeftToRead() == IOStream::SizeOfStreamUnknown); |
|---|
| 84 | |
|---|
| 85 | // Count how many bytes in it |
|---|
| 86 | int bytes = 0; |
|---|
| 87 | char buffer[125]; |
|---|
| 88 | while(stream->StreamDataLeft()) |
|---|
| 89 | { |
|---|
| 90 | bytes += stream->Read(buffer, sizeof(buffer)); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // tell the caller how many bytes there were |
|---|
| 94 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolGetStream(bytes, uncertain)); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | std::auto_ptr<TestProtocolMessage> TestProtocolString::DoCommand(TestProtocolReplyable &rProtocol, TestContext &rContext) const |
|---|
| 98 | { |
|---|
| 99 | return std::auto_ptr<TestProtocolMessage>(new TestProtocolString(mTest)); |
|---|
| 100 | } |
|---|
| 101 | |
|---|