Skip to content
This repository has been archived by the owner on May 2, 2019. It is now read-only.

Commit

Permalink
Stop RequestReceiver tasks when unloading CENOClient; Change conditio…
Browse files Browse the repository at this point in the history
…n that was causing RequestReceiver to inserting empty batch requests
  • Loading branch information
misaakidis committed Nov 20, 2015
1 parent 9daded1 commit 8d2590e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
6 changes: 4 additions & 2 deletions ceno-freenet/src/plugins/CENO/Client/CENOClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ public void terminate()
initConfig.setProperty("lastSynced", String.valueOf(channelMaker.getLastSynced()));
initConfig.storeProperties();
}
*/
*/

if(channelMakerThread != null) {
channelMakerThread.interrupt();
}

RequestSender.getInstance().stopTimerTasks();

//TODO Release ULPRs' resources
Logger.normal(this, PLUGIN_NAME + " terminated.");
}
Expand Down
39 changes: 22 additions & 17 deletions ceno-freenet/src/plugins/CENO/Client/RequestSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import plugins.CENO.Client.ULPRManager.ULPRStatus;
import plugins.CENO.Client.Signaling.Channel;
import plugins.CENO.Common.URLtoUSKTools;

Expand All @@ -34,7 +33,7 @@ public class RequestSender {
/**
* Maximum size in bytes of a batch request in order to fit within the USK chunk
*/
private static final long MAX_BATCH_SIZE = 2^10;
private static final long MAX_BATCH_SIZE = 1024;

private RequestSender() {
this.requestTable = new Hashtable<String, Long>();
Expand All @@ -43,6 +42,13 @@ private RequestSender() {
timer.schedule(new BatchReqInserter(), 0, TimeUnit.MINUTES.toMillis(3));
}

public void stopTimerTasks() {
if (timer != null) {
timer.cancel();
timer.purge();
}
}

public static RequestSender getInstance() {
return requestSender;
}
Expand All @@ -58,21 +64,18 @@ public void requestFromBridge(String url) {
}

public synchronized boolean shouldSignalBridge(String url) {
String urlValidated = null;
try {
URLtoUSKTools.validateURL(url);
urlValidated = URLtoUSKTools.validateURL(url);
} catch (MalformedURLException e) {
return false;
}

if (ULPRManager.getULPRStatus(url) == ULPRStatus.succeeded) {
return false;
}

if (!requestTable.containsKey(url)) {
if (!requestTable.containsKey(urlValidated)) {
requestTable.put(url, System.currentTimeMillis());
}

return (System.currentTimeMillis() - requestTable.get(url) > SHOULD_QUEUE_URL);
return (System.currentTimeMillis() - requestTable.get(urlValidated) > SHOULD_QUEUE_URL);
}

private void addInBatch(String url) {
Expand Down Expand Up @@ -104,21 +107,23 @@ public void run() {

for (int i = batchList.size() - 1; i >= 0; i--) {
String url = batchList.get(i);
if (shouldSignalBridge(url)) {
batchSize += url.getBytes().length;
if (batchSize <= MAX_BATCH_SIZE) {
batchListStr.append(url);
batchListStr.append("\n");
}
batchSize += url.getBytes().length;
if (batchSize < MAX_BATCH_SIZE) {
batchListStr.append(url);
batchListStr.append("\n");
}
}

if (Channel.insertBatch(batchListStr.toString())) {
String batchReq = batchListStr.toString();
if (batchReq.isEmpty()) {
return;
}

if (Channel.insertBatch(batchReq)) {
newUrlArrived = false;
}
}
}

}

}

0 comments on commit 8d2590e

Please sign in to comment.