1 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 eventList; 40 private java.util.LinkedList 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 t; 48 private javax.swing.JProgressBar eventReplayProgressBar; 49 50 public ThreadPlayer(CommunicationEventListener communicationEventListener, javax.swing.JProgressBar eventReplayProgressBar) { 51 this.communicationEventListener = communicationEventListener; 52 this.eventReplayProgressBar = eventReplayProgressBar; 53 eventList = new java.util.LinkedList (); 54 activeObjectList = new java.util.LinkedList (); 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 (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 { 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 e) {} 152 } 153 } 154 } 155 156 } 157 | Popular Tags |