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.io.IOException;
30  import java.io.ObjectOutputStream;
31  import java.net.Socket;
32  import java.net.UnknownHostException;
33  import java.util.Map;
34  
35  import org.perfidix.exceptions.SocketViewException;
36  
37  /**
38   * This class creates the connection to the eclipse view, which depict the
39   * current progress of the operation. It contains also the methods to upudate
40   * the view.
41   * 
42   * @author Lewandowski Lukas, University of Konstanz
43   */
44  public final class SocketViewStub implements IBenchRunSessionListener {
45  
46      /**
47       * The host name for the connection to the socket skeleton.
48       */
49      private transient final String host;
50      /**
51       * The command that will be send to the socket skeleton. So the skeleton
52       * knows which data will be dispatched afterwards.
53       */
54      private transient String command;
55      /**
56       * The client socket that will connect to the server socket of the ide
57       * plug-in.
58       */
59      private transient Socket socket;
60      /**
61       * The output stream for dispatching data to the ide skeleton to visualize
62       * the bench process / progress.
63       */
64      private transient ObjectOutputStream outputStream;
65  
66      /**
67       * The constructor initializes the given host name and the port, which are
68       * needed for creating a client socket. Afterwards it creates the client
69       * socket and the object output stream.
70       * 
71       * @param host
72       *            Host represents the {@link String} host name
73       * @param viewListenerPort
74       *            This param represents the port of the view.
75       * @throws SocketViewException
76       *             if communitcation fails
77       */
78      public SocketViewStub(final String host, final int viewListenerPort) throws SocketViewException {
79          if (host == null) {
80              this.host = "localhost";
81          } else {
82              this.host = host;
83          }
84          try {
85              socket = new Socket(this.host, viewListenerPort);
86              outputStream = new ObjectOutputStream(socket.getOutputStream());
87          } catch (final UnknownHostException e) {
88              throw new SocketViewException(e);
89          } catch (final IOException e) {
90              throw new SocketViewException(e);
91          }
92  
93      }
94  
95      /** {@inheritDoc} */
96      public void initTotalBenchProgress(final Map<String, Integer> elems) throws SocketViewException {
97          command = "init";
98          try {
99              outputStream.writeObject(command);
100             outputStream.writeObject(elems);
101         } catch (final IOException e) {
102             throw new SocketViewException(e);
103         }
104 
105     }
106 
107     /** {@inheritDoc} */
108     public void updateCurrentRun(final String currentElement) throws SocketViewException {
109         command = "updateCurrentRun";
110         try {
111             outputStream.writeObject(command);
112             outputStream.writeObject(currentElement);
113         } catch (final IOException e) {
114             throw new SocketViewException(e);
115         }
116 
117     }
118 
119     /** {@inheritDoc} */
120 
121     public void updateError(final String element, final String exception) throws SocketViewException {
122 
123         command = "updateError";
124         try {
125             outputStream.writeObject(command);
126             outputStream.writeObject(element);
127             outputStream.writeObject(exception);
128         } catch (final IOException e) {
129             throw new SocketViewException(e);
130         }
131 
132     }
133 
134     /** {@inheritDoc} */
135     public boolean finishedBenchRuns() throws SocketViewException {
136         command = "finished";
137         try {
138             outputStream.writeObject(command);
139             outputStream.close();
140             socket.close();
141             return true;
142         } catch (final IOException e) {
143             throw new SocketViewException(e);
144         }
145 
146     }
147 
148 }