/*
 * ALMA - Atacama Large Millimiter Array (c) European Southern Observatory, 2007
 * 
 * This library 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.1 of the License, or (at your option)
 * any later version.
 * 
 * This library 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 library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 */
package alma.alarmsystem.clients;

import java.util.Collection;
import java.util.HashSet;
import java.util.logging.Logger;

import alma.acs.component.client.AdvancedComponentClient;
import alma.acs.container.ContainerServices;
import alma.acs.nc.Consumer;
import cern.cmw.mom.pubsub.impl.ACSJMSTopicConnectionImpl;
import cern.laser.source.alarmsysteminterface.FaultState;
import cern.laser.source.alarmsysteminterface.impl.ASIMessageHelper;
import cern.laser.source.alarmsysteminterface.impl.FaultStateImpl;
import cern.laser.source.alarmsysteminterface.impl.XMLMessageHelper;
import cern.laser.source.alarmsysteminterface.impl.message.ASIMessage;

import alma.alarmsystem.clients.source.SourceListener;

public class SourceClient { 
	
	// The listeners of alarms
	private HashSet<SourceListener> listeners = new HashSet<SourceListener>();
	
	private Consumer m_consumer = null;
	private String m_channelName = "CMW.ALARM_SYSTEM.ALARMS.SOURCES.ALARM_SYSTEM_SOURCES";
	
	// Container services
	private ContainerServices contSvcs;
	
	// Logger
	private Logger logger;

	public SourceClient(ContainerServices svc) throws Exception {
		if (svc==null) {
			throw new IllegalArgumentException("Invalid ContainerServices");
		}
		contSvcs=svc;
		logger=contSvcs.getLogger();
		try {
			initialize();
		} catch (Throwable t) {
			throw new Exception("Error building CategoryClient",t);
		}
	}
	
	/**
	 * initialize the client
	 * 
	 * @throws Exception
	 */
	public void initialize() throws Exception {
		m_consumer = new Consumer(m_channelName,contSvcs);
		m_consumer.addSubscription(com.cosylab.acs.jms.ACSJMSMessageEntity.class,this);
		m_consumer.consumerReady();
	}
	
	public void tearDown() throws Exception {
		m_consumer.disconnect();
		m_consumer=null;
	}
	
	/**
	 * The method receives all the messages published in the NC
	 * For each message received it check if its content is right
	 * i.e. the name of the class, the member and the code contains the 
	 * number of the message in the sequence. In this way it also checks
	 * if the messages are received in the same order they were sent.
	 * The method also checks if all the messages have been received
	 * and prints a message if receives more messages then the messages 
	 * pushed
	 *  
	 * @param msg The message received from the NC
	 * @see alma.acs.nc.Consumer
	 */
	public void receive(com.cosylab.acs.jms.ACSJMSMessageEntity msg) {
		ASIMessage asiMsg;
		Collection<FaultState> faultStates;
		try {
			asiMsg = XMLMessageHelper.unmarshal(msg.text);
			faultStates = ASIMessageHelper.unmarshal(asiMsg);
		} catch (Exception e) {
			System.out.println("Exception caught while unmarshalling the msg "+e.getMessage());
			e.printStackTrace();
			return;
		}
		for (FaultState fs: faultStates) {
			dispatchFaultState(fs);
		}
	}
	
	/**
	 * Add a listener for the alarms.
	 * 
	 * Add the listeners to the set of listeners to be notified when
	 * a new alarms is received from the categories.
	 * 
	 * @param newListener The listener for alarms from categories
	 */
	public void addAlarmListener(SourceListener newListener) {
		if (newListener==null) {
			throw new IllegalArgumentException("Invalid null listener");
		}
		synchronized(listeners) {
			listeners.add(newListener);
		}
	}
	
	/**
	 * Remove a listener from the list of listeners to be notified
	 * when a new alarm is received 
	 * 
	 * @param listener The not null listener to remove
	 * @return true if the list of listeners contained the specified listener
	 */
	public boolean removeListener(SourceListener listener) {
		if (listener==null) {
			throw new IllegalArgumentException("Invalid null listener");
		}
		boolean ret;
		synchronized(listeners) {
			ret=listeners.remove(listener);
		}
		return ret;
	}
	
	/**
	 * This method is called by categories when a new message arrives and dispatches
	 * the alarm to the listeners.
	 * 
	 * @param newAlarm The alarm to send to the listeners
	 */
	private synchronized void dispatchFaultState(FaultState faultState) {
		if (faultState==null) {
			throw new IllegalArgumentException("Invalid null alarm to dispatch");
		}
		synchronized(listeners) {
			for (SourceListener listener: listeners) {
				listener.faultStateReceived(faultState);
			}
		}
	}
}

@@ -1051,16 +1051,53 @@
 rvices;%0A
+import alma.acs.logging.AcsLogLevel;%0A
 import a
@@ -2135,156 +2135,46 @@
 );%0A%09
-%09try %7B%0A%09%09%09initialize();%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09throw new Exception(%22Error building CategoryClient%22,t);%0A%09%09%7D%0A%09%7D%0A%09%0A%09/**%0A%09 * initialize the client
+%7D%0A%09%0A%09/**%0A%09 * Connect to the sources NC
 %0A%09 *
@@ -2219,18 +2219,15 @@
 oid 
-initialize
+connect
 () t
@@ -2236,32 +2236,111 @@
 ows Exception %7B%0A
+%09%09logger.log(AcsLogLevel.DEBUG,%22Connecting to source channel %22+m_channelName);%0A
 %09%09m_consumer = n
@@ -2493,99 +2493,83 @@
 );%0A%09
-%7D%0A%09%0A%09public void tearDown() throws Exception %7B%0A%09%09m_consumer.disconnect();%0A%09%09m_consumer=null
+%09logger.log(AcsLogLevel.DEBUG,%22Source channel %22+m_channelName+%22 connected%22)
 ;%0A%09%7D

connect method added. More log messages