KSquare Utilities
KKB::MsgQueue Class Reference

Will manage a buffer that will allow multiple threads to add and remove messages to a queue. More...

#include <MsgQueue.h>

Public Types

typedef MsgQueueMsgQueuePtr
 

Public Member Functions

 MsgQueue (const KKStr &_name)
 
 ~MsgQueue ()
 
void AddMsg (KKStrPtr msg)
 Take ownership of 'msg' and add to end of the queue. More...
 
void AddMsg (const KKStr &msg)
 
void AddMsgs (const KKStrListPtr msgs, bool takeOwnership)
 Adds the contents of 'msgs' to the message queue and depending on 'takeOwnership' will either assume that its owns (taking ownership) the KKStr instances or create duplicates. More...
 
KKStrListPtr GetAllMsgs ()
 Returns all messages that are currently in the queue. More...
 
KKStrPtr GetCopyOfLastMsg ()
 Returns a duplicate of the last string added to the message queue. More...
 
KKStrPtr GetNextMsg ()
 Removes from the queue the oldest message added to the queue that has not been removed. More...
 
kkint32 MemoryConsumedEstimated ()
 Returns an estimate of the amount of memory consumed in bytes by this instance. More...
 

Detailed Description

Will manage a buffer that will allow multiple threads to add and remove messages to a queue.

A 'GoalKeepeer' object 'gateKeeper' will be used to enforce integrity in the Multi-Threaded environment. It will guarantee that only one thread at a time can access the Queue.

Definition at line 19 of file MsgQueue.h.

Member Typedef Documentation

Definition at line 22 of file MsgQueue.h.

Constructor & Destructor Documentation

MsgQueue::MsgQueue ( const KKStr _name)

Definition at line 29 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::Create(), KKB::KKStr::KKStr(), KKB::GoalKeeperSimple::MemoryConsumedEstimated(), MsgQueue(), and KKB::operator+().

Referenced by MsgQueue().

29  : /* Name of buffer, must be unique */
30  gateKeeper (NULL),
31  memoryConsumed (0),
32  name (_name),
33  queue ()
34 {
35  GoalKeeperSimple::Create ("MsgQueue_" + name, gateKeeper);
36  memoryConsumed = sizeof (MsgQueue) + gateKeeper->MemoryConsumedEstimated ();
37 }
MsgQueue(const KKStr &_name)
Definition: MsgQueue.cpp:29
static void Create(const KKStr &_name, volatile GoalKeeperSimplePtr &_newGoalKeeper)
Create a GoalKeeperSimple object and avoid a race condition doing it.
kkint32 MemoryConsumedEstimated() const
MsgQueue::~MsgQueue ( )

Definition at line 41 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::Destroy().

42 {
43  while (this->queue.size () > 0)
44  {
45  KKStrPtr m = this->queue.front ();
46  this->queue.pop ();
47  delete m;
48  m = NULL;
49  }
50 
51  GoalKeeperSimple::Destroy (gateKeeper);
52  gateKeeper = NULL;
53 }
static void Destroy(volatile GoalKeeperSimplePtr &_goalKeeperInstance)
Destroys an existing instance of GoalKeeperSimple.

Member Function Documentation

void MsgQueue::AddMsg ( KKStrPtr  msg)

Take ownership of 'msg' and add to end of the queue.

Parameters
[in]msgPointer to message that is to be added to end of queue. The caller will pass ownership of this string to this instance of MsgQueue.Taking ownership of 'msg'

Definition at line 58 of file MsgQueue.cpp.

References KKB::KKStr::Concat(), KKB::GoalKeeperSimple::EndBlock(), KKB::KKException::KKException(), KKB::KKStr::MemoryConsumedEstimated(), and KKB::GoalKeeperSimple::StartBlock().

Referenced by KKB::KKThread::AddMsg().

59 {
60  if (msg == NULL)
61  {
62  KKStr errMsg;
63  errMsg << "MsgQueue::AddMsg == NULL";
64  cerr << std::endl << std::endl << errMsg << std::endl << std::endl;
65  throw KKException (errMsg);
66  }
67 
68  gateKeeper->StartBlock ();
69  queue.push (msg);
70  memoryConsumed = memoryConsumed + msg->MemoryConsumedEstimated ();
71  gateKeeper->EndBlock ();
72 } /* AddFrame */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
void MsgQueue::AddMsg ( const KKStr msg)

Definition at line 76 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::EndBlock(), KKB::KKStr::MemoryConsumedEstimated(), and KKB::GoalKeeperSimple::StartBlock().

