/*
Copyright 2005 Matthew J. Battey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
This software implements a platform independent Store and Forward Message Queue.
*/
#if !defined(_QSTORAGE_H_)
#define _QSTORAGE_H_
#pragma warning(disable: 4786)
#include "safmq.h"
#include "defs.h"
#include "Mutex.h"
#include "Signal.h"
#include <fstream>
#include <memory.h>
#include <set>
#include <map>
using namespace safmq;
/**
Operator to compare two uuid's. If the two uuid's are identical
then operator function returns true.
@param lhs The first uuid to compare
@param rhs The second uuid to compare
@return true If the two uuid's are equal
*/
inline bool operator==(const uuid& lhs, const uuid& rhs) {
return memcmp(&lhs, &rhs, sizeof(uuid)) == 0;
}
/**
Operator to compare two uuid's. If the first uuid is less than
the second then the operator function return strue
@param lhs The first uuid to compare
@param rhs The second uuid to compare
@return true If the first uuid is less than the second
*/
inline bool operator<(const uuid& lhs, const uuid& rhs) {
return memcmp(&lhs, &rhs, sizeof(uuid)) < 0;
}
/**
A class containing in-memory data to index the on-disk data in a message queue.
*/
struct QueueIndex {
/// The priority of the message
safmq::MessagePriorityEnum MessagePriority;
/// The identification of the message
uuid MessageID;
/// The class of the message
MessageClassEnum MessageClass;
/// The return-id of the message
uuid ReciptID;
/// The time the message was created
time_t Timestamp;
/// The number of seconds this message has to live
long TTL;
/// The id of the file the message resides in
long FileID;
/// The offset into the file where the message resides
std::fstream::off_type DataOffset;
/// A flag indicating the message has been or is about to be purged from disk
bool Purged;
QueueIndex() {
DataOffset = 0;
FileID = 0;
MessageClass = MC_USERMSG;
MessagePriority = MP_STANDARD;
MessageID = UUID_NULL;
ReciptID = UUID_NULL;
Purged = true;
}
QueueIndex(const QueueIndex& src) {
copy(src);
}
QueueIndex(MessagePriorityEnum MessagePriority, uuid MessageID, MessageClassEnum MessageClass, uuid ReciptID, time_t Timestamp, long TTL, long FileID, std::fstream::off_type DataOffset, bool Purged) {
this->MessagePriority = MessagePriority;
this->MessageID = MessageID;
this->MessageClass = MessageClass;
this->ReciptID = ReciptID;
this->Timestamp = Timestamp;
this->TTL = TTL;
this->FileID = FileID;
this->DataOffset = DataOffset;
this->Purged = Purged;
}
QueueIndex& operator=(const QueueIndex& src) {
copy(src);
return *this;
}
bool operator<(const QueueIndex& rhs) const {
return MessagePriority > rhs.MessagePriority;
}
void copy(const QueueIndex& src) {
MessagePriority = src.MessagePriority;
MessageID = src.MessageID;
MessageClass = src.MessageClass;
ReciptID = src.ReciptID;
Timestamp = src.Timestamp;
TTL = src.TTL;
FileID = src.FileID;
DataOffset = src.DataOffset;
Purged = src.Purged;
}
};
/**
Basic definition for data storage into
@field MsgMarker Always equal to 'QMsg' inorder to accurately locate the beginning of a message.
@field Purged Flag indicating whether the message has been purged from the queue and should be ignored.
@field Length Length of this record
@field Msg The message data
*/
#pragma pack(1)
struct QMsgStorageHdr {
QMsgStorageHdr() : MsgMarker(0), Purged(false), Length(0xFFFFFFFF) { }
long MsgMarker;
bool Purged;
unsigned long Length;
};
#pragma pack()
namespace safmq {
/**
Class to control queue storage...
*/
class QStorage
{
public: //behavior
typedef ra_priority_queue<QueueIndex> IndexQueue;
typedef std::map<long, long> FileUtilizationMap;
typedef long CursorHandle;
typedef std::map<CursorHandle, IndexQueue::cursor> CursorHandleMap;
typedef std::set<uuid> IDSet;
QStorage(const SAFMQ_WSTRING& name, const SAFMQ_STRING& storagedirectory, long maxfilesize=40*1024*1024);
virtual ~QStorage();
SAFMQ_WSTRING getName() { return name; }
/**
Place a message on the queue.
*/
ErrorCode Enqueue(QueueMessage& msg);
ErrorCode BeginRetrieve(bool retrievebody, QueueMessage& msg, QStorage::CursorHandle& retrieveID);
ErrorCode BeginRetrieveID(bool retrievebody, const uuid& id, QueueMessage& msg, QStorage::CursorHandle& retrieveID);
// TODO: ErrorCode BeginRetrievePriority(bool retrievebody, MessagePriorityenum priority, QueueMessage& msg, QStorage::CursorHandle& retrieveID);
ErrorCode CancelRetrieve(QStorage::CursorHandle retrieveID);
ErrorCode FinalizeRetrieve(QStorage::CursorHandle retrieveID);
ErrorCode FinalizeRetrieveCursor(QStorage::CursorHandle retrieveID);
ErrorCode PeekID(bool retrievebody, const uuid& id, QueueMessage& msg);
ErrorCode PeekFront(bool retrievebody, QueueMessage& msg);
ErrorCode OpenCursor(QStorage::CursorHandle& cursorID);
ErrorCode CloseCursor(QStorage::CursorHandle cursorID);
ErrorCode AdvanceCursor(QStorage::CursorHandle cursorID);
ErrorCode SeekID(const uuid& id, QStorage::CursorHandle cursorID);
// TODO: ErrorCode SeekFront(QStorage::CursorHandle cursorID);
// TODO: ErrorCode SeekFirstPriority(MessagePriorityEnum priority, QStorage::CursorHandle cursorID);
ErrorCode TestCursor(QStorage::CursorHandle cursorID);
ErrorCode PeekCursor(bool retrievebody, QStorage::CursorHandle cursorID, QueueMessage& msg);
ErrorCode BeginRetrieveCursor(bool retrievebody, QStorage::CursorHandle cursorID, QueueMessage& msg);
ErrorCode WaitForID(const uuid& id, long timeout, QStorage::CursorHandle& messageCursor);
ErrorCode WaitFront(long timeout);
ErrorCode TTLPurge();
protected: // behavior
void init();
void recover();
void recover_file(long fileid);
long getFileIDFromName(const SAFMQ_STRING& name);
SAFMQ_STRING& getFileNameFromID(SAFMQ_STRING& dest, long fileid);
SAFMQ_STRING& getCurrentFileName(SAFMQ_STRING& dest);
QStorage::CursorHandle nextCursorID();
ErrorCode statMessage(QStorage::CursorHandle cursorID, bool purged);
ErrorCode statMessage(QStorage::IndexQueue::cursor idxcur, bool purged);
ErrorCode testCursor(QStorage::CursorHandle cursorID, QStorage::CursorHandleMap::iterator& cur);
ErrorCode peekCursor(bool retrievebody, QStorage::CursorHandle cursorID, QueueMessage& msg);
ErrorCode peekCursor(bool retrievebody, QStorage::IndexQueue::cursor idxcur, QueueMessage& message);
ErrorCode openCursor(QStorage::CursorHandle& cursorID);
ErrorCode closeCursor(QStorage::CursorHandle cursorID);
ErrorCode seekID(const uuid& id, QStorage::CursorHandle cursorID);
ErrorCode retrieveCursor(QStorage::CursorHandle cursorID, QueueMessage& message);
private: // behavior
public: // data
protected: // data
IndexQueue index;
FileUtilizationMap fileutilization;
CursorHandleMap cursors;
long currentfileid;
SAFMQ_STRING storagedirectory;
long maxfilesize;
Mutex qmtx;
IDSet waitset;
Signal enqsignal;
Signal idsignal;
SAFMQ_WSTRING name;
static const SAFMQ_STRING DIRSEP;
private: // data
};
} // end of safmq namespace
#endif // !defined(_QSTORAGE_H_)