Skip to content

Instantly share code, notes, and snippets.

@marchof
Created September 2, 2014 05:09
Show Gist options
  • Save marchof/1fd7dc6dd06e0510c814 to your computer and use it in GitHub Desktop.
Save marchof/1fd7dc6dd06e0510c814 to your computer and use it in GitHub Desktop.
ExecutionDataServer with in-memory buffer
/*******************************************************************************
* Copyright (c) 2009, 2014 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.examples;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.ExecutionDataWriter;
import org.jacoco.core.data.IExecutionDataVisitor;
import org.jacoco.core.data.ISessionInfoVisitor;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.core.runtime.RemoteControlReader;
import org.jacoco.core.runtime.RemoteControlWriter;
/**
* This example starts a socket server to collect coverage from agents that run
* in output mode <code>tcpclient</code>. The collected data is dumped to a
* local file.
*/
public final class ExecutionDataServer {
private static final String DESTFILE = "jacoco-server.exec";
private static final String ADDRESS = "localhost";
private static final int PORT = 6300;
/**
* Start the server as a standalone program.
*
* @param args
* @throws IOException
*/
public static void main(final String[] args) throws IOException {
final ServerSocket server = new ServerSocket(PORT, 0,
InetAddress.getByName(ADDRESS));
while (true) {
final Handler handler = new Handler(server.accept());
new Thread(handler).start();
}
}
private static class Handler implements Runnable, ISessionInfoVisitor,
IExecutionDataVisitor {
private final Socket socket;
private final RemoteControlReader reader;
private final ByteArrayOutputStream buffer;
private final ExecutionDataWriter writer;
Handler(final Socket socket) throws IOException {
this.socket = socket;
buffer = new ByteArrayOutputStream();
this.writer = new ExecutionDataWriter(buffer);
// Just send a valid header:
new RemoteControlWriter(socket.getOutputStream());
reader = new RemoteControlReader(socket.getInputStream());
reader.setSessionInfoVisitor(this);
reader.setExecutionDataVisitor(this);
}
public void run() {
try {
while (reader.read()) {
}
socket.close();
// Now do something with the buffer content:
buffer.toByteArray();
} catch (final IOException e) {
e.printStackTrace();
}
}
public void visitSessionInfo(final SessionInfo info) {
System.out.printf("Retrieving execution Data for session: %s%n",
info.getId());
writer.visitSessionInfo(info);
}
public void visitClassExecution(final ExecutionData data) {
writer.visitClassExecution(data);
}
}
private ExecutionDataServer() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment