cstamas commented on code in PR #435: URL: https://github.com/apache/maven-resolver/pull/435#discussion_r1504317933
########## maven-resolver-named-locks-ipc/src/main/java/org/eclipse/aether/named/ipc/IpcClient.java: ########## @@ -0,0 +1,417 @@ +/* + * 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. + */ +package org.eclipse.aether.named.ipc; + +import java.io.*; +import java.net.SocketAddress; +import java.nio.channels.ByteChannel; +import java.nio.channels.Channels; +import java.nio.channels.FileLock; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Random; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_ACQUIRE; +import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_CLOSE; +import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_CONTEXT; +import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_STOP; +import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_ACQUIRE; +import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_CLOSE; +import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_CONTEXT; +import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_STOP; + +/** + * Client side implementation. + * The client instance is bound to a given maven repository. + * + * @since 2.0.0 + */ +public class IpcClient { + + static final boolean IS_WINDOWS = + System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win"); + + volatile boolean initialized; + Path lockPath; + Path logPath; + Path syncPath; + SocketChannel socket; + DataOutputStream output; + DataInputStream input; + Thread receiver; + AtomicInteger requestId = new AtomicInteger(); + Map<Integer, CompletableFuture<List<String>>> responses = new ConcurrentHashMap<>(); + + IpcClient(Path lockPath, Path logPath, Path syncPath) { + this.lockPath = lockPath; + this.logPath = logPath; + this.syncPath = syncPath; + } + + void ensureInitialized() throws IOException { + if (!initialized) { + synchronized (this) { + if (!initialized) { + socket = createClient(); + ByteChannel wrapper = new ByteChannelWrapper(socket); + input = new DataInputStream(Channels.newInputStream(wrapper)); + output = new DataOutputStream(Channels.newOutputStream(wrapper)); + receiver = new Thread(this::receive); + receiver.setDaemon(true); + receiver.start(); + initialized = true; + } + } + } + } + + SocketChannel createClient() throws IOException { + String familyProp = System.getProperty(IpcServer.SYSTEM_PROP_FAMILY, IpcServer.DEFAULT_FAMILY); + SocketFamily family = familyProp != null ? SocketFamily.valueOf(familyProp) : SocketFamily.inet; + + Path lockPath = this.lockPath.toAbsolutePath().normalize(); + Path lockFile = + lockPath.resolve(".maven-resolver-ipc-lock-" + family.name().toLowerCase()); + if (!Files.isRegularFile(lockFile)) { + if (!Files.isDirectory(lockFile.getParent())) { + Files.createDirectories(lockFile.getParent()); + } + } + + try (RandomAccessFile raf = new RandomAccessFile(lockFile.toFile(), "rw")) { + try (FileLock lock = raf.getChannel().lock()) { + String line = raf.readLine(); + if (line != null) { + try { + SocketAddress address = SocketFamily.fromString(line); + return SocketChannel.open(address); + } catch (IOException e) { + // ignore + } + } + + ServerSocketChannel ss = family.openServerSocket(); + String tmpaddr = SocketFamily.toString(ss.getLocalAddress()); + String rand = Long.toHexString(new Random().nextLong()); + String syncCmd = IS_WINDOWS ? "mvnd-sync.exe" : "mvnd-sync"; Review Comment: Original mvnd code did use graalvm to make native image out of server, that stuff I did not bring over, but we may... -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
