View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.socketadapter;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import org.perfidix.element.BenchmarkMethod;
34  import org.perfidix.exceptions.AbstractPerfidixMethodException;
35  import org.perfidix.exceptions.SocketViewException;
36  import org.perfidix.meter.AbstractMeter;
37  
38  /**
39   * This class creates the connection to the eclipse view via {@link SocketViewStub}. It contains the methods
40   * which update the view to
41   * inform about the bench process progress.
42   * 
43   * @author Lewandowski Lukas, University of Konstanz
44   */
45  public final class SocketViewProgressUpdater implements IUpdater {
46  
47      private final transient SocketViewStub viewStub;
48      private transient int meterHash = 0;
49      private transient boolean regMeterHash = false;
50  
51      /**
52       * The constructor initializes the host and port for creation a client
53       * socket.
54       * 
55       * @param host
56       *            Host is the host name of the view provider.
57       * @param port
58       *            Port represent the port number of the eclipse view.
59       * @throws SocketViewException
60       */
61      public SocketViewProgressUpdater(final String host, final int port) throws SocketViewException {
62          String strubParam = host;
63          if (host == null || host.equals("")) {
64              strubParam = "localhost";
65          }
66          viewStub = new SocketViewStub(strubParam, port);
67  
68      }
69  
70      /**
71       * This method initializes the values of the eclipse view and resets the
72       * progress bar.
73       * 
74       * @param mapping
75       *            a mapping with all methods to benchmark and the related runs
76       * @throws SocketViewException
77       */
78      public boolean initProgressView(final Map<BenchmarkMethod, Integer> mapping) throws SocketViewException {
79          if (mapping != null) {
80              final Set<BenchmarkMethod> methodSet = mapping.keySet();
81  
82              final Map<String, Integer> finalMap = new HashMap<String, Integer>();
83              for (BenchmarkMethod benchmarkMethod : methodSet) {
84  
85                  finalMap.put(benchmarkMethod.getMethodWithClassName(), mapping.get(benchmarkMethod));
86              }
87  
88              viewStub.initTotalBenchProgress(finalMap);
89          }
90          return true;
91      }
92  
93      /**
94       * This method notifies the eclipse view which element is currently benched.
95       * 
96       * @param meter
97       *            The current meter.
98       * @param name
99       *            This param represents the java element which is currently
100      *            benched and which is fully qualified.
101      * @throws SocketViewException
102      */
103     public boolean updateCurrentElement(final AbstractMeter meter, final String name)
104         throws SocketViewException {
105         if (meter != null && !regMeterHash) {
106             registerFirstMeterHash(meter);
107         }
108         if (name != null && meter.hashCode() == (getRegMeter())) {
109             viewStub.updateCurrentRun(name);
110         }
111         return true;
112     }
113 
114     /**
115      * This method informs the view that an error occurred while benching the
116      * current element.
117      * 
118      * @param name
119      *            Element represents the java element which has not been
120      *            executed successfully.
121      * @param exception
122      *            The exception caused by the element.
123      * @throws SocketViewException
124      */
125     public boolean updateErrorInElement(final String name, final Exception exception)
126         throws SocketViewException {
127         if (name != null && exception != null) {
128             if (exception instanceof AbstractPerfidixMethodException) {
129                 final AbstractPerfidixMethodException exc = (AbstractPerfidixMethodException)exception;
130                 viewStub.updateError(name, exc.getExec().getClass().getSimpleName());
131             }
132             if (exception instanceof SocketViewException) {
133                 final SocketViewException viewException = (SocketViewException)exception;
134                 viewStub.updateError(name, viewException.getExc().getClass().getSimpleName());
135             }
136         }
137         return true;
138     }
139 
140     /**
141      * This method notifies the view that all benches have been executed and the
142      * bench progress is finished.
143      * 
144      * @throws SocketViewException
145      */
146     public void finished() throws SocketViewException {
147         viewStub.finishedBenchRuns();
148         regMeterHash = false;
149     }
150 
151     /**
152      * Register the meter hash for update the view, so only one time the current
153      * view element will be updated and not for every meter.
154      * 
155      * @param meterHash
156      *            The hash of the meter.
157      */
158     private void registerFirstMeterHash(final AbstractMeter meterHash) {
159         this.meterHash = meterHash.hashCode();
160         regMeterHash = true;
161     }
162 
163     /**
164      * The meter hash.
165      * 
166      * @return The meter hash of the registered hash;
167      */
168     private int getRegMeter() {
169         return meterHash;
170     }
171 
172 }