<?xml version="1.0" encoding="euc-kr"?>
<%@ page contentType="text/xml; charset=utf-8"%>
<%@ page import="java.util.*,com.eactive.eai.common.util.Logger" %>
<%@ page import="java.io.*"%>
<%@ 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="java.util.*"%>
<%@ page import="org.apache.mina.common.IoSession"%>
<%@ page import="java.util.concurrent.ConcurrentHashMap"%>
<%@ 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.common.server.EAIServerManager"%>
<Dataset>
    <Columns>
          <Column Name="eaiSvrInstNm" Type="string" Size="50" />
          <Column Name="adapterGroupName" Type="string" Size="50" />
          <Column Name="loginId" Type="string" Size="50" />
          <Column Name="localSocketAddress" Type="string" Size="50" />
          <Column Name="remoteSocketAddress" Type="string" Size="50" />
          <Column Name="socketStatus" Type="string" Size="20" />
    </Columns>
    <Rows>
<%
	String eaiSvrInstNm = EAIServerManager.getInstance().getLocalServerName();
    String loginUserId = request.getParameter("loginUserId");
    boolean isSpUserFound = false;   
    String spUuid = null;
	int rowCount = 0;
	try {
    	if(loginUserId == null) {
    		loginUserId = "";
    	}
    	else {
    		SessionManager	sessionManager = SessionManager.getInstance();
    	  	loginUserId = loginUserId.trim();
    		if(loginUserId.length() > 0) {
    			isSpUserFound = true;
    			for(String uuid : sessionManager.getHttpLoginCache().getKeys()) {
    				SessionVO httpLogin = sessionManager.getHttpLogin(uuid);
    				if( loginUserId.equals(httpLogin.getLoginUserId()) ) {    					
        				spUuid = httpLogin.getUuid();
        				break;
    				}
    			}
    		}
    	}

		WebSocketSessionManager wsSessionManager = WebSocketSessionManager.getInstance();
		HashMap<String, ConcurrentHashMap<String, WebSocket>> adapterGroups = wsSessionManager.getAdapterGroupConnections();
		System.out.println("adapterGroups. " + adapterGroups);
		Iterator i = adapterGroups.keySet().iterator();
		while (i.hasNext()) {
			String adapterGroupName = (String) i.next();
			ConcurrentHashMap<String, WebSocket> conMap = adapterGroups.get(adapterGroupName);
			Iterator ci = conMap.keySet().iterator();

			if(isSpUserFound) {
				WebSocket s = (WebSocket)conMap.get(loginUserId);
				if(s!= null) {
					rowCount++;
	%>
<Row>
  <Data><%= eaiSvrInstNm %></Data>
  <Data><%= adapterGroupName %></Data>
  <Data><%= loginUserId %></Data>
  <Data><%= s.getLocalSocketAddress() %></Data>
  <Data><%= s.getRemoteSocketAddress() %></Data>
  <Data><%= s.getReadyState().name() %></Data>
</Row>	
	<%
				}
			}
			else {
				while (ci.hasNext()) {
					String ckey = (String) ci.next();
					//System.out.println(ckey + "=" + conMap.get(ckey));
					WebSocket s = (WebSocket)conMap.get(ckey);
				
%>
<Row>
  <Data><%= eaiSvrInstNm %></Data>
  <Data><%= adapterGroupName %></Data>
  <Data><%= ckey %></Data>
  <Data><%= s.getLocalSocketAddress() %></Data>
  <Data><%= s.getRemoteSocketAddress() %></Data>
  <Data><%= s.getReadyState().name() %></Data>
</Row>
<% 
				rowCount++;
			}
		}
		if(rowCount == 0) {
%>
					<Row>
					  <Data><%= eaiSvrInstNm %></Data>
					  <Data></Data>
					  <Data></Data>
					  <Data></Data>
					  <Data></Data>
					</Row>
			<% 
		}
	}
}
catch(Exception e) {
	e.printStackTrace();
	//e.printStackTrace(out);
}	
%>
    </Rows>
</Dataset>