• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.examples;
14 
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.net.InetAddress;
18 import java.net.Socket;
19 
20 import org.jacoco.core.data.ExecutionDataWriter;
21 import org.jacoco.core.runtime.RemoteControlReader;
22 import org.jacoco.core.runtime.RemoteControlWriter;
23 
24 /**
25  * This example connects to a coverage agent that run in output mode
26  * <code>tcpserver</code> and requests execution data. The collected data is
27  * dumped to a local file.
28  */
29 public final class ExecutionDataClient {
30 
31 	private static final String DESTFILE = "jacoco-client.exec";
32 
33 	private static final String ADDRESS = "localhost";
34 
35 	private static final int PORT = 6300;
36 
37 	/**
38 	 * Starts the execution data request.
39 	 *
40 	 * @param args
41 	 * @throws IOException
42 	 */
main(final String[] args)43 	public static void main(final String[] args) throws IOException {
44 		final FileOutputStream localFile = new FileOutputStream(DESTFILE);
45 		final ExecutionDataWriter localWriter = new ExecutionDataWriter(
46 				localFile);
47 
48 		// Open a socket to the coverage agent:
49 		final Socket socket = new Socket(InetAddress.getByName(ADDRESS), PORT);
50 		final RemoteControlWriter writer = new RemoteControlWriter(
51 				socket.getOutputStream());
52 		final RemoteControlReader reader = new RemoteControlReader(
53 				socket.getInputStream());
54 		reader.setSessionInfoVisitor(localWriter);
55 		reader.setExecutionDataVisitor(localWriter);
56 
57 		// Send a dump command and read the response:
58 		writer.visitDumpCommand(true, false);
59 		if (!reader.read()) {
60 			throw new IOException("Socket closed unexpectedly.");
61 		}
62 
63 		socket.close();
64 		localFile.close();
65 	}
66 
ExecutionDataClient()67 	private ExecutionDataClient() {
68 	}
69 }
70