/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2009 The eXist Project
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id:$
 */
package org.exist.debuggee.dgbp;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.exist.debuggee.DebuggeeJoint;
import org.exist.debuggee.dgbp.packets.Command;
import org.exist.debuggee.dgbp.packets.Init;
import org.exist.debuggee.dgbp.packets.Stop;
import org.exist.security.xacml.XACMLSource;

/**
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 *
 */
public class DGBPProtocolHandler extends IoHandlerAdapter {

	private DebuggeeJoint joint;
	private XACMLSource source;
	
	public DGBPProtocolHandler(DebuggeeJoint joint, XACMLSource source) {
		super();
		
		this.joint = joint;
		this.source = source;
	}

	@Override
	public void sessionOpened(IoSession session) {
		// Set reader idle time to 30 seconds.
		// sessionIdle(...) method will be invoked when no data is read
		// for 30 seconds.
		//XXX: fix -> 30 ???
		//session.getConfig().setIdleTime(IdleStatus.READER_IDLE, 3000);
		 
		session.setAttribute("joint", joint);
		
		session.write(new Init(source));
	}

	@Override
	public void sessionClosed(IoSession session) {
		DebuggeeJoint joint = (DebuggeeJoint) session.getAttribute("joint");
		joint.continuation(new Stop(session, ""));

		// Print out total number of bytes read from the remote peer.
		System.err.println("Total " + session.getReadBytes() + " byte(s)");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) {
//		// Close the connection if reader is idle.
//		if (status == IdleStatus.READER_IDLE) {
//			session.close(true);
//		}
	}

	@Override
	public void messageReceived(IoSession session, Object message) {
		Command command = (Command) message;
		
//		command.exec();
		
		session.write(command);
	}
	
	public void exceptionCaught(IoSession session, Throwable cause) {
		System.out.println(cause);
	}
}

@@ -1693,25 +1693,25 @@
 time to 
-30 second
+10 minute
 s.%0A%09%09// 
@@ -1784,47 +1784,22 @@
 for 
-30 seconds.%0A%09%09//XXX: fix -%3E 30 ???%0A%09%09//
+10 minutes.%0A%09%09
 sess
@@ -1850,17 +1850,27 @@
 R_IDLE, 
-3
+10 * 60 * 1
 000);%0A%09%09
@@ -2343,18 +2343,16 @@
 atus) %7B%0A
-//
 %09%09// Clo
@@ -2388,18 +2388,16 @@
 s idle.%0A
-//
 %09%09if (st
@@ -2430,18 +2430,16 @@
 IDLE) %7B%0A
-//
 %09%09%09sessi
@@ -2454,18 +2454,16 @@
 (true);%0A
-//
 %09%09%7D%0A%09%7D%0A%0A

set idle timeout for 10 minutes for debugging session