Revision: 4784
          http://cel.svn.sourceforge.net/cel/?rev=4784&view=rev
Author:   jorrit
Date:     2012-03-14 05:42:49 +0000 (Wed, 14 Mar 2012)
Log Message:
-----------
Worked on logging of messages and implemented message cycling.

Modified Paths:
--------------
    cel/trunk/include/propclass/messenger.h
    cel/trunk/plugins/propclass/messenger/messengerfact.cpp
    cel/trunk/plugins/propclass/messenger/messengerfact.h

Modified: cel/trunk/include/propclass/messenger.h
===================================================================
--- cel/trunk/include/propclass/messenger.h     2012-03-14 05:36:31 UTC (rev 
4783)
+++ cel/trunk/include/propclass/messenger.h     2012-03-14 05:42:49 UTC (rev 
4784)
@@ -57,10 +57,15 @@
   virtual const char* Next () = 0;
 
   /**
-   * Get the id of the next message as it will be returned by Next()
+   * Get the id of the previous message as it was returned by Next()
    * but don't advance the iterator.
    */
   virtual const char* GetID () = 0;
+
+  /**
+   * Get the amount of times this message has been given to the user.
+   */
+  virtual size_t GetCounter () = 0;
 };
 
 /**

Modified: cel/trunk/plugins/propclass/messenger/messengerfact.cpp
===================================================================
--- cel/trunk/plugins/propclass/messenger/messengerfact.cpp     2012-03-14 
05:36:31 UTC (rev 4783)
+++ cel/trunk/plugins/propclass/messenger/messengerfact.cpp     2012-03-14 
05:42:49 UTC (rev 4784)
@@ -38,29 +38,32 @@
   iMessageLogIterator>
 {
 private:
-  MessageType* mt;
-  size_t idx;
+  MessageHash::GlobalIterator it;
+  csString id;
+  size_t counter;
 
 public:
-  MessageLogIterator (MessageType* mt) :
-    scfImplementationType (this), mt (mt), idx (0) { }
+  MessageLogIterator (MessageHash::GlobalIterator it) :
+    scfImplementationType (this), it (it) { }
   virtual ~MessageLogIterator () { }
   virtual bool HasNext () const
   {
-    MessageLog& log = mt->GetLog ();
-    return idx < log.GetCount ();
+    return it.HasNext ();
   }
   virtual const char* Next ()
   {
-    MessageLog& log = mt->GetLog ();
-    idx++;
-    return log.GetMessage (idx-1);
+    MessageCounter msg = it.Next (id);
+    counter = msg.counter;
+    return msg.message;
   }
   virtual const char* GetID ()
   {
-    MessageLog& log = mt->GetLog ();
-    return log.GetID (idx);
+    return id;
   }
+  virtual size_t GetCounter ()
+  {
+    return counter;
+  }
 };
 
 //---------------------------------------------------------------------------
@@ -789,21 +792,57 @@
 void celPcMessenger::Message (const char* type, const char* id,
       const csStringArray& msgs)
 {
-printf ("Message %s/%s\n", type, id); fflush (stdout);
   MessageType* mt = GetType (type);
   if (!mt)
   {
     Error ("Can't find message type '%s'!\n", type);
     return;
   }
-  if (mt->GetDoLog ())
+
+  MessageLog& log = mt->GetLog ();
+  CycleType cycleType;
+  if (!id || !*id) cycleType = mt->GetCycleFirst ();
+  else if (log.IDExists (id)) cycleType = mt->GetCycleNext ();
+  else cycleType = mt->GetCycleFirst ();
+
+  const char* msg = 0;
+  switch (cycleType)
   {
-    // @@@ Keep message.
+    case CYCLE_RANDOM: msg = msgs[rng.Get (msgs.GetSize ())]; break;
+    case CYCLE_SEQUENCE:
+      {
+       size_t counter = 0;
+        if (id && *id) counter = log.GetMessageCount (id);
+       msg = msgs[counter % msgs.GetSize ()];
+      }
+      break;
+    case CYCLE_NONE:
+      msg = 0;
+      break;
+    default:
+      if (int (cycleType) >= int (CYCLE_INDEX))
+      {
+       int idx = int (cycleType) - int (CYCLE_INDEX);
+       if (size_t (idx) >= msgs.GetSize ())
+       {
+         printf ("Warning! Not enough messages!\n");
+         idx = msgs.GetSize ()-1;
+       }
+       msg = msgs[idx];
+      }
+      break;
   }
-  if (mt->GetSlot ())
+
+  if (id && *id)
   {
-    // @@@ Select the right message.
-    mt->GetSlot ()->Message (msgs[0], mt->GetTimeout(), mt->GetFadetime (),
+    if (mt->GetDoLog ())
+      log.PushMessage (id, msg);
+    else
+      log.PushMessage (id, "X");       // Also log in case we have an id.
+  }
+  if (msg && mt->GetSlot ())
+  {
+    mt->GetSlot ()->Message (msg, mt->GetTimeout(), mt->GetFadetime (),
        mt->GetFont (), mt->GetTextColor ());
   }
 }
@@ -832,10 +871,9 @@
   for (size_t i = 0 ; i < types.GetSize () ; i++)
   {
     MessageLog& log = types.Get (i)->GetLog ();
-    size_t idx = log.GetMessageIndex (id);
-    if (idx != csArrayItemNotFound)
+    if (log.IDExists (id))
     {
-      log.ClearMessage (idx);
+      log.ClearMessage (id);
       return;
     }
   }
@@ -857,7 +895,7 @@
 {
   MessageType* mt = GetType (type);
   if (!mt) return 0;
-  return new MessageLogIterator (mt);
+  return new MessageLogIterator (mt->GetLog ().GetIterator ());
 }
 
 MessageLog* celPcMessenger::GetMessageLog (const char* type)

Modified: cel/trunk/plugins/propclass/messenger/messengerfact.h
===================================================================
--- cel/trunk/plugins/propclass/messenger/messengerfact.h       2012-03-14 
05:36:31 UTC (rev 4783)
+++ cel/trunk/plugins/propclass/messenger/messengerfact.h       2012-03-14 
05:42:49 UTC (rev 4784)
@@ -27,6 +27,7 @@
 #include "ivideo/graph3d.h"
 #include "csutil/scf.h"
 #include "csutil/parray.h"
+#include "csutil/randomgen.h"
 #include "cstool/pen.h"
 #include "physicallayer/propclas.h"
 #include "physicallayer/propfact.h"
@@ -41,39 +42,57 @@
 
 //---------------------------------------------------------------------------
 
+struct MessageCounter
+{
+  csString message;
+  size_t counter;
+  MessageCounter () : counter (0) { }
+};
+
+typedef csHash<MessageCounter,csString> MessageHash;
+
 class MessageLog
 {
 private:
-  csStringArray ids;
-  csStringArray messages;
+  MessageHash idToMessage;
 
 public:
-  size_t GetCount () { return ids.GetSize (); }
-  const char* GetID (size_t idx) const { return ids.Get (idx); }
-  const char* GetMessage (size_t idx) const { return messages.Get (idx); }
+  size_t GetCount () { return idToMessage.GetSize (); }
+  MessageHash::GlobalIterator GetIterator ()
+  {
+    return idToMessage.GetIterator ();
+  }
   void PushMessage (const char* id, const char* msg)
   {
-    ids.Push (id);
-    messages.Push (msg);
+    MessageCounter mc;
+    if (idToMessage.Contains (id))
+    {
+      idToMessage.Get (id, mc).counter++;
+    }
+    else
+    {
+      mc.message = msg;
+      mc.counter = 1;
+      idToMessage.Put (id, mc);
+    }
   }
   void ClearLog ()
   {
-    ids.DeleteAll ();
-    messages.DeleteAll ();
+    idToMessage.DeleteAll ();
   }
-  size_t GetMessageIndex (const char* id)
+  bool IDExists (const char* id)
   {
-    // @@@ Not efficient!
-    for (size_t i = 0 ; i < ids.GetSize () ; i++)
-      if (ids[i] == id)
-       return i;
-    return csArrayItemNotFound;
+    return idToMessage.Contains (id);
   }
-  void ClearMessage (size_t idx)
+  size_t GetMessageCount (const char* id)
   {
-    ids.DeleteIndex (idx);
-    messages.DeleteIndex (idx);
+    MessageCounter mc;
+    return idToMessage.Get (id, mc).counter;
   }
+  void ClearMessage (const char* id)
+  {
+    idToMessage.DeleteAll (id);
+  }
 };
 
 //---------------------------------------------------------------------------
@@ -299,6 +318,7 @@
 
   csRef<iVirtualClock> vc;
   csRef<iGraphics3D> g3d;
+  csRandomGen rng;
 
   // For actions.
   enum actionids

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Cel-cvs-update mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cel-cvs-update

Reply via email to