KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > pump > PumpPeriodicSynchronizedImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.pump;
26
27 import org.objectweb.dream.PullException;
28 import org.objectweb.dream.PushException;
29 import org.objectweb.dream.message.Message;
30
31 /**
32  * Extention of the basic pump that use a period <code>T</code> (in ms). More
33  * precisely, every T ms, a message is pulled on the input and is then pushed on
34  * the output. If these two actions (pull then push) are longer than T ms, an
35  * other message is pulled immediately.
36  */

37 public class PumpPeriodicSynchronizedImpl extends PumpSynchronizedImpl
38     implements
39       PumpPeriodicAttributeController
40 {
41
42   /**
43    * The initialization parameter that can be passed to this component to
44    * specify the pump's period.
45    */

46   public static final String JavaDoc PERIOD_INIT_PARAM_NAME = "period";
47
48   protected long period = -1;
49
50   /**
51    * Default constructor
52    */

53   public PumpPeriodicSynchronizedImpl()
54   {
55     pumpTask = new PeriodicPumpTask();
56   }
57
58   class PeriodicPumpTask extends PumpTask
59   {
60
61     /**
62      * @see org.objectweb.dream.control.activity.task.Task#execute(Object)
63      */

64     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
65     {
66
67       long begin = System.currentTimeMillis();
68       if (mutexItf != null)
69       {
70         mutexItf.lock();
71       }
72       try
73       {
74         Message msg = inPullItf.pull(null);
75         if (msg != null || pushNullPolicy)
76         {
77           outPushItf.push(msg, null);
78         }
79       }
80       catch (PullException e)
81       {
82         throw new InterruptedException JavaDoc(e.getLocalizedMessage());
83       }
84       catch (PushException e)
85       {
86         throw new InterruptedException JavaDoc(e.getLocalizedMessage());
87       }
88       finally
89       {
90         if (mutexItf != null)
91         {
92           mutexItf.unlock();
93         }
94       }
95
96       long sleepTime = period - (System.currentTimeMillis() - begin);
97       if (sleepTime > 0)
98       {
99         Thread.sleep(sleepTime);
100       }
101       return EXECUTE_AGAIN;
102     }
103   }
104
105   /**
106    * @see org.objectweb.dream.pump.PumpPeriodicAttributeController#getPeriod()
107    */

108   public long getPeriod()
109   {
110     return period;
111   }
112
113   /**
114    * @see org.objectweb.dream.pump.PumpPeriodicAttributeController#setPeriod(long)
115    */

116   public void setPeriod(long period)
117   {
118     if (period < 0)
119     {
120       throw new IllegalArgumentException JavaDoc("Period value negative");
121     }
122     this.period = period;
123   }
124 }
Popular Tags