KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > recording > ThreadPlayer


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.gui.recording;
32
33 import org.objectweb.proactive.ic2d.spy.SpyEvent;
34 import org.objectweb.proactive.ic2d.data.ActiveObject;
35 import org.objectweb.proactive.ic2d.event.CommunicationEventListener;
36
37 public class ThreadPlayer {
38
39   private java.util.LinkedList JavaDoc eventList;
40   private java.util.LinkedList JavaDoc activeObjectList;
41   private int recordMark = 0;
42   private CommunicationEventListener communicationEventListener;
43
44   private boolean record = false;
45   private boolean pause = false;
46   private boolean play = false;
47   private Thread JavaDoc t;
48   private javax.swing.JProgressBar JavaDoc eventReplayProgressBar;
49
50   public ThreadPlayer(CommunicationEventListener communicationEventListener, javax.swing.JProgressBar JavaDoc eventReplayProgressBar) {
51     this.communicationEventListener = communicationEventListener;
52     this.eventReplayProgressBar = eventReplayProgressBar;
53     eventList = new java.util.LinkedList JavaDoc();
54     activeObjectList = new java.util.LinkedList JavaDoc();
55   }
56
57   public int recordEvent(ActiveObject activeObject, SpyEvent evt) {
58     if (record && (!pause)) {
59       activeObjectList.add(activeObject);
60       eventList.add(evt);
61       return eventList.size();
62     }
63     return -1;
64   }
65
66   
67   public void record() {
68     if (record && pause)
69       pause = false;
70     else if (record) {
71       record = false;
72     } else if (!record) {
73       recordMark = 0;
74       activeObjectList.clear();
75       eventList.clear();
76       record = true;
77     }
78   }
79
80
81   public void pause() {
82     if (pause == false)
83       pause = true;
84     else pause = false;
85   }
86
87
88   public void play() {
89     if (play && pause) {
90       pause = false;
91     } else if (play) {
92       play = false;
93       t = null;
94     } else {
95       recordMark = 0;
96       play = true;
97       t = new Thread JavaDoc(new Runner(), "IC2D ThreadPlayer");
98       t.start();
99     }
100   }
101   
102   private void dispatchEvent(ActiveObject activeObject, SpyEvent event) {
103     switch (event.getType()) {
104     
105       case SpyEvent.OBJECT_WAIT_FOR_REQUEST_TYPE:
106         communicationEventListener.objectWaitingForRequest(activeObject, event);
107         break;
108         
109       case SpyEvent.OBJECT_WAIT_BY_NECESSITY_TYPE:
110         communicationEventListener.objectWaitingByNecessity(activeObject, event);
111         break;
112         
113         case SpyEvent.REQUEST_SENT_MESSAGE_TYPE:
114           communicationEventListener.requestMessageSent(activeObject, event);
115           break;
116           
117         case SpyEvent.REPLY_SENT_MESSAGE_TYPE:
118           communicationEventListener.replyMessageSent(activeObject, event);
119           break;
120           
121         case SpyEvent.REQUEST_RECEIVED_MESSAGE_TYPE:
122           communicationEventListener.requestMessageReceived(activeObject, event);
123           break;
124           
125         case SpyEvent.REPLY_RECEIVED_MESSAGE_TYPE:
126           communicationEventListener.replyMessageReceived(activeObject, event);
127           break;
128         
129         case SpyEvent.VOID_REQUEST_SERVED_TYPE:
130           communicationEventListener.voidRequestServed(activeObject, event);
131           break;
132         
133     }
134     communicationEventListener.allEventsProcessed();
135   }
136
137
138   private class Runner implements Runnable JavaDoc {
139     public void run() {
140       eventReplayProgressBar.setMinimum(1);
141       eventReplayProgressBar.setMaximum(eventList.size());
142       while ((recordMark < eventList.size()) && play || pause) {
143         if (! pause) {
144           eventReplayProgressBar.setString((recordMark + 1) + "/" + eventList.size());
145           eventReplayProgressBar.setValue(recordMark+1);
146           dispatchEvent((ActiveObject)activeObjectList.get(recordMark), (SpyEvent)eventList.get(recordMark));
147           recordMark++;
148         }
149         try {
150           Thread.sleep(500);
151         } catch (InterruptedException JavaDoc e) {}
152       }
153     }
154   }
155   
156 }
157
Popular Tags