<%@ page import="java.io.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="com.eactive.eai.adapter.websocket.WebSocketSessionManager,org.java_websocket.WebSocket"%>
<%@ page import="java.util.concurrent.ConcurrentHashMap"%>
<%@ page import="com.eactive.eai.common.session.SessionManager,com.eactive.eai.common.session.SessionVO"%>
<%@ page import="com.eactive.eai.common.session.CacheInfoVO"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.mina.common.IoSession"%>
<%@ 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"%>
<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>WebSocket Login Monitoring</TITLE>
</head>
<body>
<p style="font-size:13pt;">
<b>[ WebSocket Login Monitoring ]</b>
</p>
<%
String eaiSvrInstNm = EAIServerManager.getInstance().getLocalServerName();
String loginUserId = request.getParameter("loginUserId");
boolean isSpUserFound = false;
String spUuid = null;
try {
	if(loginUserId == null) {
		loginUserId = "";
	}
	else {
		loginUserId = loginUserId.trim();
		if(loginUserId.length() > 0) isSpUserFound = true;
	}
	SessionManager	sessionManager = SessionManager.getInstance();
	CacheInfoVO mcache = sessionManager.getEvictMasterCache();
%>
<li>WAS Instance name : <%=eaiSvrInstNm %>
<li>Current DateTime : <%=DatetimeUtil.getTimeStampString() %>
<li>Search parameter : loginUserId
<form>
<input type="text" name="loginUserId" value="<%= loginUserId %>" size="20"><input type="submit" name="filter" value="filter"> 
</form>
<hr>
<p style="font-size:12pt;">
<b>Adapter WebSocket (Local)</b>
</p>
<table width="100%"  border=0 bgcolor='black' cellpadding="1" cellspacing="1">
<%
	WebSocketSessionManager wsSessionManager = WebSocketSessionManager.getInstance();
	HashMap<String, ConcurrentHashMap<String, WebSocket>> adapterGroups = wsSessionManager.getAdapterGroupConnections();
	Iterator i = adapterGroups.keySet().iterator();
	while (i.hasNext()) {
		String key = (String) i.next();
		ConcurrentHashMap<String, WebSocket> conMap = adapterGroups.get(key);
%>
<tr bgcolor="#ccccff">
	 <td>adapterGroupName</td>
	 <td>loginUserId</td>
	 <td>localSocketAddress</td>
	 <td>RemoteSocketAddress</td>
	 <td>socketStatus</td>
	</tr>
<%
		if(isSpUserFound) {
			WebSocket s = (WebSocket)conMap.get(loginUserId);
			if(s!= null) { 
%>
			<tr bgcolor='white'>
			 <td><%= key %></td>
			 <td><%= loginUserId %></td>
			 <td><%= s.getLocalSocketAddress() %></td>
		     <td><%= s.getRemoteSocketAddress() %></td>
		     <td><%= s.getReadyState().name() %></td>
			</tr>
<% 
			}
		}
		else {
			Iterator ci = conMap.keySet().iterator();
			while (ci.hasNext()) {
				String ckey = (String) ci.next();
				WebSocket s = (WebSocket)conMap.get(ckey);
			
%>
	<tr bgcolor='white'>
	 <td><%= key %></td>
	 <td><%= ckey %></td>
	 <td><%= s.getLocalSocketAddress() %></td>
     <td><%= s.getRemoteSocketAddress() %></td>
     <td><%= s.getReadyState().name() %></td>
	</tr>
<% 
			}
		}
	}

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

<p style="font-size:11pt;">
<b>Shared Cache - Login Session Info</b>
</p>
<table width="100%" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
<!-- <tr bgcolor="#ccccff"> -->
<!-- 	 <td>key</td> -->
<!-- 	 <td colspan="5">SessionVO</td> -->
<!-- 	 <td colspan="3">Cache Info</td> -->
<!-- </tr> -->
<tr bgcolor="#ccccff">
	 <td>key</td>
	 <td>loginUserId</td>
	 <td>loginInstId</td>
	 <td>wsInstId</td>
	 <td>wsAdapterGroupName</td>
	 <td>uuid</td>
	 <td>brncd</td>
	 <td>terminalId</td>
	</tr>
<%
	if(isSpUserFound) {
		SessionVO svo = null;
		for(String uuid : sessionManager.getHttpLoginCache().getKeys()) {
			SessionVO httpLogin = sessionManager.getHttpLogin(uuid);
			if( loginUserId.equals(httpLogin.getLoginUserId()) ) {
				svo = httpLogin;
				spUuid = httpLogin.getUuid();
				break;
			}
		}
		
		if(svo != null) {
	    	spUuid = svo.getUuid();
%>
<tr bgcolor='white'>
  <td><%= loginUserId %></td>
  <td><%= svo.getLoginUserId() %></td>
  <td><%= svo.getLoginInstId() %></td>
  <td><%= svo.getWsInstId() %></td>
  <td><%= svo.getWsAdapterGroupName() %></td>
  <td><%= svo.getUuid() %></td>
  <td><%= svo.getBrncd() %></td>
  <td><%= svo.getTerminalId() %></td>
</tr>
<%
		}
	}
	else {
		for(String key : cacheInfo.getKeys()) {
		    SessionVO svo = sessionManager.getHttpLogin(key);
%>
	<tr bgcolor='white'>
	  <td><%= key %></td>
	  <td><%= svo.getLoginUserId() %></td>
	  <td><%= svo.getLoginInstId() %></td>
	  <td><%= svo.getWsInstId() %></td>
	  <td><%= svo.getWsAdapterGroupName() %></td>
	  <td><%= svo.getUuid() %></td>
	  <td><%= svo.getBrncd() %></td>
	  <td><%= svo.getTerminalId() %></td>	  
	</tr>
<% 
		}
	}
%>
</table>
<hr>
<%
	CacheInfoVO hcache = sessionManager.getHttpLoginCache();
	cacheName = hcache.getName();
	cacheSize = String.valueOf(hcache.getSize());
	cacheTime = String.valueOf(hcache.getTimeToLiveSeconds());
%>
<p style="font-size:12pt;">
<b>HTTP Login Cache Info (Distributed)</b>
</p>
CacheName: <%= cacheName %>, CacheSize: <%= cacheSize %>, Cache TTL: <%= cacheTime %>

<p style="font-size:11pt;">
<b>HTTP Login Cache - Session Info</b>
</p>
<table width="100%" border=0 bgcolor='black' cellpadding="1" cellspacing="1">
<tr bgcolor="#ccccff">
	 <td>key</td>
	 <td>loginUserId</td>
	 <td>loginInstId</td>
	 <td>wsInstId</td>
	 <td>wsAdapterGroupName</td>
	 <td>uuid</td>
	 <td>brncd</td>
	 <td>terminalId</td>
	 </tr>
<%
	int rowCount = 0;
	if(isSpUserFound) {
		SessionVO svo = sessionManager.getHttpLogin(spUuid);
		if(svo != null) {
%>
<tr bgcolor="white">
	<td><%= spUuid %></td>
	<td><%= svo.getLoginUserId() %></td>
	<td><%= svo.getLoginInstId() %></td>
	<td><%= svo.getWsInstId() %></td>
	<td><%= svo.getWsAdapterGroupName() %></td>
	<td><%= svo.getUuid() %></td>
	<td><%= svo.getBrncd() %></td>
	<td><%= svo.getTerminalId() %></td>
</tr>
<%
		}
	}
	else {
		for(String key : hcache.getKeys()) {
			SessionVO svo = sessionManager.getHttpLogin(key);
		    if (svo == null){
		    	continue;
		    }
%>
	<tr bgcolor="white">
		<td><%= key %></td>
		<td><%= svo.getLoginUserId() %></td>
		<td><%= svo.getLoginInstId() %></td>
		<td><%= svo.getWsInstId() %></td>
		<td><%= svo.getWsAdapterGroupName() %></td>
		<td><%= svo.getUuid() %></td>
		<td><%= svo.getBrncd() %></td>
		<td><%= svo.getTerminalId() %></td>
	</tr>
<% 
		}
	}
%>
</table>
<hr>
<%	
}
catch(Exception e) {
	e.printStackTrace();
	//e.printStackTrace(out);
}
%>
</table>
</body>
</HTML>
