|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.nio.channels.SelectionKey; |
| 22 | +import java.nio.channels.Selector; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | +import jnr.enxio.channels.NativeSelectorProvider; |
| 27 | +import jnr.unixsocket.UnixServerSocketChannel; |
| 28 | +import jnr.unixsocket.UnixSocketAddress; |
| 29 | +import jnr.unixsocket.UnixSocketChannel; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * This is a simple echo unix socket server adapted from the JNR socket project. |
| 35 | + * https://github.com/jnr/jnr-unixsocket/blob/master/src/test/java/jnr/unixsocket/example/UnixServer.java |
| 36 | + */ |
| 37 | +class FakeUnixSocketServer { |
| 38 | + private static Logger log = LoggerFactory.getLogger(FakeUnixSocketServer.class); |
| 39 | + private final String path; |
| 40 | + private final UnixSocketAddress address; |
| 41 | + private UnixServerSocketChannel channel; |
| 42 | + private final AtomicBoolean closed = new AtomicBoolean(); |
| 43 | + |
| 44 | + FakeUnixSocketServer(String path) { |
| 45 | + this.path = path; |
| 46 | + this.address = new UnixSocketAddress(path); |
| 47 | + } |
| 48 | + |
| 49 | + public synchronized void close() throws IOException { |
| 50 | + if (!this.closed.get()) { |
| 51 | + this.closed.set(true); |
| 52 | + } |
| 53 | + if (this.channel != null) { |
| 54 | + channel.close(); |
| 55 | + this.channel = null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public synchronized void start() throws IOException { |
| 60 | + java.io.File path = new java.io.File(this.path); |
| 61 | + path.deleteOnExit(); |
| 62 | + |
| 63 | + this.channel = UnixServerSocketChannel.open(); |
| 64 | + channel.configureBlocking(false); |
| 65 | + channel.socket().bind(address); |
| 66 | + |
| 67 | + Thread t = new Thread(this::run); |
| 68 | + t.start(); |
| 69 | + } |
| 70 | + |
| 71 | + public void run() { |
| 72 | + log.info("Starting fake unix socket server at path " + this.path); |
| 73 | + try { |
| 74 | + Selector sel = NativeSelectorProvider.getInstance().openSelector(); |
| 75 | + channel.register(sel, SelectionKey.OP_ACCEPT, new ServerActor(channel)); |
| 76 | + |
| 77 | + log.info("Waiting for connections path " + this.path); |
| 78 | + while (!this.closed.get()) { |
| 79 | + if (sel.select() > 0) { |
| 80 | + Set<SelectionKey> keys = sel.selectedKeys(); |
| 81 | + Iterator<SelectionKey> iterator = keys.iterator(); |
| 82 | + boolean running = false; |
| 83 | + boolean cancelled = false; |
| 84 | + while (iterator.hasNext()) { |
| 85 | + SelectionKey k = iterator.next(); |
| 86 | + Actor a = (Actor) k.attachment(); |
| 87 | + if (a.rxready()) { |
| 88 | + running = true; |
| 89 | + } else { |
| 90 | + k.cancel(); |
| 91 | + cancelled = true; |
| 92 | + } |
| 93 | + iterator.remove(); |
| 94 | + } |
| 95 | + if (!running && cancelled) { |
| 96 | + log.info("No Actors Running any more"); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } catch (IOException ex) { |
| 102 | + log.info("IOException: waiting for sockets", ex); |
| 103 | + } |
| 104 | + log.info("UnixServer EXIT"); |
| 105 | + } |
| 106 | + |
| 107 | + static interface Actor { |
| 108 | + public boolean rxready(); |
| 109 | + } |
| 110 | + |
| 111 | + static final class ServerActor implements Actor { |
| 112 | + private final UnixServerSocketChannel channel; |
| 113 | + |
| 114 | + public ServerActor(UnixServerSocketChannel channel) { |
| 115 | + this.channel = channel; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public final boolean rxready() { |
| 120 | + log.info("Handling unix socket connect."); |
| 121 | + try { |
| 122 | + UnixSocketChannel client = channel.accept(); |
| 123 | + client.configureBlocking(false); |
| 124 | + ByteBuffer response = ByteBuffer.wrap("HELLO\n".getBytes("UTF-8")); |
| 125 | + client.write(response); |
| 126 | + log.info("Handling unix socket done."); |
| 127 | + return true; |
| 128 | + } catch (IOException ex) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments