eolivelli commented on a change in pull request #240: [SUREFIRE-1658] TCP/IP Channel for forked Surefire JVM. Extensions API and SPI. Polymorphism for remote and local process communication. URL: https://github.com/apache/maven-surefire/pull/240#discussion_r381320876
########## File path: maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java ########## @@ -0,0 +1,123 @@ +package org.apache.maven.plugin.surefire.extensions; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.surefire.extensions.CloseableDaemonThread; +import org.apache.maven.surefire.extensions.CommandReader; +import org.apache.maven.surefire.extensions.EventHandler; +import org.apache.maven.surefire.extensions.ForkChannel; +import org.apache.maven.surefire.extensions.util.CountdownCloseable; +import org.apache.maven.surefire.extensions.util.LineConsumerThread; +import org.apache.maven.surefire.extensions.util.StreamFeeder; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketOption; +import java.nio.channels.Channel; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.WritableByteChannel; + +import static java.net.StandardSocketOptions.SO_KEEPALIVE; +import static java.net.StandardSocketOptions.SO_REUSEADDR; +import static java.net.StandardSocketOptions.TCP_NODELAY; +import static java.nio.channels.ServerSocketChannel.open; + +/** + * + */ +final class SurefireForkChannel extends ForkChannel +{ + private final ServerSocketChannel server; + private final int serverPort; + private SocketChannel channel; + + SurefireForkChannel( int forkChannelId ) throws IOException + { + super( forkChannelId ); + server = open(); + setTrueOptions( SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE ); + server.bind( new InetSocketAddress( 0 ) ); + serverPort = ( (InetSocketAddress) server.getLocalAddress() ).getPort(); + } + + @Override + public void connectToClient() throws IOException + { + if ( channel != null ) + { + throw new IllegalStateException( "already accepted TCP client connection" ); + } + channel = server.accept(); Review comment: here we should add some kind of authentication, at least exchange a one time token (UUID). Otherwise any other program on the same machine can connect to this server and start pushing data (leading to out of memory or other nasty things). We can do it in a follow up patch (this is already huge), but we have to create a JIRA and set it blocker ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
