RemoteControlReader.java revision e4b0fede262e9df8546def83e68c687656f539ba
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 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 * $Id: $
12 *******************************************************************************/
13package org.jacoco.core.runtime;
14
15import java.io.EOFException;
16import java.io.IOException;
17import java.io.InputStream;
18
19import org.jacoco.core.data.ExecutionDataReader;
20
21/**
22 * {@link ExecutionDataReader} with commands added for runtime remote control.
23 *
24 * @author Marc R. Hoffmann
25 * @version $Revision: $
26 */
27public class RemoteControlReader extends ExecutionDataReader {
28
29	private IRemoteCommandVisitor remoteCommandVisitor;
30
31	/**
32	 * Create a new read based on the given input stream.
33	 *
34	 * @param input
35	 *            input stream to read commands from
36	 * @throws IOException
37	 *             if the stream does not have a valid header
38	 */
39	public RemoteControlReader(final InputStream input) throws IOException {
40		super(input);
41	}
42
43	@Override
44	protected boolean readBlock(final byte blockid) throws IOException {
45		switch (blockid) {
46		case RemoteControlWriter.BLOCK_CMDDUMP:
47			readDumpCommand();
48			return true;
49		case RemoteControlWriter.BLOCK_CMDOK:
50			return false;
51		case RemoteControlWriter.BLOCK_CMDCLOSE:
52			throw new EOFException();
53		default:
54			return super.readBlock(blockid);
55		}
56	}
57
58	/**
59	 * Sets an listener for agent commands.
60	 *
61	 * @param visitor
62	 */
63	public void setRemoteCommandVisitor(final IRemoteCommandVisitor visitor) {
64		this.remoteCommandVisitor = visitor;
65	}
66
67	private void readDumpCommand() throws IOException {
68		if (remoteCommandVisitor == null) {
69			throw new IOException("No remote command visitor.");
70		}
71		final boolean dump = in.readBoolean();
72		final boolean reset = in.readBoolean();
73		remoteCommandVisitor.visitDumpCommand(dump, reset);
74	}
75
76}
77