00001 #ifndef CRYPTOPP_MQUEUE_H
00002 #define CRYPTOPP_MQUEUE_H
00003
00004 #include "cryptlib.h"
00005 #include "queue.h"
00006 #include "filters.h"
00007 #include "misc.h"
00008
00009 #include <deque>
00010
00011 NAMESPACE_BEGIN(CryptoPP)
00012
00013
00014 class CRYPTOPP_DLL MessageQueue : public AutoSignaling<BufferedTransformation>
00015 {
00016 public:
00017 MessageQueue(unsigned int nodeSize=256);
00018
00019 void IsolatedInitialize(const NameValuePairs ¶meters)
00020 {m_queue.IsolatedInitialize(parameters); m_lengths.assign(1, 0U); m_messageCounts.assign(1, 0U);}
00021 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00022 {
00023 CRYPTOPP_UNUSED(blocking);
00024 m_queue.Put(begin, length);
00025 m_lengths.back() += length;
00026 if (messageEnd)
00027 {
00028 m_lengths.push_back(0);
00029 m_messageCounts.back()++;
00030 }
00031 return 0;
00032 }
00033 bool IsolatedFlush(bool hardFlush, bool blocking)
00034 {CRYPTOPP_UNUSED(hardFlush), CRYPTOPP_UNUSED(blocking); return false;}
00035 bool IsolatedMessageSeriesEnd(bool blocking)
00036 {CRYPTOPP_UNUSED(blocking); m_messageCounts.push_back(0); return false;}
00037
00038 lword MaxRetrievable() const
00039 {return m_lengths.front();}
00040 bool AnyRetrievable() const
00041 {return m_lengths.front() > 0;}
00042
00043 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
00044 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
00045
00046 lword TotalBytesRetrievable() const
00047 {return m_queue.MaxRetrievable();}
00048 unsigned int NumberOfMessages() const
00049 {return (unsigned int)m_lengths.size()-1;}
00050 bool GetNextMessage();
00051
00052 unsigned int NumberOfMessagesInThisSeries() const
00053 {return m_messageCounts[0];}
00054 unsigned int NumberOfMessageSeries() const
00055 {return (unsigned int)m_messageCounts.size()-1;}
00056
00057 unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
00058
00059 const byte * Spy(size_t &contiguousSize) const;
00060
00061 void swap(MessageQueue &rhs);
00062
00063 private:
00064 ByteQueue m_queue;
00065 std::deque<lword> m_lengths;
00066 std::deque<unsigned int> m_messageCounts;
00067 };
00068
00069
00070
00071 class CRYPTOPP_DLL EqualityComparisonFilter : public Unflushable<Multichannel<Filter> >
00072 {
00073 public:
00074 struct MismatchDetected : public Exception {MismatchDetected() : Exception(DATA_INTEGRITY_CHECK_FAILED, "EqualityComparisonFilter: did not receive the same data on two channels") {}};
00075
00076
00077 EqualityComparisonFilter(BufferedTransformation *attachment=NULL, bool throwIfNotEqual=true, const std::string &firstChannel="0", const std::string &secondChannel="1")
00078 : m_throwIfNotEqual(throwIfNotEqual), m_mismatchDetected(false)
00079 , m_firstChannel(firstChannel), m_secondChannel(secondChannel)
00080 {Detach(attachment);}
00081
00082 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
00083 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
00084
00085 private:
00086 unsigned int MapChannel(const std::string &channel) const;
00087 bool HandleMismatchDetected(bool blocking);
00088
00089 bool m_throwIfNotEqual, m_mismatchDetected;
00090 std::string m_firstChannel, m_secondChannel;
00091 MessageQueue m_q[2];
00092 };
00093
00094 NAMESPACE_END
00095
00096 #ifndef __BORLANDC__
00097 NAMESPACE_BEGIN(std)
00098 template<> inline void swap(CryptoPP::MessageQueue &a, CryptoPP::MessageQueue &b)
00099 {
00100 a.swap(b);
00101 }
00102 NAMESPACE_END
00103 #endif
00104
00105 #endif