package shedar.mods.ic2.nuclearcontrol.panel.http;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import shedar.mods.ic2.nuclearcontrol.IC2NuclearControl;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import argo.jdom.JsonNodeBuilder;
import argo.jdom.JsonNodeBuilders;
import argo.jdom.JsonObjectNodeBuilder;

public class HttpCardSender {
	private static final String ID_URL_TEMPLATE = "http://sensors.modstats.org/api/v1/register?p=";
	private static final String DATA_URL_TEMPLATE = "http://sensors.modstats.org/api/v1/report";
	public static HttpCardSender instance = new HttpCardSender();

	@SuppressWarnings("rawtypes")
	private ConcurrentHashMap<Long, JsonNodeBuilder> unsent = new ConcurrentHashMap<Long, JsonNodeBuilder>();
	public ConcurrentLinkedQueue<Long> availableIds = new ConcurrentLinkedQueue<Long>();
	private ExecutorService executor = Executors.newFixedThreadPool(2);

	private HttpCardSender() {}

	public void requestId() {
		try {
			executor.submit(new Request(new URL(ID_URL_TEMPLATE + IC2NuclearControl.instance.httpSensorKey), null));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public void send() {
		try {
			executor.submit(new Request(new URL(DATA_URL_TEMPLATE), unsent));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public void add(NBTTagCompound cardData, UUID cardType, Long id) {
		JsonObjectNodeBuilder builder = JsonNodeBuilders.anObjectBuilder();
		builder.withField("id", JsonNodeBuilders.aNumberBuilder(id.toString()));
		builder.withField("type", JsonNodeBuilders.aStringBuilder(cardType.toString().replace("-", "")));
		Iterator iterator = cardData.func_150296_c().iterator();
		while (iterator.hasNext()) {
			String s = (String) iterator.next();
			NBTBase tag = cardData.getTag(s);
			if (!s.equals("_webSensorId")) {
				if (s.equals("energyL") || s.equals("maxStorageL")) {
					builder.withField(s, JsonNodeBuilders.aStringBuilder(String.valueOf(Double.valueOf(tag.toString()).longValue())));
				} else {
					builder.withField(s, JsonNodeBuilders.aStringBuilder(tag.toString()));
				}
			}
		}
		unsent.put(id, builder);
	}
}
@@ -161,16 +161,65 @@
 .UUID;%0D%0A
+import java.util.concurrent.ArrayBlockingQueue;%0D%0A
 import j
@@ -388,78 +388,125 @@
 ent.
-Executors
+RejectedExecutionException
 ;%0D%0A
-%0D%0A
 import 
-shedar.mods.ic2.nuclearcontrol.IC2NuclearControl;
+java.util.concurrent.ThreadPoolExecutor;%0D%0Aimport java.util.concurrent.TimeUnit;%0D%0A
 %0D%0Aim
@@ -580,16 +580,18 @@
 pound;%0D%0A
+%0D%0A
 import a
@@ -693,16 +693,74 @@
 uilder;%0D
+%0Aimport shedar.mods.ic2.nuclearcontrol.IC2NuclearControl;%0D
 %0A%0D%0Apubli
@@ -1085,16 +1085,22 @@
 private 
+final 
 Concurre
@@ -1277,16 +1277,93 @@
 ng%3E();%0D%0A
+%09// single thread executor service with a maximum queue size of 64 elements%0D%0A
 %09private
@@ -1363,16 +1363,22 @@
 private 
+final 
 Executor
@@ -1400,38 +1400,94 @@
 r = 
-Executors.newFixedThreadPool(2
+new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue%3CRunnable%3E(64)
 );%0D%0A
@@ -1722,32 +1722,185 @@
 tStackTrace();%0D%0A
+%09%09%7D catch (RejectedExecutionException e) %7B%0D%0A%09%09%09// the server isn't processing requests fast enough, drop the request%0D%0A%09%09%09// TODO: reschedule? fallback?%0D%0A
 %09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09publ
@@ -2060,16 +2060,169 @@
 ace();%0D%0A
+%09%09%7D catch (RejectedExecutionException e) %7B%0D%0A%09%09%09// the server isn't processing requests fast enough, drop the request%0D%0A%09%09%09// TODO: reschedule? fallback?%0D%0A
 %09%09%7D%0D%0A%09%7D%0D

Fix http request queue growing without bounds if the requests aren't being processed fast enough.