TcpServerController.java
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 *******************************************************************************/
12package org.jacoco.agent.rt.controller;
13
14import java.io.IOException;
15import java.net.InetAddress;
16import java.net.ServerSocket;
17import java.net.UnknownHostException;
18
19import org.jacoco.agent.rt.IExceptionLogger;
20import org.jacoco.core.runtime.AgentOptions;
21import org.jacoco.core.runtime.IRuntime;
22
23/**
24 * @author Marc R. Hoffmann
25 * @version 0.4.1.20101007204400
26 */
27public class TcpServerController implements IAgentController {
28
29 private TcpConnection connection;
30
31 private final IExceptionLogger logger;
32
33 private ServerSocket serverSocket;
34
35 private Thread worker;
36
37 public TcpServerController(final IExceptionLogger logger) {
38 this.logger = logger;
39 }
40
41 public void startup(final AgentOptions options, final IRuntime runtime)
42 throws IOException {
43 serverSocket = createServerSocket(options);
44 worker = new Thread(new Runnable() {
45 public void run() {
46 while (!serverSocket.isClosed()) {
47 try {
48 synchronized (serverSocket) {
49 connection = new TcpConnection(serverSocket
50 .accept(), runtime);
51 }
52 connection.init();
53 connection.run();
54 } catch (IOException e) {
55 // If the serverSocket is closed while accepting
56 // connections a SocketException is expected.
57 if (!serverSocket.isClosed()) {
58 logger.logExeption(e);
59 }
60 }
61 }
62 }
63 });
64 worker.setName(getClass().getName());
65 worker.setDaemon(true);
66 worker.start();
67 }
68
69 public void shutdown() throws Exception {
70 serverSocket.close();
71 synchronized (serverSocket) {
72 if (connection != null) {
73 connection.close();
74 }
75 }
76 worker.join();
77 }
78
79 public void writeExecutionData() throws IOException {
80 if (connection != null) {
81 connection.writeExecutionData();
82 }
83 }
84
85 /**
86 * Open a server socket based on the given configuration.
87 *
88 * @param options
89 * address and port configuration
90 * @return opened server socket
91 * @throws IOException
92 */
93 protected ServerSocket createServerSocket(final AgentOptions options)
94 throws IOException {
95 final InetAddress inetAddr = getInetAddress(options.getAddress());
96 return new ServerSocket(options.getPort(), 1, inetAddr);
97 }
98
99 /**
100 * Returns the {@link InetAddress} object to open the server socket on.
101 *
102 * @param options
103 * agent options
104 * @return address to open the server socket
105 * @throws UnknownHostException
106 */
107 protected InetAddress getInetAddress(final String address)
108 throws UnknownHostException {
109 if ("*".equals(address)) {
110 return null;
111 } else {
112 return InetAddress.getByName(address);
113 }
114 }
115
116}