001 package toolbus.viewer;
002
003 import toolbus.IOperations;
004 import toolbus.tool.ToolInstance;
005 import toolbus.util.collections.ConcurrentHashMap;
006 import toolbus.util.collections.ConcurrentHashSet;
007 import toolbus.util.collections.EntryHandlerConstants;
008 import toolbus.util.collections.ConcurrentHashMap.HashMapEntryHandler;
009 import aterm.ATerm;
010
011 /**
012 * Handles performance information dispatch and retrieval.
013 *
014 * @author Arnold Lankamp
015 */
016 public class PerformanceInformationHandler{
017 private final IPerformanceMonitor performanceMonitor;
018
019 private final ConcurrentHashSet<ATerm> toolInstancesToMonitor;
020 private final ConcurrentHashSet<String> toolTypesToMonitor;
021
022 private final ConcurrentHashMap<ToolInstance, ATerm> arrivedPerformanceStats;
023 private final PerformanceStatsEntryHandler performanceStatsEntryHandler;
024
025 /**
026 * Constructor.
027 *
028 * @param performanceMonitor
029 * The tool performance monitor to use.
030 */
031 public PerformanceInformationHandler(IPerformanceMonitor performanceMonitor){
032 super();
033
034 this.performanceMonitor = performanceMonitor;
035
036 toolInstancesToMonitor = new ConcurrentHashSet<ATerm>();
037 toolTypesToMonitor = new ConcurrentHashSet<String>();
038
039 arrivedPerformanceStats = new ConcurrentHashMap<ToolInstance, ATerm>();
040 performanceStatsEntryHandler = new PerformanceStatsEntryHandler(performanceMonitor);
041 }
042
043 /**
044 * Initiates the monitoring of the tool associated with the given tool key.
045 *
046 * @param toolKey
047 * The tool key associated with the tool we want to monitor.
048 */
049 public void startMonitoringTool(ATerm toolKey){
050 toolInstancesToMonitor.put(toolKey);
051 }
052
053 /**
054 * Stops monitoring the tool associated with the given tool key.
055 *
056 * @param toolKey
057 * The tool key associated with the tool which we want to stop monitoring.
058 */
059 public void stopMonitoringTool(ATerm toolKey){
060 toolInstancesToMonitor.remove(toolKey);
061 }
062
063 /**
064 * Initiates the monitoring of the given tool type.
065 *
066 * @param toolName
067 * The tool type of tool we want to monitor.
068 */
069 public void startMonitorToolType(String toolName){
070 toolTypesToMonitor.put(toolName);
071 }
072
073 /**
074 * Stops monitoring tools of the given type.
075 *
076 * @param toolName
077 * The type of tool which we want to stop monitoring.
078 */
079 public void stopMonitoringToolType(String toolName){
080 toolTypesToMonitor.remove(toolName);
081 }
082
083 /**
084 * Invoked by the debug toolbus when communication with a tool instance occured.
085 * @param toolInstance
086 * The tool instance that received the data.
087 * @param operation
088 * The operation associated with the received data.
089 */
090 public void toolCommunicationTriggered(ToolInstance toolInstance, int operation){
091 if(operation == IOperations.DEBUGPERFORMANCESTATS){
092 arrivedPerformanceStats.put(toolInstance, toolInstance.getLastDebugPerformanceStats());
093 }else if(operation == IOperations.CONNECT){
094 performanceMonitor.toolConnected(toolInstance);
095 }else if(operation == IOperations.END){
096 performanceMonitor.toolConnectionClosed(toolInstance);
097 }else if(toolInstance.isConnected()){
098 if(toolInstancesToMonitor.contains(toolInstance.getToolKey()) || toolTypesToMonitor.contains(toolInstance.getToolName())){
099 toolInstance.sendDebugPerformanceStatsRequest();
100 }
101 }
102 }
103
104 /**
105 * Invoked by the debug toolbus when it wishes to process all currently received performance
106 * data.
107 */
108 public void handleRetrievedData(){
109 arrivedPerformanceStats.iterate(performanceStatsEntryHandler);
110 }
111
112 /**
113 * Entry handler used for iterating over the arrived performance statistics.
114 *
115 * @author Arnold Lankamp
116 */
117 private static class PerformanceStatsEntryHandler extends HashMapEntryHandler<ToolInstance, ATerm>{
118 private final IPerformanceMonitor performanceMonitor;
119
120 /**
121 * Costructor.
122 *
123 * @param performanceMonitor
124 * The performance monitor that handles the data.
125 */
126 public PerformanceStatsEntryHandler(IPerformanceMonitor performanceMonitor){
127 super();
128
129 this.performanceMonitor = performanceMonitor;
130 }
131
132 /**
133 * @see toolbus.util.collections.ConcurrentHashMap.HashMapEntryHandler#handle(Object, Object)
134 */
135 public int handle(ToolInstance toolInstance, ATerm performanceStats){
136 performanceMonitor.performanceStatsArrived(toolInstance, performanceStats);
137
138 return EntryHandlerConstants.REMOVE;
139 }
140 }
141 }