Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SocketViewStub |
|
| 3.8;3.8 |
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 | 0 | public SocketViewStub(final String host, final int viewListenerPort) throws SocketViewException { |
79 | 0 | if (host == null) { |
80 | 0 | this.host = "localhost"; |
81 | } else { | |
82 | 0 | this.host = host; |
83 | } | |
84 | try { | |
85 | 0 | socket = new Socket(this.host, viewListenerPort); |
86 | 0 | outputStream = new ObjectOutputStream(socket.getOutputStream()); |
87 | 0 | } catch (final UnknownHostException e) { |
88 | 0 | throw new SocketViewException(e); |
89 | 0 | } catch (final IOException e) { |
90 | 0 | throw new SocketViewException(e); |
91 | 0 | } |
92 | ||
93 | 0 | } |
94 | ||
95 | /** {@inheritDoc} */ | |
96 | public void initTotalBenchProgress(final Map<String, Integer> elems) throws SocketViewException { | |
97 | 0 | command = "init"; |
98 | try { | |
99 | 0 | outputStream.writeObject(command); |
100 | 0 | outputStream.writeObject(elems); |
101 | 0 | } catch (final IOException e) { |
102 | 0 | throw new SocketViewException(e); |
103 | 0 | } |
104 | ||
105 | 0 | } |
106 | ||
107 | /** {@inheritDoc} */ | |
108 | public void updateCurrentRun(final String currentElement) throws SocketViewException { | |
109 | 0 | command = "updateCurrentRun"; |
110 | try { | |
111 | 0 | outputStream.writeObject(command); |
112 | 0 | outputStream.writeObject(currentElement); |
113 | 0 | } catch (final IOException e) { |
114 | 0 | throw new SocketViewException(e); |
115 | 0 | } |
116 | ||
117 | 0 | } |
118 | ||
119 | /** {@inheritDoc} */ | |
120 | ||
121 | public void updateError(final String element, final String exception) throws SocketViewException { | |
122 | ||
123 | 0 | command = "updateError"; |
124 | try { | |
125 | 0 | outputStream.writeObject(command); |
126 | 0 | outputStream.writeObject(element); |
127 | 0 | outputStream.writeObject(exception); |
128 | 0 | } catch (final IOException e) { |
129 | 0 | throw new SocketViewException(e); |
130 | 0 | } |
131 | ||
132 | 0 | } |
133 | ||
134 | /** {@inheritDoc} */ | |
135 | public boolean finishedBenchRuns() throws SocketViewException { | |
136 | 0 | command = "finished"; |
137 | try { | |
138 | 0 | outputStream.writeObject(command); |
139 | 0 | outputStream.close(); |
140 | 0 | socket.close(); |
141 | 0 | return true; |
142 | 0 | } catch (final IOException e) { |
143 | 0 | throw new SocketViewException(e); |
144 | } | |
145 | ||
146 | } | |
147 | ||
148 | } |