View Javadoc

1   /*
2    * @(#)Duplicate.java Feb 1, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.formalisms.flow;
11  
12  import java.io.Serializable;
13  import java.rmi.MarshalledObject;
14  import java.rmi.RemoteException;
15  
16  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
17  import nl.tudelft.simulation.logger.Logger;
18  
19  /***
20   * A duplicate station duplicates incoming objects and sends them to their
21   * alternative destination. <br>
22   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
23   * University of Technology </a>, the Netherlands. <br>
24   * See for project information <a
25   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
26   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
27   * License (GPL) </a>, no warranty <br>
28   * 
29   * @version 2.0 21.09.2003 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
31   *         Jacobs </a>, <a
32   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
33   *         Verbraeck </a>
34   */
35  public class Duplicate extends Station
36  {
37  	/*** duplicateDestination which is the duplicate definition */
38  	private StationInterface duplicateDestination;
39  
40  	/*** numberCopies refers to the number of duplicates */
41  	private int numberCopies;
42  
43  	/***
44  	 * Method Duplicate. Creates a new Duplicate
45  	 * 
46  	 * @param simulator on which is scheduled
47  	 * @param duplicateDestination the duplicate destination
48  	 */
49  	public Duplicate(final DEVSSimulatorInterface simulator,
50  			final StationInterface duplicateDestination)
51  	{
52  		this(simulator, duplicateDestination, 1);
53  	}
54  
55  	/***
56  	 * Method Duplicate.
57  	 * 
58  	 * @param simulator on which is scheduled
59  	 * @param duplicateDestination which is the duplicate definition
60  	 * @param numberCopies the number of copies
61  	 */
62  	public Duplicate(final DEVSSimulatorInterface simulator,
63  			final StationInterface duplicateDestination, final int numberCopies)
64  	{
65  		super(simulator);
66  		this.duplicateDestination = duplicateDestination;
67  		this.numberCopies = numberCopies;
68  	}
69  
70  	/***
71  	 * @see StationInterface#receiveObject(Object)
72  	 */
73  	public synchronized void receiveObject(final Object object)
74  			throws RemoteException
75  	{
76  		super.receiveObject(object);
77  		try
78  		{
79  			this.releaseObject(object);
80  			if (object instanceof Serializable)
81  			{
82  				for (int i = 0; i < this.numberCopies; i++)
83  				{
84  					Object clone = new MarshalledObject(object).get();
85  					this.fireEvent(StationInterface.RELEASE_EVENT, 1);
86  					this.duplicateDestination.receiveObject(clone);
87  				}
88  			} else
89  			{
90  				throw new Exception("cannot duplicate object: "
91  						+ object.getClass()
92  						+ " does not implement java.io.Serializable");
93  			}
94  		} catch (Exception exception)
95  		{
96  			Logger.warning(this, "receiveMethod", exception);
97  		}
98  	}
99  }