Home | History | Annotate | Download | only in output
      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.agent.rt.internal.output;
     13 
     14 import java.io.IOException;
     15 import java.net.Socket;
     16 
     17 import org.jacoco.agent.rt.internal.IExceptionLogger;
     18 import org.jacoco.core.runtime.AgentOptions;
     19 import org.jacoco.core.runtime.RuntimeData;
     20 
     21 /**
     22  * Output that connects to a TCP port. This controller uses the following
     23  * agent options:
     24  * <ul>
     25  * <li>address</li>
     26  * <li>port</li>
     27  * </ul>
     28  */
     29 public class TcpClientOutput implements IAgentOutput {
     30 
     31 	private final IExceptionLogger logger;
     32 
     33 	private TcpConnection connection;
     34 
     35 	private Thread worker;
     36 
     37 	/**
     38 	 * New controller instance.
     39 	 *
     40 	 * @param logger
     41 	 *            logger to use in case of exceptions is spawned threads
     42 	 */
     43 	public TcpClientOutput(final IExceptionLogger logger) {
     44 		this.logger = logger;
     45 	}
     46 
     47 	public void startup(final AgentOptions options, final RuntimeData data)
     48 			throws IOException {
     49 		final Socket socket = createSocket(options);
     50 		connection = new TcpConnection(socket, data);
     51 		connection.init();
     52 		worker = new Thread(new Runnable() {
     53 			public void run() {
     54 				try {
     55 					connection.run();
     56 				} catch (final IOException e) {
     57 					logger.logExeption(e);
     58 				}
     59 			}
     60 		});
     61 		worker.setName(getClass().getName());
     62 		worker.setDaemon(true);
     63 		worker.start();
     64 	}
     65 
     66 	public void shutdown() throws Exception {
     67 		connection.close();
     68 		worker.join();
     69 	}
     70 
     71 	public void writeExecutionData(final boolean reset) throws IOException {
     72 		connection.writeExecutionData(reset);
     73 	}
     74 
     75 	/**
     76 	 * Open a socket based on the given configuration.
     77 	 *
     78 	 * @param options
     79 	 *            address and port configuration
     80 	 * @return opened socket
     81 	 * @throws IOException
     82 	 */
     83 	protected Socket createSocket(final AgentOptions options)
     84 			throws IOException {
     85 		return new Socket(options.getAddress(), options.getPort());
     86 	}
     87 
     88 }
     89