<%@ page import="java.io.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="com.eactive.eai.adapter.socket2.config.SocketAdapterManager"%>
<%@ page import="com.eactive.eai.common.session.SessionManager,com.eactive.eai.common.session.TerminalVO"%>
<%@ page import="com.eactive.eai.common.session.CacheInfoVO"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.mina.common.IoSession"%>
<%@ page import="java.util.concurrent.ConcurrentMap"%>
<%@ page import="java.util.concurrent.ConcurrentHashMap"%>
<%@ page import="net.sf.ehcache.Cache"%>
<%@ page import="net.sf.ehcache.Cache,net.sf.ehcache.Element"%>
<%@ page import="com.eactive.eai.adapter.socket2.common.ExpiringMap"%>
<%@ page import="com.eactive.eai.common.util.DatetimeUtil"%>
<%@ page import="com.eactive.eai.common.server.EAIServerManager"%>
<%@ page import="com.eactive.eai.adapter.socket2.config.SocketAdapterManager"%>
<%@ page import="com.eactive.eai.adapter.socket2.protocol.ISocketAdapter"%>
<%@ page import="org.apache.mina.common.IoSession"%>
<%@ page import="net.sf.ehcache.Cache,net.sf.ehcache.Element"%>
<%@ page import="net.sf.ehcache.Cache"%>
<%@ page import="com.eactive.eai.adapter.socket2.common.ExpiringMap"%>
<%@ page import="com.eactive.eai.adapter.socket2.common.TimeUtil"%>
<%@ page import="com.eactive.eai.common.server.EAIServerManager"%>
<HTML>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body {	
	font-size: 9pt;
	scrollbar-face-color: #DCDCDC;
	scrollbar-shadow-color:#FFFFFF;
	scrollbar-highlight-color:#FFFFFF;
	scrollbar-3dlight-color: #BBBBBB;
	scrollbar-darkshadow-color: #BBBBBB;
	scrollbar-track-color: #EFEFEF;
	scrollbar-arrow-color: #626262;
	}

table {
	font-family: µ¸¿ò;
	font-size: 9pt;
	line-height: 14px;
	color: #555555;
	}
