llmessagethrottle.cpp

Go to the documentation of this file.
00001 
00032 #include "linden_common.h"
00033 
00034 #include "llhash.h"
00035 
00036 #include "llmessagethrottle.h"
00037 #include "llframetimer.h"
00038 
00039 // This is used for the stl search_n function.
00040 bool eq_message_throttle_entry(LLMessageThrottleEntry a, LLMessageThrottleEntry b)
00041                 { return a.getHash() == b.getHash(); }
00042 
00043 const U64 SEC_TO_USEC = 1000000;
00044                 
00045 // How long (in microseconds) each type of message stays in its throttle list.
00046 const U64 MAX_MESSAGE_AGE[MTC_EOF] =
00047 {
00048         10 * SEC_TO_USEC,       // MTC_VIEWER_ALERT
00049         10 * SEC_TO_USEC        // MTC_AGENT_ALERT
00050 };
00051 
00052 LLMessageThrottle::LLMessageThrottle()
00053 {
00054 }
00055 
00056 LLMessageThrottle::~LLMessageThrottle()
00057 {
00058 }
00059 
00060 void LLMessageThrottle::pruneEntries()
00061 {
00062         // Go through each message category, and prune entries older than max age.
00063         S32 cat;
00064         for (cat = 0; cat < MTC_EOF; cat++)
00065         {
00066                 message_list_t* message_list = &(mMessageList[cat]);
00067 
00068                 // Use a reverse iterator, since entries on the back will be the oldest.
00069                 message_list_reverse_iterator_t r_iterator      = message_list->rbegin();
00070                 message_list_reverse_iterator_t r_last          = message_list->rend();
00071 
00072                 // Look for the first entry younger than the maximum age.
00073                 F32 max_age = (F32)MAX_MESSAGE_AGE[cat]; 
00074                 BOOL found = FALSE;
00075                 while (r_iterator != r_last && !found)
00076                 {
00077                         if ( LLFrameTimer::getTotalTime() - (*r_iterator).getEntryTime() < max_age )
00078                         {
00079                                 // We found a young enough entry.
00080                                 found = TRUE;
00081 
00082                                 // Did we find at least one entry to remove?
00083                                 if (r_iterator != message_list->rbegin())
00084                                 {
00085                                         // Yes, remove it.
00086                                         message_list->erase(r_iterator.base(), message_list->end());
00087                                 }
00088                         }
00089                         else
00090                         {
00091                                 r_iterator++;
00092                         }
00093                 }
00094 
00095                 // If we didn't find any entries young enough to keep, remove them all.
00096                 if (!found)
00097                 {
00098                         message_list->clear();
00099                 }
00100         }
00101 }
00102 
00103 BOOL LLMessageThrottle::addViewerAlert(const LLUUID& to, const char* mesg)
00104 {
00105         message_list_t* message_list = &(mMessageList[MTC_VIEWER_ALERT]);
00106 
00107         // Concatenate from,to,mesg into one string.
00108         std::ostringstream full_mesg;
00109         full_mesg << to << mesg;
00110 
00111         // Create an entry for this message.
00112         size_t hash = llhash<const char*> (full_mesg.str().c_str());
00113         LLMessageThrottleEntry entry(hash, LLFrameTimer::getTotalTime());
00114 
00115         // Check if this message is already in the list.
00116         message_list_iterator_t found = std::search_n(message_list->begin(), message_list->end(),
00117                                                                                                   1, entry, eq_message_throttle_entry);
00118 
00119         if (found == message_list->end())
00120         {
00121                 // This message was not found.  Add it to the list.
00122                 message_list->push_front(entry);
00123                 return TRUE;
00124         }
00125         else
00126         {
00127                 // This message was already in the list.
00128                 return FALSE;
00129         }
00130 }
00131 
00132 BOOL LLMessageThrottle::addAgentAlert(const LLUUID& agent, const LLUUID& task, const char* mesg)
00133 {
00134         message_list_t* message_list = &(mMessageList[MTC_AGENT_ALERT]);
00135 
00136         // Concatenate from,to,mesg into one string.
00137         std::ostringstream full_mesg;
00138         full_mesg << agent << task << mesg;
00139 
00140         // Create an entry for this message.
00141         size_t hash = llhash<const char*> (full_mesg.str().c_str());
00142         LLMessageThrottleEntry entry(hash, LLFrameTimer::getTotalTime());
00143 
00144         // Check if this message is already in the list.
00145         message_list_iterator_t found = std::search_n(message_list->begin(), message_list->end(),
00146                                                                                                   1, entry, eq_message_throttle_entry);
00147 
00148         if (found == message_list->end())
00149         {
00150                 // This message was not found.  Add it to the list.
00151                 message_list->push_front(entry);
00152                 return TRUE;
00153         }
00154         else
00155         {
00156                 // This message was already in the list.
00157                 return FALSE;
00158         }
00159 }
00160 

Generated on Thu Jul 1 06:08:53 2010 for Second Life Viewer by  doxygen 1.4.7