package gcm.custom;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.json.JSONObject;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response.Status;

public class PushServerReciver extends NanoHTTPD {

	private static final int PORT = 9876;

	public static PushServerReciver instance;

	public static PushServerReciver getInstance() throws IOException {
		if (instance == null) {
			instance = new PushServerReciver();
		}
		return instance;
	}

	public static void main(String[] args) throws IOException {
		PushServerReciver.getInstance();
	}

	private ConcurrentLinkedQueue<JSONObject> pushNotifications = new ConcurrentLinkedQueue<>();

	private String pushToken;

	private PushServerReciver() throws IOException {
		super(PORT);
		start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
		System.out.println("Running " + PORT);
	}

	public synchronized Response serve(IHTTPSession session) {
		Map<String, String> files = new HashMap<String, String>();
		Method method = session.getMethod();
		if (Method.POST.equals(method)) {

			try {
				session.parseBody(files);
			} catch (Exception e) {
				e.printStackTrace();
			}

			// get the POST body
			String postBody = files.get("postData");
			JSONObject body = new JSONObject(postBody);
			String type = body.optString("pushToken", null);
			if (type == null) {
				pushNotifications.add(body);
				System.out.println(body);

			} else {
				String pushToken = body.getString("pushToken");
				if (!pushToken.equals(this.pushToken)) {
					this.pushToken = pushToken;
					System.out.println(pushToken);
				}
			}

		}

		return newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, "ok i am ");
	}

	public void clearQueue() {
		this.pushNotifications.clear();
	}

	public String getPushToken(long timeOut) throws InterruptedException {
		long waitet = 0;
		while (waitet < timeOut) {
			if (this.pushToken != null && !this.pushToken.isEmpty()) {
				return this.pushToken;
			}
			Thread.sleep(10);
			waitet += 10;

		}
		throw new PushServerTimeOutException("No Push received in " + timeOut + "ms");
	}

	public JSONObject getPush(long timeOut) throws InterruptedException {
		long waitet = 0;
		while (waitet < timeOut) {
			JSONObject m = pushNotifications.poll();

			if (m != null) {
				return m;
			} else {
				Thread.sleep(10);
				waitet += 10;
			}
		}
		throw new PushServerTimeOutException("Push recived in " + timeOut + "ms");
	}

}
@@ -456,16 +456,124 @@
 ption %7B%0A
+%09%09return getInstance(PORT);%0A%09%7D%0A%0A%09public static PushServerReciver getInstance(int port) throws IOException %7B%0A
 %09%09if (in
@@ -626,16 +626,20 @@
 Reciver(
+port
 );%0A%09%09%7D%0A%09
@@ -910,16 +910,24 @@
 Reciver(
+int port
 ) throws
@@ -949,20 +949,20 @@
 %09%09super(
-PORT
+port
 );%0A%09%09sta
@@ -1007,49 +1007,8 @@
 e);%0A
-%09%09System.out.println(%22Running %22 + PORT);%0A
 %09%7D%0A%0A
@@ -1528,39 +1528,8 @@
 y);%0A
-%09%09%09%09System.out.println(body);%0A%0A
 %09%09%09%7D
@@ -1629,24 +1629,24 @@
 shToken)) %7B%0A
+
 %09%09%09%09%09this.pu
@@ -1670,44 +1670,8 @@
 en;%0A
-%09%09%09%09%09System.out.println(pushToken);%0A
 %09%09%09%09

add port to get instance method