package eu.musesproject.server.contextdatareceiver;

/*
 * #%L
 * MUSES Server
 * %%
 * Copyright (C) 2013 - 2014 S2 Grupo
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import eu.musesproject.contextmodel.ContextEvent;
import eu.musesproject.server.connectionmanager.ConnectionManager;
import eu.musesproject.server.connectionmanager.IConnectionCallbacks;
import eu.musesproject.server.connectionmanager.Statuses;


public class ConnectionCallbacksImpl implements IConnectionCallbacks, Runnable {
	
	private Logger logger = Logger.getLogger(ConnectionCallbacksImpl.class.getName());
	private ConnectionManager connManager;
	private boolean isRunning = false;
	boolean isDataAvailable = false;
	public static volatile String lastSessionId = null;
	private static volatile String data = "";
	public static volatile String receiveData;

	public ConnectionCallbacksImpl(){
		connManager = ConnectionManager.getInstance();
		connManager.registerReceiveCb(this);
		startConnection();
	}
	private void startConnection() {
				
		logger.info("Start Server Connection");
		if (!isRunning){
		   (new Thread(this)).start();
		}   
	}
	
	@Override
	public void run() {
		isRunning = true;
		logger.info("Connection Callbacks Implementation running");
			
		isDataAvailable = true;
		ConnectionCallbacksImpl.data = "Data available";
		if (ConnectionCallbacksImpl.lastSessionId != null){
			connManager.sendData(ConnectionCallbacksImpl.lastSessionId, ConnectionCallbacksImpl.data);
		}	
		logger.info("Data availability :" + isDataAvailable);
		try {
			Thread.sleep(10000);
		} catch (InterruptedException ex) {
			logger.log(Level.FATAL, null, ex);
		}
		isRunning = false;
		logger.info("Connection server stopped");
			

	}

	@Override
	public String receiveCb(String sessionId, String rData) {
		ConnectionCallbacksImpl.lastSessionId = sessionId;
		ConnectionCallbacksImpl.receiveData = rData;
		List<ContextEvent> list = JSONManager.processJSONMessage(ConnectionCallbacksImpl.receiveData);
		UserContextEventDataReceiver.getInstance().processContextEventList(list);
		
		logger.info("*************Receive Callback called: "
				+ ConnectionCallbacksImpl.receiveData + "from client ID " + sessionId);
		if (isDataAvailable) {
			connManager.sendData(sessionId, ConnectionCallbacksImpl.data);
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return ConnectionCallbacksImpl.receiveData;
	}
	@Override
	public void sessionCb(String sessionId, int status) {
		logger.info("*************Session Callback called Id: " + sessionId + " Status: " + ((status == Statuses.DISCONNECTED) ? "Disconnected" : "Data sent"));
		
	}

}

@@ -1230,18 +1230,8 @@
 acks
-, Runnable
  %7B%0A%09
@@ -1359,44 +1359,8 @@
 er;%0A
-%09private boolean isRunning = false;%0A
 %09boo
@@ -1762,660 +1762,14 @@
 %22);%0A
-%09%09if (!isRunning)%7B%0A%09%09   (new Thread(this)).start();%0A%09%09%7D   %0A%09%7D%0A%09%0A%09@Override%0A%09public void run() %7B%0A%09%09isRunning = true;%0A%09%09logger.info(%22Connection Callbacks Implementation running%22);%0A%09%09%09%0A%09%09isDataAvailable = true;%0A%09%09ConnectionCallbacksImpl.data = %22Data available%22;%0A%09%09if (ConnectionCallbacksImpl.lastSessionId != null)%7B%0A%09%09%09connManager.sendData(ConnectionCallbacksImpl.lastSessionId, ConnectionCallbacksImpl.data);%0A%09%09%7D%09%0A%09%09logger.info(%22Data availability :%22 + isDataAvailable);%0A%09%09try %7B%0A%09%09%09Thread.sleep(10000);%0A%09%09%7D catch (InterruptedException ex) %7B%0A%09%09%09logger.log(Level.FATAL, null, ex);%0A%09%09%7D%0A%09%09isRunning = false;%0A%09%09logger.info(%22Connection server stopped%22);%0A%09%09%09%0A%0A%09%7D
+ %0A%09%7D%0A%09
 %0A%0A%09@

Remove runnable implementation from ConnectionCallbacksImpl