</style>	
<TITLE>Socket Terminal Monitoring</TITLE>
</head>
<body>
<p style="font-size:13pt;">
<b>[ Socket Terminal Monitoring ]</b>
</p>
<%
String eaiSvrInstNm = EAIServerManager.getInstance().getLocalServerName();
String terminalNo = request.getParameter("terminalNo");
boolean isSpUserFound = false;
String spUuid = null;
try {
	if(terminalNo == null) {
		terminalNo = "";
	}
	else {
		terminalNo = terminalNo.trim();
		if(terminalNo.length() > 0) isSpUserFound = true;
	}
	SessionManager	sessionManager = SessionManager.getInstance();
%>
<li>WAS Instance name : <%=eaiSvrInstNm %>
<li>Current DateTime : <%=DatetimeUtil.getTimeStampString() %>
<li>Search parameter : terminalNo
<form>
<input type="text" name="terminalNo" value="<%= terminalNo %>" size="20"><input type="submit" name="filter" value="filter"> 
</form>
<hr>
<p style="font-size:12pt;">
<b>Adapter Socket (Local)</b>
</p>
<table width="100%"  border=0 bgcolor='black' cellpadding="1" cellspacing="1">
<tr bgcolor="#ccccff">
	 <td>eaiSvrInstNm</td>
	 <td>adapterGroupName</td>
	 <td>terminalNo</td>
	 <td>localSocketAddress</td>
	 <td>remoteSocketAddress</td>
	 <td>createTime</td>
	 <td>idleTime</td>
	</tr>
<%
ConcurrentMap<String, ConcurrentHashMap<String, ISocketAdapter>> adapterGroups = SocketAdapterManager.getInstance().getSocketAdapterGroups();
	for (String adapterGroupName : adapterGroups.keySet()) {
		ConcurrentHashMap adapters = (ConcurrentHashMap) adapterGroups.get(adapterGroupName);
    	for (Enumeration ee = adapters.elements() ; ee.hasMoreElements() ;) {
	    	ISocketAdapter adapter = (ISocketAdapter) ee.nextElement();
	    	if (!"XC3EPB".equals(adapter.getContext().getProtocol())){
	    		break;
	    	}
	    	for (Iterator iii = adapter.getSessionPool().iterator() ; iii.hasNext(); ) {
		    	IoSession session1 = (IoSession) iii.next();
		    	TerminalVO vo = (TerminalVO)session1.getAttribute("TERMINAL_KEY");
%>

<%
				if(isSpUserFound && vo != null && vo.getTerminalNo().equals(terminalNo) ) {
%>
					<tr bgcolor='white'>
					 <td><%= eaiSvrInstNm %></td>
					 <td><%= adapterGroupName %></td>
					 <td><%= vo.getTerminalNo() %></td>
				     <td><%= session1.getLocalAddress() %></td>
				     <td><%= session1.getRemoteAddress() %></td>
				     <td><%= TimeUtil.formatDate(session1.getCreationTime()) %></td>
				     <td><%= TimeUtil.elapsedTime(session1.getLastIoTime()) %></td>
					</tr>
<% 
				} else {
		
%>
					<tr bgcolor='white'>
					 <td><%= eaiSvrInstNm %></td>
					 <td><%= adapterGroupName %></td>
					 <td><%= vo.getTerminalNo() %></td>
				     <td><%= session1.getLocalAddress() %></td>
				     <td><%= session1.getRemoteAddress() %></td>
				     <td><%= TimeUtil.formatDate(session1.getCreationTime()) %></td>
				     <td><%= TimeUtil.elapsedTime(session1.getLastIoTime()) %></td>
					</tr>
<% 
				}
			}		
		}
	}

	CacheInfoVO cacheInfo = sessionManager.getTerminalCache();
	String cacheName = cacheInfo.getName();
	String cacheSize = String.valueOf(cacheInfo.getSize());
	String cacheTime = String.valueOf(cacheInfo.getTimeToLiveSeconds());
%>
</table>
<hr>
<p style="font-size:12pt;">
<b>Terminal Cache Info (Distributed)</b>
</p>
CacheName: <%= cacheName %>, CacheSize: <%= cacheSize %>, Cache TTL: <%= cacheTime %>

<p style="font-size:11pt;">
<b>Shared Cache - Terminal Session Info</b>
</p>
<table width="100%" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
<tr bgcolor="#ccccff">
	 <td>key</td>
	 <td>terminalNo</td>
	 <td>RofcCd</td>
	 <td>Brncd</td>
	 <td>BrnName</td>
	</tr>
<%
	if(isSpUserFound) {
		TerminalVO svo = sessionManager.getTerminal(terminalNo);
		if(svo != null) {
	    	spUuid = svo.getTerminalNo();
%>
<tr bgcolor='white'>
  <td><%= terminalNo %></td>
  <td><%= svo.getTerminalNo() %></td>
  <td><%= svo.getRofcCd() %></td>
  <td><%= svo.getBrncd() %></td>
  <td><%= svo.getBrnName() %></td>
</tr>
<%
		}
	}
	else {
		for(String key : cacheInfo.getKeys()) {
		    TerminalVO svo = sessionManager.getTerminal(key);
%>
	<tr bgcolor='white'>
	  <td><%= key %></td>
	  <td><%= svo.getTerminalNo() %></td>
	  <td><%= svo.getRofcCd() %></td>
	  <td><%= svo.getBrncd() %></td>
	  <td><%= svo.getBrnName() %></td>
	</tr>
<% 
		}
	}
}
catch(Exception e) {
	e.printStackTrace();
	//e.printStackTrace(out);
}
%>
</table>
</body>
</HTML>
