KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > utobcast > basic > ProducerConsumerImpl


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): Vivien Quema
22  * Contributor(s):
23  */

24
25 package utobcast.basic;
26
27 import java.util.Collections JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.dream.AbstractComponent;
31 import org.objectweb.dream.Push;
32 import org.objectweb.dream.PushException;
33 import org.objectweb.dream.control.activity.Util;
34 import org.objectweb.dream.control.activity.task.AbstractTask;
35 import org.objectweb.dream.control.activity.task.Task;
36 import org.objectweb.dream.message.Message;
37 import org.objectweb.dream.message.MessageType;
38 import org.objectweb.dream.message.MessageTypeImpl;
39 import org.objectweb.dream.message.manager.MessageManager;
40 import org.objectweb.fractal.api.Component;
41 import org.objectweb.fractal.api.NoSuchInterfaceException;
42 import org.objectweb.fractal.api.control.IllegalBindingException;
43 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * Implementation of the ConsumerProducer component.
48  */

49 public class ProducerConsumerImpl extends AbstractComponent implements Push
50 {
51
52   Push outPushItf;
53   MessageManager messageManagerItf;
54   MessageType msgType = new MessageTypeImpl(TestChunk.DEFAULT_NAME,
55                              TestChunk.TYPE);
56
57   Task tasks;
58
59   // -------------------------------------------------------------------------
60
// Overriden methods
61
// -------------------------------------------------------------------------
62

63   /**
64    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
65    */

66   protected void beforeFirstStart(Component componentItf)
67       throws IllegalLifeCycleException
68   {
69     try
70     {
71       Util.addTask(componentItf, new ProducerTask("Producer task"),
72           Collections.EMPTY_MAP);
73       logger.log(BasicLevel.DEBUG, "task added");
74     }
75     catch (Exception JavaDoc e)
76     {
77       throw new IllegalLifeCycleException("Can't add task");
78     }
79   }
80
81   // ---------------------------------------------------------------------------
82
// Implementation of the Push interface
83
// ---------------------------------------------------------------------------
84

85   /**
86    * @see org.objectweb.dream.Push#push(org.objectweb.dream.message.Message,
87    * java.util.Map)
88    */

89   public void push(Message message, Map JavaDoc context) throws PushException
90   {
91     logger.log(BasicLevel.INFO, "Received message " + message);
92     messageManagerItf.deleteMessage(message);
93   }
94
95   // ---------------------------------------------------------------------------
96
// Implementation of the BindingController interface
97
// ---------------------------------------------------------------------------
98

99   /**
100    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
101    * Object)
102    */

103   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
104       throws NoSuchInterfaceException, IllegalBindingException,
105       IllegalLifeCycleException
106   {
107     super.bindFc(clientItfName, serverItf);
108     if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME))
109     {
110       outPushItf = (Push) serverItf;
111     }
112     else if (clientItfName.equals(MessageManager.ITF_NAME))
113     {
114       messageManagerItf = (MessageManager) serverItf;
115     }
116   }
117
118   /**
119    * @see org.objectweb.fractal.api.control.BindingController#listFc()
120    */

121   public String JavaDoc[] listFc()
122   {
123     return new String JavaDoc[]{Push.OUT_PUSH_ITF_NAME, MessageManager.ITF_NAME};
124   }
125
126   private class ProducerTask extends AbstractTask
127   {
128
129     private String JavaDoc message = "Producer";
130
131     /**
132      * Constructor
133      */

134     public ProducerTask(String JavaDoc name)
135     {
136       super(name);
137     }
138
139     /**
140      * @see Task#execute(Object)
141      */

142     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
143     {
144       try
145       {
146         Message msg = messageManagerItf.createMessage(msgType);
147         TestChunk chunk = (TestChunk) msg.getChunk(TestChunk.DEFAULT_NAME);
148         chunk.setMessage(message);
149         outPushItf.push(msg, null);
150       }
151       catch (Exception JavaDoc e)
152       {
153         e.printStackTrace();
154         return STOP_EXECUTING;
155       }
156       Thread.sleep(1000);
157       return EXECUTE_AGAIN;
158     }
159   }
160
161 }
Popular Tags