11package fi .helsinki .cs .tmc .spyware ;
22
3+ import static com .google .common .base .Preconditions .checkArgument ;
4+
35import com .google .common .collect .Iterables ;
46import fi .helsinki .cs .tmc .data .Course ;
57import fi .helsinki .cs .tmc .model .CourseDb ;
@@ -33,6 +35,7 @@ public class EventSendBuffer implements EventReceiver {
3335 public static final int DEFAULT_MAX_EVENTS = 64 * 1024 ;
3436 public static final int DEFAULT_AUTOSEND_THREHSOLD = DEFAULT_MAX_EVENTS / 2 ;
3537 public static final int DEFAULT_AUTOSEND_COOLDOWN = 30 *1000 ;
38+ public static final int DEFAULT_MAX_EVENTS_PER_SEND = 500 ;
3639
3740 private Random random = new Random ();
3841 private SpywareSettings settings ;
@@ -45,6 +48,7 @@ public class EventSendBuffer implements EventReceiver {
4548 private int eventsToRemoveAfterSend = 0 ;
4649 private int maxEvents = DEFAULT_MAX_EVENTS ;
4750 private int autosendThreshold = DEFAULT_AUTOSEND_THREHSOLD ;
51+ private int maxEventsPerSend = DEFAULT_MAX_EVENTS_PER_SEND ; // Servers have POST size limits
4852 private Cooldown autosendCooldown ;
4953
5054
@@ -70,17 +74,17 @@ public EventSendBuffer(SpywareSettings settings, ServerAccess serverAccess, Cour
7074 }
7175
7276 public void setSendingInterval (long interval ) {
77+ checkArgument (interval >= 0 );
7378 this .sendingTask .setInterval (interval );
7479 }
7580
7681 public void setSavingInterval (long interval ) {
82+ checkArgument (interval >= 0 );
7783 this .savingTask .setInterval (interval );
7884 }
7985
8086 public void setMaxEvents (int newMaxEvents ) {
81- if (newMaxEvents <= 0 ) {
82- throw new IllegalArgumentException ();
83- }
87+ checkArgument (newMaxEvents > 0 );
8488
8589 synchronized (sendQueue ) {
8690 if (newMaxEvents < maxEvents ) {
@@ -107,7 +111,17 @@ public void setAutosendThreshold(int autosendThreshold) {
107111 }
108112
109113 public void setAutosendCooldown (long durationMillis ) {
110- this .autosendCooldown .setDurationMillis (durationMillis );
114+ checkArgument (durationMillis > 0 );
115+ synchronized (sendQueue ) {
116+ this .autosendCooldown .setDurationMillis (durationMillis );
117+ }
118+ }
119+
120+ public void setMaxEventsPerSend (int maxEventsPerSend ) {
121+ checkArgument (maxEventsPerSend > 0 );
122+ synchronized (sendQueue ) {
123+ this .maxEventsPerSend = maxEventsPerSend ;
124+ }
111125 }
112126
113127 public void sendNow () {
@@ -174,9 +188,6 @@ public void close() {
174188
175189
176190 private SingletonTask sendingTask = new SingletonTask (new Runnable () {
177- // Sending too many at once may go over the server's POST size limit.
178- private static final int MAX_EVENTS_PER_SEND = 500 ;
179-
180191 @ Override
181192 public void run () {
182193 boolean shouldSendMore ;
@@ -198,7 +209,9 @@ public void run() {
198209
199210 log .log (Level .INFO , "Sending {0} events to {1}" , new Object [] { eventsToSend .size (), url });
200211
201- doSend (eventsToSend , url );
212+ if (!tryToSend (eventsToSend , url )) {
213+ shouldSendMore = false ;
214+ }
202215 } while (shouldSendMore );
203216 }
204217
@@ -207,7 +220,7 @@ private ArrayList<LoggableEvent> copyEventsToSendFromQueue() {
207220 ArrayList <LoggableEvent > eventsToSend = new ArrayList <LoggableEvent >(sendQueue .size ());
208221
209222 Iterator <LoggableEvent > i = sendQueue .iterator ();
210- while (i .hasNext () && eventsToSend .size () < MAX_EVENTS_PER_SEND ) {
223+ while (i .hasNext () && eventsToSend .size () < maxEventsPerSend ) {
211224 eventsToSend .add (i .next ());
212225 }
213226
@@ -235,7 +248,7 @@ private String pickDestinationUrl() {
235248 return url ;
236249 }
237250
238- private void doSend (final ArrayList <LoggableEvent > eventsToSend , final String url ) {
251+ private boolean tryToSend (final ArrayList <LoggableEvent > eventsToSend , final String url ) {
239252 CancellableCallable <Object > task = serverAccess .getSendEventLogJob (url , eventsToSend );
240253 Future <Object > future = BgTask .start ("Sending stats" , task );
241254
@@ -245,7 +258,7 @@ private void doSend(final ArrayList<LoggableEvent> eventsToSend, final String ur
245258 future .cancel (true );
246259 } catch (ExecutionException ex ) {
247260 log .log (Level .INFO , "Sending failed" , ex );
248- return ;
261+ return false ;
249262 }
250263
251264 log .log (Level .INFO , "Sent {0} events successfully to {1}" , new Object [] { eventsToSend .size (), url });
@@ -256,6 +269,7 @@ private void doSend(final ArrayList<LoggableEvent> eventsToSend, final String ur
256269 // then we may end up sending duplicate events later.
257270 // This will hopefully be very rare.
258271 savingTask .start ();
272+ return true ;
259273 }
260274
261275 private void removeSentEventsFromQueue () {
0 commit comments