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