aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/llmessagethrottle.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/llmessage/llmessagethrottle.cpp')
-rw-r--r--linden/indra/llmessage/llmessagethrottle.cpp154
1 files changed, 154 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llmessagethrottle.cpp b/linden/indra/llmessage/llmessagethrottle.cpp
new file mode 100644
index 0000000..309a1d9
--- /dev/null
+++ b/linden/indra/llmessage/llmessagethrottle.cpp
@@ -0,0 +1,154 @@
1/**
2 * @file llmessagethrottle.cpp
3 * @brief LLMessageThrottle class used for throttling messages.
4 *
5 * Copyright (c) 2004-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#include "llhash.h"
29
30#include "llmessagethrottle.h"
31#include "llframetimer.h"
32
33// This is used for the stl search_n function.
34bool eq_message_throttle_entry(LLMessageThrottleEntry a, LLMessageThrottleEntry b)
35 { return a.getHash() == b.getHash(); }
36
37const U64 SEC_TO_USEC = 1000000;
38
39// How long (in microseconds) each type of message stays in its throttle list.
40const U64 MAX_MESSAGE_AGE[MTC_EOF] =
41{
42 10 * SEC_TO_USEC, // MTC_VIEWER_ALERT
43 10 * SEC_TO_USEC // MTC_AGENT_ALERT
44};
45
46LLMessageThrottle::LLMessageThrottle()
47{
48}
49
50LLMessageThrottle::~LLMessageThrottle()
51{
52}
53
54void LLMessageThrottle::pruneEntries()
55{
56 // Go through each message category, and prune entries older than max age.
57 S32 cat;
58 for (cat = 0; cat < MTC_EOF; cat++)
59 {
60 message_list_t* message_list = &(mMessageList[cat]);
61
62 // Use a reverse iterator, since entries on the back will be the oldest.
63 message_list_reverse_iterator_t r_iterator = message_list->rbegin();
64 message_list_reverse_iterator_t r_last = message_list->rend();
65
66 // Look for the first entry younger than the maximum age.
67 F32 max_age = (F32)MAX_MESSAGE_AGE[cat];
68 BOOL found = FALSE;
69 while (r_iterator != r_last && !found)
70 {
71 if ( LLFrameTimer::getTotalTime() - (*r_iterator).getEntryTime() < max_age )
72 {
73 // We found a young enough entry.
74 found = TRUE;
75
76 // Did we find at least one entry to remove?
77 if (r_iterator != message_list->rbegin())
78 {
79 // Yes, remove it.
80 message_list->erase(r_iterator.base(), message_list->end());
81 }
82 }
83 else
84 {
85 r_iterator++;
86 }
87 }
88
89 // If we didn't find any entries young enough to keep, remove them all.
90 if (!found)
91 {
92 message_list->clear();
93 }
94 }
95}
96
97BOOL LLMessageThrottle::addViewerAlert(const LLUUID& to, const char* mesg)
98{
99 message_list_t* message_list = &(mMessageList[MTC_VIEWER_ALERT]);
100
101 // Concatenate from,to,mesg into one string.
102 std::ostringstream full_mesg;
103 full_mesg << to << mesg;
104
105 // Create an entry for this message.
106 size_t hash = llhash<const char*> (full_mesg.str().c_str());
107 LLMessageThrottleEntry entry(hash, LLFrameTimer::getTotalTime());
108
109 // Check if this message is already in the list.
110 message_list_iterator_t found = std::search_n(message_list->begin(), message_list->end(),
111 1, entry, eq_message_throttle_entry);
112
113 if (found == message_list->end())
114 {
115 // This message was not found. Add it to the list.
116 message_list->push_front(entry);
117 return TRUE;
118 }
119 else
120 {
121 // This message was already in the list.
122 return FALSE;
123 }
124}
125
126BOOL LLMessageThrottle::addAgentAlert(const LLUUID& agent, const LLUUID& task, const char* mesg)
127{
128 message_list_t* message_list = &(mMessageList[MTC_AGENT_ALERT]);
129
130 // Concatenate from,to,mesg into one string.
131 std::ostringstream full_mesg;
132 full_mesg << agent << task << mesg;
133
134 // Create an entry for this message.
135 size_t hash = llhash<const char*> (full_mesg.str().c_str());
136 LLMessageThrottleEntry entry(hash, LLFrameTimer::getTotalTime());
137
138 // Check if this message is already in the list.
139 message_list_iterator_t found = std::search_n(message_list->begin(), message_list->end(),
140 1, entry, eq_message_throttle_entry);
141
142 if (found == message_list->end())
143 {
144 // This message was not found. Add it to the list.
145 message_list->push_front(entry);
146 return TRUE;
147 }
148 else
149 {
150 // This message was already in the list.
151 return FALSE;
152 }
153}
154