package com.eactive.eai.adapter.socket2.protocol.http.servlet;

import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
import com.eactive.eai.adapter.socket2.protocol.http.HttpServlet;
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpRequest;
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpResponse;
import com.eactive.eai.common.util.Logger;

import java.text.DecimalFormat;

public class MemoryServlet extends HttpServlet {
    static Logger logger = SocketLoggerManager.getInstance().getLogger("FileLogger{NET}_HTTP");

	protected void doService(HttpRequest request, HttpResponse response) throws Exception {
		response.setContentType("text/html");
		response.setResponseCode(HttpResponse.HTTP_STATUS_SUCCESS);
		response.appendBody("<pre>");
		response.appendBody(memoryStat());
		response.appendBody("</pre>");
	}
	
	private String memoryStat() throws Exception {
	   Runtime rt = Runtime.getRuntime();
	   long total = rt.totalMemory();
	   long free = rt.freeMemory();
	   long max = rt.maxMemory();
	   
	   DecimalFormat df = new DecimalFormat("###,###,###");;

	   StringBuilder sb = new StringBuilder();
	   sb.append(String.format("total : %15s B\n", df.format(total)));
	   sb.append(String.format(" used : %15s B\n", df.format(total-free)));
	   sb.append(String.format(" free : %15s B\n", df.format(free)));
	   sb.append(String.format("  max : %15s B\n", df.format(max)));
	   return sb.toString();
	}
}
