aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core/src/org/jacoco/core/tools/ExecDumpClient.java
blob: 420c7680e64f7d4affddc927811670d52c43ef7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.tools;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.Socket;

import org.jacoco.core.runtime.RemoteControlReader;
import org.jacoco.core.runtime.RemoteControlWriter;

/**
 * A client for remote execution data dumps.
 */
public class ExecDumpClient {

	private boolean dump;
	private boolean reset;
	private int retryCount;
	private long retryDelay;

	/**
	 * New instance with the defaults <code>dump==true</code>,
	 * <code>reset==false</code>, <code>retryCount==0</code> and
	 * <code>retryDelay=1000</code>.
	 */
	public ExecDumpClient() {
		this.dump = true;
		this.reset = false;
		this.retryCount = 0;
		this.setRetryDelay(1000);
	}

	/**
	 * Specifies whether a dump should be requested
	 *
	 * @param dump
	 *            <code>true</code> if a dump should be requested
	 */
	public void setDump(final boolean dump) {
		this.dump = dump;
	}

	/**
	 * Specifies whether execution data should be reset.
	 *
	 * @param reset
	 *            <code>true</code> if execution data should be reset
	 */
	public void setReset(final boolean reset) {
		this.reset = reset;
	}

	/**
	 * Sets the number of retry attempts to connect to the target socket. This
	 * allows to wait for a certain time until the target agent has initialized.
	 *
	 * @param retryCount
	 *            number of retries
	 */
	public void setRetryCount(final int retryCount) {
		this.retryCount = retryCount;
	}

	/**
	 * Sets the delay time before between connection attempts.
	 *
	 * @param retryDelay
	 *            delay in milliseconds
	 */
	public void setRetryDelay(final long retryDelay) {
		this.retryDelay = retryDelay;
	}

	/**
	 * Requests a dump from the given end-point.
	 *
	 * @param address
	 *            IP-Address to connect to
	 * @param port
	 *            port to connect to
	 * @return container for the dumped data
	 * @throws IOException
	 *             in case the dump can not be requested
	 */
	public ExecFileLoader dump(final String address, final int port)
			throws IOException {
		return dump(InetAddress.getByName(address), port);
	}

	/**
	 * Requests a dump from the given end-point.
	 *
	 * @param address
	 *            host name or IP-Address to connect to
	 * @param port
	 *            port to connect to
	 * @return container for the dumped data
	 * @throws IOException
	 *             in case the dump can not be requested
	 */
	public ExecFileLoader dump(final InetAddress address, final int port)
			throws IOException {
		final ExecFileLoader loader = new ExecFileLoader();
		final Socket socket = tryConnect(address, port);
		try {
			final RemoteControlWriter remoteWriter = new RemoteControlWriter(
					socket.getOutputStream());
			final RemoteControlReader remoteReader = new RemoteControlReader(
					socket.getInputStream());
			remoteReader.setSessionInfoVisitor(loader.getSessionInfoStore());
			remoteReader
					.setExecutionDataVisitor(loader.getExecutionDataStore());

			remoteWriter.visitDumpCommand(dump, reset);

			if (!remoteReader.read()) {
				throw new IOException("Socket closed unexpectedly.");
			}

		} finally {
			socket.close();
		}
		return loader;
	}

	private Socket tryConnect(final InetAddress address, final int port)
			throws IOException {
		int count = 0;
		while (true) {
			try {
				onConnecting(address, port);
				return new Socket(address, port);
			} catch (final IOException e) {
				if (++count > retryCount) {
					throw e;
				}
				onConnectionFailure(e);
				sleep();
			}
		}
	}

	private void sleep() throws InterruptedIOException {
		try {
			Thread.sleep(retryDelay);
		} catch (final InterruptedException e) {
			throw new InterruptedIOException();
		}
	}

	/**
	 * This method can be overwritten to get an event just before a connection
	 * is made.
	 *
	 * @param address
	 *            target address
	 * @param port
	 *            target port
	 */
	protected void onConnecting(
			@SuppressWarnings("unused") final InetAddress address,
			@SuppressWarnings("unused") final int port) {
	}

	/**
	 * This method can be overwritten to get an event for connection failures
	 * when another retry will be attempted.
	 *
	 * @param exception
	 *            connection error
	 */
	protected void onConnectionFailure(
			@SuppressWarnings("unused") final IOException exception) {
	}

}