Referenced by KKB::KKThread::AddMsg(), and KKB::RunLog::WriteLine().

77 {
78  gateKeeper->StartBlock ();
79  queue.push (new KKStr (msg));
80  memoryConsumed = memoryConsumed + msg.MemoryConsumedEstimated ();
81  gateKeeper->EndBlock ();
82 } /* AddFrame */
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
void MsgQueue::AddMsgs ( const KKStrListPtr  msgs,
bool  takeOwnership 
)

Adds the contents of 'msgs' to the message queue and depending on 'takeOwnership' will either assume that its owns (taking ownership) the KKStr instances or create duplicates.

If 'takeOwnership' is true will and the KKStr instances in 'msgs' to the queue otherwise it will make duplicates of the KKStr instances. It is up to the caller to make sure that they set the Owner flag on the 'msgs' instance; for example if 'takeOwnership' is 'true' then caller should set 'msgs.Owner (false);'.

Definition at line 86 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::EndBlock(), KKB::KKStr::MemoryConsumedEstimated(), and KKB::GoalKeeperSimple::StartBlock().

89 {
90  gateKeeper->StartBlock ();
92  for (idx = msgs->begin (); idx != msgs->end (); ++idx)
93  {
94  KKStrPtr msg = *idx;
95  if (takeOwnership)
96  queue.push (msg);
97  else
98  queue.push (new KKStr (*msg));
99 
100  memoryConsumed = memoryConsumed + msg->MemoryConsumedEstimated ();
101  }
102  gateKeeper->EndBlock ();
103 }
std::vector< KKStr * >::iterator iterator
Definition: KKQueue.h:88
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
KKStrListPtr MsgQueue::GetAllMsgs ( )

Returns all messages that are currently in the queue.

Definition at line 125 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::EndBlock(), KKB::KKStrList::KKStrList(), and KKB::GoalKeeperSimple::StartBlock().

126 {
127  KKStrListPtr result = new KKStrList ();
128  gateKeeper->StartBlock ();
129 
130  KKStrPtr msg = NULL;
131  while (queue.size () > 0)
132  {
133  msg = queue.front ();
134  queue.pop ();
135  result->PushOnBack (msg);
136  memoryConsumed = memoryConsumed - msg->MemoryConsumedEstimated ();
137  }
138 
139  gateKeeper->EndBlock ();
140 
141  return result;
142 } /* GetAllMsgs */
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
virtual void PushOnBack(EntryPtr _entry)
Definition: KKQueue.h:398
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
KKStrPtr MsgQueue::GetCopyOfLastMsg ( )

Returns a duplicate of the last string added to the message queue.

This will not effect the current copy of the message queue. The returned string will be owned by the caller who will be responsible for deleting it.

Definition at line 157 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::EndBlock(), and KKB::GoalKeeperSimple::StartBlock().

158 {
159  KKStrPtr msg = NULL;
160  gateKeeper->StartBlock ();
161  if (queue.size () > 0)
162  {
163  msg = new KKStr (*(queue.back ()));
164  }
165  gateKeeper->EndBlock ();
166  return msg;
167 }
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
KKStrPtr MsgQueue::GetNextMsg ( )

Removes from the queue the oldest message added to the queue that has not been removed.

The caller will get ownership of the string and be responsible for deleting it.

Definition at line 107 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::EndBlock(), and KKB::GoalKeeperSimple::StartBlock().

Referenced by KKB::KKThread::GetMsgs().

108 {
109  gateKeeper->StartBlock ();
110 
111  KKStrPtr msg = NULL;
112  if (queue.size () > 0)
113  {
114  msg = queue.front ();
115  queue.pop ();
116  memoryConsumed = memoryConsumed - msg->MemoryConsumedEstimated ();
117  }
118 
119  gateKeeper->EndBlock ();
120  return msg;
121 } /* GetNextMsg */
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
kkint32 MsgQueue::MemoryConsumedEstimated ( )

Returns an estimate of the amount of memory consumed in bytes by this instance.

This will help managed objects keep track of how much memory they are using in the unmanaged world.

Definition at line 146 of file MsgQueue.cpp.

References KKB::GoalKeeperSimple::EndBlock(), and KKB::GoalKeeperSimple::StartBlock().

Referenced by KKB::KKThread::MemoryConsumedEstimated().

147 {
148  kkint32 result = 0;
149  gateKeeper->StartBlock ();
150  result = memoryConsumed;
151  gateKeeper->EndBlock ();
152  return result;
153 } /* MemoryConsumedEstimated */
__int32 kkint32
Definition: KKBaseTypes.h:88
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.

The documentation for this class was generated from the following files: