/* This file is part of Reflex
 * Copyright (C) 2010 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.eso;

import java.util.List;

import ptolemy.actor.IOPort;
import ptolemy.actor.TypedAtomicActor;
import ptolemy.actor.TypedIOPort;
import ptolemy.data.BooleanToken;
import ptolemy.data.IntToken;
import ptolemy.data.StringToken;
import ptolemy.data.expr.Parameter;
import ptolemy.data.type.BaseType;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.NameDuplicationException;
import ptolemy.kernel.util.Settable;

/**
 * The FitsRouter routes the files present on the input port to the output ports
 * based on file category. There are two modes:
 * 
 * Simple mode: create an output port named against a file type (e.g. FLAT), all files 
 * of that type will be routed there
 * 
 * Advanced mode: create an output port named as you like (e.g. MyPort) and a StringParameter
 * named MyPort_config which contains a comma separated list of regular expressions. All files 
 * matching at least one of the regexps will be routed to that port.
 * 
 * You can limit the number of files broadcasted from a port defining the attributes '
 * MyPort_min_number and MyPort_max_number.
 * 
 * Note: a file can be routed to many ports
 *  
 * @author Vincenzo Forchi` - vforchi@eso.org
 */
public class FitsRouter extends TypedAtomicActor {
	
	private static final long serialVersionUID = -880546985796686687L;

	/* INPUT */
	/* the input port containing the data sets to be processed */
	private TypedIOPort d_loso = new TypedIOPort(this, "loso", true, false);
	private Parameter d_loso_tokenConsumptionRate;
	
	/* OUTPUT */
	/* the output port containing the rejected data sets */
	private TypedIOPort d_rejected = new TypedIOPort(this, "rejected", false, true);
	
	/* the output ports where the file should be routed */
	List<IOPort> ports;

	public FitsRouter(CompositeEntity container, String name)
			throws IllegalActionException, NameDuplicationException {
		super(container, name);
		
		//d_loso.setMultiport(true);
		d_loso.setTypeEquals(BaseType.STRING);
		
		d_rejected.setMultiport(true);
		d_rejected.setTypeEquals(BaseType.STRING);
		d_rejected.setDefaultWidth(1);
		
		/* show port names */
		new Parameter(d_loso, "_showName", new BooleanToken(true));
		new Parameter(d_rejected, "_showName", new BooleanToken(true));
		
		/* port parameter */
		d_loso_tokenConsumptionRate = new Parameter(d_loso, "tokenConsumptionRate");
		d_loso_tokenConsumptionRate.setVisibility(Settable.NOT_EDITABLE);
		d_loso_tokenConsumptionRate.setTypeEquals(BaseType.INT);
		d_loso_tokenConsumptionRate.setToken(new IntToken(1));
	}
	
	@Override
	public void fire() throws IllegalActionException {
		/* get input data set */
		try {
			/* get the token and convert it to a string */
			if (d_loso.hasToken(0)) {
				String xml = ((StringToken) d_loso.get(0)).stringValue();
				d_rejected.broadcast(new StringToken(xml));
			} else {
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalActionException(e.getMessage());
		}
	}

}