View Javadoc

1   /*
2    * @(#)HandleMessagesTimeDelay.java Mar 3, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package nl.tudelft.simulation.actor.messagehandlers;
15  
16  import nl.tudelft.simulation.actor.ActorInterface;
17  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
18  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
19  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
20  import nl.tudelft.simulation.logger.Logger;
21  import nl.tudelft.simulation.messaging.Message;
22  import nl.tudelft.simulation.messaging.queues.MessageQueueInterface;
23  
24  /***
25   * The MessageHandlerTimeDelay takes messages one by one and only looks at the
26   * next message after a certain time delay. A busy flag indicates whether the
27   * message handler is already working, to avoid two separate threads of
28   * execution to be scheduled on the same handler at the same time (effectively
29   * doubling its capacity). <br>
30   * <br>
31   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
32   * Delft, the Netherlands. All rights reserved.
33   * 
34   * See for project information <a href="http://www.simulation.tudelft.nl/">
35   * www.simulation.tudelft.nl </a>.
36   * 
37   * The source code and binary code of this software is proprietary information
38   * of Delft University of Technology.
39   * 
40   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
41   *         Jacobs </a>, <a
42   *         href="mailto:s.p.a.vanhouten@tbm.tudelft.nl">Stijn-Pieter van Houten
43   *         </a>, <a
44   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
45   *         Verbraeck </a>
46   * @version $$Revision: 1.3 $$ $$Date: 2005/04/08 11:29:13 $$
47   */
48  public class HandleMessagesTimeDelay implements MessageHandlerInterface
49  {
50  	/*** the serial version uid */
51  	private static final long serialVersionUID = 12L;
52  
53  	/*** the owner of this handler */
54  	private ActorInterface owner;
55  
56  	/*** the simulator to schedule on */
57  	private DEVSSimulatorInterface simulator;
58  
59  	/*** the time distribution for handling the message */
60  	private DistContinuous handlingTime;
61  
62  	/*** a flag to indicate that the message handler is already busy */
63  	private boolean busy;
64  
65  	/***
66  	 * Create a Messagehandler that takes a stochastic time to handle messages,
67  	 * and that is only able to handle messages one by one.
68  	 * 
69  	 * @param owner the owner (actor) of this handler
70  	 * @param simulator to schedule on
71  	 * @param handlingTime distribution for handling the message
72  	 */
73  	public HandleMessagesTimeDelay(final ActorInterface owner,
74  			final DEVSSimulatorInterface simulator,
75  			final DistContinuous handlingTime)
76  	{
77  		super();
78  		this.owner = owner;
79  		this.simulator = simulator;
80  		this.handlingTime = handlingTime;
81  	}
82  
83  	/***
84  	 * @see nl.tudelft.simulation.actor.messagehandlers.MessageHandlerInterface
85  	 *      #handleMessageQueue(nl.tudelft.simulation.messaging.queues.MessageQueueInterface)
86  	 */
87  	public synchronized void handleMessageQueue(
88  			final MessageQueueInterface messageQueue)
89  	{
90  		if (this.busy)
91  		{
92  			return;
93  		}
94  		this.busy = true;
95  		check(messageQueue);
96  	}
97  
98  	/***
99  	 * @param messageQueue the quese with messages
100 	 */
101 	protected synchronized void check(final MessageQueueInterface messageQueue)
102 	{
103 		if (messageQueue.isEmpty())
104 		{
105 			this.busy = false;
106 			return;
107 		}
108 		// handle one message now. Schedule the next handling. If the handler
109 		// method
110 		// returns false, leave message in the queue, and stop handling
111 		Message message = messageQueue.first();
112 		boolean success = this.owner.handleContent(message);
113 		if (success)
114 		{
115 			messageQueue.remove(message);
116 			try
117 			{
118 				this.simulator.scheduleEvent(new SimEvent(this.simulator
119 						.getSimulatorTime()
120 						+ this.handlingTime.draw(), this, this, "check",
121 						new Object[]{messageQueue}));
122 				return;
123 			} catch (Exception e)
124 			{
125 				Logger.warning(this, "check", e);
126 			}
127 		}
128 		this.busy = false;
129 	}
130 }