diff --git a/pom.xml b/pom.xml index 7af7ab4..f343fc9 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,11 @@ + + com.teragrep + net_01 + 1.0.1 + org.slf4j diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainer.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainer.java deleted file mode 100644 index 8b98dae..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainer.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import java.nio.ByteBuffer; - -/** - * BufferContainer is a decorator for {@link ByteBuffer} with an id. - */ -public interface BufferContainer { - - /** - * @return id of the buffer - */ - long id(); - - /** - * @return encapsulated {@link ByteBuffer}. - */ - ByteBuffer buffer(); - - /** - * @return is this a stub implementation. - */ - boolean isStub(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainerImpl.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainerImpl.java deleted file mode 100644 index ee744f7..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainerImpl.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.ByteBuffer; - -/** - * Decorator for {@link ByteBuffer} with a synchronized access for it. - */ -final class BufferContainerImpl implements BufferContainer { - - private static final Logger LOGGER = LoggerFactory.getLogger(BufferContainerImpl.class); - private final long id; - private final ByteBuffer buffer; - - BufferContainerImpl(long id, ByteBuffer buffer) { - this.id = id; - this.buffer = buffer; - } - - @Override - public long id() { - return id; - } - - @Override - public synchronized ByteBuffer buffer() { - return buffer; - } - - @Override - public String toString() { - return "BufferContainer{" + "buffer=" + buffer + ", id=" + id + '}'; - } - - @Override - public boolean isStub() { - LOGGER.debug("id <{}>", id); - return false; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainerStub.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainerStub.java deleted file mode 100644 index 402f5d2..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferContainerStub.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import java.nio.ByteBuffer; - -/** - * Stub implementation of the {@link BufferContainer}. - */ -final class BufferContainerStub implements BufferContainer { - - BufferContainerStub() { - - } - - @Override - public long id() { - throw new IllegalStateException("BufferContainerStub does not have an id!"); - } - - @Override - public ByteBuffer buffer() { - throw new IllegalStateException("BufferContainerStub does not allow access to the buffer!"); - } - - @Override - public boolean isStub() { - return true; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLease.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferLease.java deleted file mode 100644 index b0c80ad..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLease.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import java.nio.ByteBuffer; - -/** - * BufferLease is a decorator for {@link BufferContainer} with reference counter - */ -public interface BufferLease { - - /** - * @return identity of the decorated {@link BufferContainer}. - */ - long id(); - - /** - * @return current reference count. - */ - long refs(); - - /** - * @return encapsulated buffer of the {@link BufferContainer}. - */ - ByteBuffer buffer(); - - /** - * Add reference, throws {@link IllegalStateException} if lease has expired. - */ - void addRef() throws IllegalStateException; - - /** - * Remove reference, throws {@link IllegalStateException} if lease has expired. - */ - void removeRef() throws IllegalStateException; - - /** - * @return status of the lease, {@code true} indicates that the lease has expired. - */ - boolean isTerminated(); - - /** - * @return is this a stub implementation. - */ - boolean isStub(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeaseImpl.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeaseImpl.java deleted file mode 100644 index fcd8b35..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeaseImpl.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import java.nio.ByteBuffer; -import java.util.concurrent.Phaser; - -/** - * Decorator for {@link BufferContainer} that automatically clears (frees) the encapsulated {@link ByteBuffer} and - * returns the {@link BufferContainer} to {@link BufferLeasePool} when reference count hits zero. Starts with one - * initial reference. Internally uses a {@link Phaser} to track reference count in a non-blocking way. - */ -final class BufferLeaseImpl implements BufferLease { - - private final BufferContainer bufferContainer; - private final Phaser phaser; - private final BufferLeasePool bufferLeasePool; - - BufferLeaseImpl(BufferContainer bc, BufferLeasePool bufferLeasePool) { - this.bufferContainer = bc; - this.bufferLeasePool = bufferLeasePool; - - // initial registered parties set to 1 - this.phaser = new ClearingPhaser(1); - } - - @Override - public long id() { - return bufferContainer.id(); - } - - @Override - public long refs() { - // initial number of registered parties is 1 - return phaser.getRegisteredParties(); - } - - @Override - public ByteBuffer buffer() { - if (phaser.isTerminated()) { - throw new IllegalStateException( - "Cannot return wrapped ByteBuffer, BufferLease phaser was already terminated!" - ); - } - return bufferContainer.buffer(); - } - - @Override - public void addRef() { - if (phaser.register() < 0) { - throw new IllegalStateException("Cannot add reference, BufferLease phaser was already terminated!"); - } - } - - @Override - public void removeRef() { - if (phaser.arriveAndDeregister() < 0) { - throw new IllegalStateException("Cannot remove reference, BufferLease phaser was already terminated!"); - } - } - - @Override - public boolean isTerminated() { - return phaser.isTerminated(); - } - - @Override - public boolean isStub() { - return bufferContainer.isStub(); - } - - /** - * Phaser that clears the buffer on termination (registeredParties=0) - */ - private class ClearingPhaser extends Phaser { - - public ClearingPhaser(int i) { - super(i); - } - - @Override - protected boolean onAdvance(int phase, int registeredParties) { - boolean rv = false; - if (registeredParties == 0) { - buffer().clear(); - bufferLeasePool.internalOffer(bufferContainer); - rv = true; - } - return rv; - } - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeasePool.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeasePool.java deleted file mode 100644 index b179cff..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeasePool.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.ByteBuffer; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Supplier; - -/** - * Non-blocking pool for {@link BufferContainer} objects. All objects in the pool are {@link ByteBuffer#clear()}ed - * before returning to the pool by {@link BufferLease}. - */ -public final class BufferLeasePool { - // TODO create tests - - private static final Logger LOGGER = LoggerFactory.getLogger(BufferLeasePool.class); - - private final Supplier byteBufferSupplier; - - private final ConcurrentLinkedQueue queue; - - private final BufferLease bufferLeaseStub; - private final BufferContainer bufferContainerStub; - private final AtomicBoolean close; - - private final int segmentSize; - - private final AtomicLong bufferId; - - private final Lock lock; - - // TODO check locking pattern, addRef in BufferLease can escape offer's check and cause dirty in pool? - public BufferLeasePool() { - this.segmentSize = 4096; - this.byteBufferSupplier = () -> ByteBuffer.allocateDirect(segmentSize); // TODO configurable extents - this.queue = new ConcurrentLinkedQueue<>(); - this.bufferLeaseStub = new BufferLeaseStub(); - this.bufferContainerStub = new BufferContainerStub(); - this.close = new AtomicBoolean(); - this.bufferId = new AtomicLong(); - this.lock = new ReentrantLock(); - } - - private BufferLease take() { - // get or create - BufferContainer bufferContainer = queue.poll(); - BufferLease bufferLease; - if (bufferContainer == null) { - // if queue is empty or stub object, create a new BufferContainer and BufferLease. - bufferLease = new BufferLeaseImpl( - new BufferContainerImpl(bufferId.incrementAndGet(), byteBufferSupplier.get()), - this - ); - } - else { - // otherwise, wrap bufferContainer with phaser decorator (bufferLease) - bufferLease = new BufferLeaseImpl(bufferContainer, this); - } - - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "returning bufferLease id <{}> with refs <{}> at buffer position <{}>", bufferLease.id(), - bufferLease.refs(), bufferLease.buffer().position() - ); - } - - if (bufferLease.buffer().position() != 0) { - throw new IllegalStateException("Dirty buffer in pool, terminating!"); - } - - return bufferLease; - - } - - /** - * @param size minimum size of the {@link BufferLease}s requested. - * @return list of {@link BufferLease}s meeting or exceeding the size requested. - */ - public List take(long size) { - if (close.get()) { - return Collections.singletonList(bufferLeaseStub); - } - - LOGGER.debug("requesting take with size <{}>", size); - long currentSize = 0; - List bufferLeases = new LinkedList<>(); - while (currentSize < size) { - BufferLease bufferLease = take(); - bufferLeases.add(bufferLease); - currentSize = currentSize + bufferLease.buffer().capacity(); - - } - return bufferLeases; - - } - - /** - * return {@link BufferContainer} into the pool. - * - * @param bufferContainer {@link BufferContainer} from {@link BufferLease} which has been - * {@link ByteBuffer#clear()}ed. - */ - void internalOffer(BufferContainer bufferContainer) { - // Add buffer back to pool if it is not a stub object - if (!bufferContainer.isStub()) { - queue.add(bufferContainer); - } - - if (close.get()) { - LOGGER.debug("closing in offer"); - while (!queue.isEmpty()) { - if (lock.tryLock()) { - queue.clear(); - lock.unlock(); - } - else { - break; - } - } - } - if (LOGGER.isDebugEnabled()) { - long queueSegments = queue.size(); - long queueBytes = queueSegments * segmentSize; - LOGGER.debug("offer complete, queueSegments <{}>, queueBytes <{}>", queueSegments, queueBytes); - } - } - - /** - * Closes the {@link BufferLeasePool}, deallocating currently residing {@link BufferContainer}s and future ones when - * returned. - */ - public void close() { - LOGGER.debug("close called"); - close.set(true); - - // close all that are in the pool right now - internalOffer(bufferContainerStub); - - } - - /** - * Estimate the pool size, due to non-blocking nature of the pool, this is only an estimate. - * - * @return estimate of the pool size, counting only the residing buffers. - */ - public int estimatedSize() { - return queue.size(); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeaseStub.java b/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeaseStub.java deleted file mode 100644 index a2fa685..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/BufferLeaseStub.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer; - -import java.nio.ByteBuffer; - -/** - * Stub implementation of the {@link BufferLease} - */ -final class BufferLeaseStub implements BufferLease { - - BufferLeaseStub() { - - } - - @Override - public long id() { - throw new IllegalStateException("BufferLeaseStub does not have an id!"); - } - - @Override - public long refs() { - throw new IllegalStateException("BufferLeaseStub does not have refs!"); - } - - @Override - public ByteBuffer buffer() { - throw new IllegalStateException("BufferLeaseStub does not have a buffer!"); - } - - @Override - public void addRef() { - throw new IllegalStateException("BufferLeaseStub does not allow adding refs!"); - } - - @Override - public void removeRef() { - throw new IllegalStateException("BufferLeaseStub does not allow removing refs!"); - } - - @Override - public boolean isTerminated() { - throw new IllegalStateException("BufferLeaseStub does not have ref count!"); - } - - @Override - public boolean isStub() { - return true; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/access/Access.java b/src/main/java/com/teragrep/net_01/channel/buffer/access/Access.java deleted file mode 100644 index 2d40749..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/access/Access.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.access; - -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Supplier; - -public final class Access implements Supplier { - - private long accessCount; - private boolean terminated; - private final Lock lock; - - public Access() { - this.accessCount = 0; - this.terminated = false; - this.lock = new ReentrantLock(); - } - - @Override - public Lease get() { - lock.lock(); - try { - if (terminated()) { - throw new IllegalStateException("Access already terminated"); - } - - accessCount++; - return new Lease(this); - } - finally { - lock.unlock(); - } - } - - public void terminate() { - if (lock.tryLock()) { - try { - if (accessCount != 0) { - throw new IllegalStateException("Open leases still exist"); - } - else { - if (terminated) { - throw new IllegalStateException("Access already terminated"); - } - terminated = true; - } - } - finally { - lock.unlock(); - } - } - else { - throw new IllegalStateException("Lease operation in progress"); - } - } - - public boolean terminated() { - lock.lock(); - try { - return terminated; - } - finally { - lock.unlock(); - } - } - - public void release(Lease lease) { - if (lease.isOpen()) { - throw new IllegalStateException("Can not be release an open lease"); - } - lock.lock(); - try { - long newAccessCount = accessCount - 1; - if (newAccessCount < 0) { - throw new IllegalStateException("AccessCount must not be negative"); - } - accessCount = newAccessCount; - } - finally { - lock.unlock(); - } - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/access/Lease.java b/src/main/java/com/teragrep/net_01/channel/buffer/access/Lease.java deleted file mode 100644 index 40ac9a0..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/access/Lease.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.access; - -public final class Lease implements AutoCloseable { - - private final Access access; - - private volatile boolean isOpen; - - Lease(Access access) { - this.access = access; - this.isOpen = true; - } - - @Override - public void close() { - if (!isOpen) { - throw new IllegalStateException(); - } - else { - isOpen = false; - access.release(this); - } - } - - public boolean isOpen() { - return isOpen; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/Writeable.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/Writeable.java deleted file mode 100644 index 43e00ce..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/Writeable.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import java.io.Closeable; -import java.nio.ByteBuffer; - -public interface Writeable extends Closeable { - - @Override - void close(); - - ByteBuffer[] buffers(); - - boolean hasRemaining(); - - boolean isStub(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableAccess.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableAccess.java deleted file mode 100644 index 3973ca2..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableAccess.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import com.teragrep.net_01.channel.buffer.access.Access; -import com.teragrep.net_01.channel.buffer.access.Lease; - -import java.nio.ByteBuffer; - -public final class WriteableAccess implements Writeable { - - private final Writeable writeable; - private final Access access; - - public WriteableAccess(Writeable writeable, Access access) { - this.writeable = writeable; - this.access = access; - } - - @Override - public ByteBuffer[] buffers() { - // FIXME just not right - try (Lease ignored = access.get()) { - return writeable.buffers(); - } - } - - @Override - public boolean hasRemaining() { - try (Lease ignored = access.get()) { - return writeable.hasRemaining(); - } - } - - @Override - public boolean isStub() { - try (Lease ignored = access.get()) { - return writeable.isStub(); - } - } - - @Override - public void close() { - try (Lease ignored = access.get()) { - writeable.close(); - } - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableClosure.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableClosure.java deleted file mode 100644 index 37c73a0..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableClosure.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import com.teragrep.net_01.channel.context.EstablishedContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.ByteBuffer; - -/** - * Closes a connection at close() - */ -public final class WriteableClosure implements Writeable { - - private static final Logger LOGGER = LoggerFactory.getLogger(WriteableClosure.class); - - private final Writeable writeable; - private final EstablishedContext establishedContext; - - public WriteableClosure(Writeable writeable, EstablishedContext establishedContext) { - this.writeable = writeable; - this.establishedContext = establishedContext; - } - - @Override - public void close() { - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Sent command <{}>, Closing connection to PeerAddress <{}> PeerPort <{}>", "serverclose", - establishedContext.socket().getTransportInfo().getPeerAddress(), - establishedContext.socket().getTransportInfo().getPeerPort() - ); - } - establishedContext.close(); - writeable.close(); - } - - @Override - public ByteBuffer[] buffers() { - return writeable.buffers(); - } - - @Override - public boolean hasRemaining() { - return writeable.hasRemaining(); - } - - @Override - public boolean isStub() { - return writeable.isStub(); - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableInvalidation.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableInvalidation.java deleted file mode 100644 index 47694a7..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableInvalidation.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import java.nio.ByteBuffer; - -/** - * Decoration of {@link Writeable} Invalidates a writable so that {@link ByteBuffer}s returned from - * {@link Writeable#buffers()} have position and limit set to zero. Because ByteBuffer can not be Decorated directly - * this is only viable alternative to best-effort invalidate access to it. - */ -public final class WriteableInvalidation implements Writeable { - - private final Writeable writeable; - - public WriteableInvalidation(Writeable writeable) { - this.writeable = writeable; - } - - @Override - public void close() { - for (ByteBuffer byteBuffer : buffers()) { - byteBuffer.limit(0); - } - writeable.close(); - } - - @Override - public ByteBuffer[] buffers() { - return writeable.buffers(); - } - - @Override - public boolean hasRemaining() { - return writeable.hasRemaining(); - } - - @Override - public boolean isStub() { - return writeable.isStub(); - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableLeaseful.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableLeaseful.java deleted file mode 100644 index eb2871b..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableLeaseful.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import com.teragrep.net_01.channel.buffer.BufferLease; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.ByteBuffer; -import java.util.List; - -public final class WriteableLeaseful implements Writeable { - - private static final Logger LOGGER = LoggerFactory.getLogger(WriteableLeaseful.class); - - private final Writeable writeable; - private final List leases; - - public WriteableLeaseful(Writeable writeable, List leases) { - this.writeable = writeable; - this.leases = leases; - } - - @Override - public ByteBuffer[] buffers() { - return writeable.buffers(); - } - - @Override - public boolean hasRemaining() { - return writeable.hasRemaining(); - } - - @Override - public boolean isStub() { - return writeable.isStub(); - } - - @Override - public void close() { - writeable.close(); - // TODO subleases for fragments - for (BufferLease bufferLease : leases) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("releasing id <{}> with refs <{}>", bufferLease.id(), bufferLease.refs()); - } - bufferLease.removeRef(); - } - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableStub.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableStub.java deleted file mode 100644 index 263cc38..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/WriteableStub.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import java.nio.ByteBuffer; - -public final class WriteableStub implements Writeable { - - @Override - public ByteBuffer[] buffers() { - throw new UnsupportedOperationException("WriteableStub does not allow this method"); - } - - @Override - public boolean hasRemaining() { - throw new UnsupportedOperationException("WriteableStub does not allow this method"); - } - - @Override - public boolean isStub() { - return true; - } - - @Override - public void close() { - throw new UnsupportedOperationException("WriteableStub does not allow this method"); - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/buffer/writable/Writeables.java b/src/main/java/com/teragrep/net_01/channel/buffer/writable/Writeables.java deleted file mode 100644 index f7bb925..0000000 --- a/src/main/java/com/teragrep/net_01/channel/buffer/writable/Writeables.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.buffer.writable; - -import java.nio.ByteBuffer; - -public final class Writeables implements Writeable { - - private final Writeable[] writeables; - - public Writeables(Writeable ... writeables) { - this.writeables = writeables; - } - - @Override - public ByteBuffer[] buffers() { - long totalBuffers = 0; - for (Writeable writeable : writeables) { - totalBuffers = totalBuffers + writeable.buffers().length; - } - - if (totalBuffers > Integer.MAX_VALUE) { - throw new IllegalArgumentException( - "Too many writeable buffers, exceeded Integer.MAX_VALUE <" + Integer.MAX_VALUE + ">" - ); - } - - ByteBuffer[] bufferArray = new ByteBuffer[(int) totalBuffers]; - int written = 0; - for (final Writeable writeable : writeables) { - int bufferLength = writeable.buffers().length; - System.arraycopy(writeable.buffers(), 0, bufferArray, written, bufferLength); - written += bufferLength; - } - - return bufferArray; - } - - @Override - public boolean hasRemaining() { - boolean hasRemaining = false; - for (Writeable writeable : writeables) { - if (writeable.hasRemaining()) { - hasRemaining = true; - break; - } - } - return hasRemaining; - } - - @Override - public boolean isStub() { - return false; - } - - @Override - public void close() { - for (Writeable writeable : writeables) { - writeable.close(); - } - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/Clock.java b/src/main/java/com/teragrep/net_01/channel/context/Clock.java deleted file mode 100644 index a835ba6..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/Clock.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.buffer.BufferLease; - -public interface Clock extends AutoCloseable { - - /** - * @param bufferLease to be consumed by the Clock. IMPORTANT: current tls implementation will not work properly if - * {@link BufferLease} is not fully consumed. - */ - void advance(BufferLease bufferLease); -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/ClockFactory.java b/src/main/java/com/teragrep/net_01/channel/context/ClockFactory.java deleted file mode 100644 index 6e7a5cd..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/ClockFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -public interface ClockFactory { - - Clock create(EstablishedContext establishedContext); -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/ConnectContext.java b/src/main/java/com/teragrep/net_01/channel/context/ConnectContext.java deleted file mode 100644 index ab45445..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/ConnectContext.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.SocketFactory; -import com.teragrep.net_01.eventloop.EventLoop; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.nio.channels.SelectionKey; -import java.nio.channels.SocketChannel; -import java.util.concurrent.ExecutorService; -import java.util.function.Consumer; - -/** - * Initiate type {@link Context} that produces an EstablishedContext once it receives an OP_CONNECT type - * {@link SelectionKey} from {@link EventLoop} and socketChannel.finishConnect() succeeds. Use - * {@link EventLoop#register(Context)} to register it to the desired {@link EventLoop}, - */ -public final class ConnectContext implements Context { - // TODO should this be named InitiateContext? - - private static final Logger LOGGER = LoggerFactory.getLogger(ConnectContext.class); - - private final SocketChannel socketChannel; - private final ExecutorService executorService; - private final SocketFactory socketFactory; - private final ClockFactory clockFactory; - - private final Consumer establishedContextConsumer; - - ConnectContext( - SocketChannel socketChannel, - ExecutorService executorService, - SocketFactory socketFactory, - ClockFactory clockFactory, - Consumer establishedContextConsumer - ) { - this.socketChannel = socketChannel; - this.executorService = executorService; - this.socketFactory = socketFactory; - this.establishedContextConsumer = establishedContextConsumer; - this.clockFactory = clockFactory; - } - - public SocketChannel socketChannel() { - return socketChannel; - } - - @Override - public int initialSelectionKey() { - return SelectionKey.OP_CONNECT; - } - - @Override - public void handleEvent(SelectionKey selectionKey) { - if (selectionKey.isConnectable()) { - try { - if (!socketChannel.finishConnect()) { - // not yet complete - return; - } - } - catch (IOException ioException) { - LOGGER.warn("socketChannel <{}> finishConnect threw, closing", socketChannel, ioException); - close(); - return; - } - - // No need to be longer interested in connect. - selectionKey.interestOps(selectionKey.interestOps() & ~SelectionKey.OP_CONNECT); - - InterestOps interestOps = new InterestOpsImpl(selectionKey); - - EstablishedContext establishedContext = new EstablishedContextImpl( - executorService, - socketFactory.create(socketChannel), - interestOps - ); - - selectionKey.attach(establishedContext); - - establishedContext.ingress().register(clockFactory.create(establishedContext)); - - LOGGER.debug("establishedContext <{}>", establishedContext); - establishedContextConsumer.accept(establishedContext); - } - } - - @Override - public void close() { - try { - socketChannel.close(); - } - catch (IOException ioException) { - LOGGER.warn("socketChannel <{}> close threw", socketChannel, ioException); - } - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/ConnectContextFactory.java b/src/main/java/com/teragrep/net_01/channel/context/ConnectContextFactory.java deleted file mode 100644 index 5ecb140..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/ConnectContextFactory.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.Socket; -import com.teragrep.net_01.channel.socket.SocketFactory; -import com.teragrep.net_01.eventloop.EventLoop; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.channels.SocketChannel; -import java.util.concurrent.ExecutorService; -import java.util.function.Consumer; - -/** - * Factory for creating {@link ConnectContext}s for initiating new connections. - */ -public final class ConnectContextFactory { - - private final ExecutorService executorService; - private final SocketFactory socketFactory; - - /** - * @param executorService {@link ExecutorService} to handle connection's events with - * @param socketFactory {@link SocketFactory} that produces the desired type {@link Socket} for the connection. - */ - public ConnectContextFactory(ExecutorService executorService, SocketFactory socketFactory) { - this.executorService = executorService; - this.socketFactory = socketFactory; - } - - /** - * @param inetSocketAddress {@link InetSocketAddress} to initiate connection to. - * @param clockFactory {@link ClockFactory} for processing received data with. - * @param establishedContextConsumer {@link Consumer} of {@link EstablishedContext} for handling the callback once - * connection is established. - * @return {@link ConnectContext} to be registered with {@link EventLoop#register(Context)}. - * @throws IOException if underlying {@link SocketChannel} is unable to initiate the connection. - */ - public ConnectContext create( - InetSocketAddress inetSocketAddress, - ClockFactory clockFactory, - Consumer establishedContextConsumer - ) throws IOException { - SocketChannel socketChannel = SocketChannel.open(); - try { - socketChannel.socket().setKeepAlive(true); - socketChannel.configureBlocking(false); - socketChannel.connect(inetSocketAddress); - } - catch (IOException ioException) { - socketChannel.close(); - throw ioException; - } - - return new ConnectContext( - socketChannel, - executorService, - socketFactory, - clockFactory, - establishedContextConsumer - ); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/Context.java b/src/main/java/com/teragrep/net_01/channel/context/Context.java deleted file mode 100644 index 587c660..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/Context.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import java.io.Closeable; -import java.nio.channels.SelectionKey; -import java.nio.channels.spi.AbstractSelectableChannel; - -/** - * Context is a network connection element, it may be one of the: initiate {@link ConnectContext}, listen - * {@link ListenContext} or established {@link EstablishedContext} types. - */ -public interface Context extends Closeable { - - /** - * Handles this context's {@link SelectionKey} events. Providing a non-related key will result in non-foreseen - * issues, and this is a programming error. - * - * @param selectionKey key of this context to handle an event for. - */ - void handleEvent(SelectionKey selectionKey); - - /** - * Closes the underlying network connection element and frees resources attached to it. - */ - @Override - void close(); // no exception is thrown - - /** - * @return {@link AbstractSelectableChannel} of the network connection element. - */ - AbstractSelectableChannel socketChannel(); - - /** - * @return initial state of the {@link SelectionKey} which the network connection element starts with. - */ - int initialSelectionKey(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/Egress.java b/src/main/java/com/teragrep/net_01/channel/context/Egress.java deleted file mode 100644 index 567ee38..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/Egress.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.buffer.writable.Writeable; - -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Consumer; - -/** - * Egress {@link com.teragrep.rlp_03.frame.RelpFrame} are handled by this - */ -public interface Egress extends Consumer, Runnable { - - /** - * Sends asynchronously the writeable provided. Implementation is required to be thread-safe. - * - * @param writeable to send - */ - @Override - void accept(Writeable writeable); - - @Override - void run(); - - AtomicBoolean needRead(); - - int outstanding(); - -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/EgressImpl.java b/src/main/java/com/teragrep/net_01/channel/context/EgressImpl.java deleted file mode 100644 index f874de5..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/EgressImpl.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.buffer.writable.Writeable; -import com.teragrep.net_01.channel.buffer.writable.WriteableStub; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import tlschannel.NeedsReadException; -import tlschannel.NeedsWriteException; - -import java.io.IOException; - -import java.nio.ByteBuffer; -import java.nio.channels.CancelledKeyException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import static java.nio.channels.SelectionKey.OP_READ; -import static java.nio.channels.SelectionKey.OP_WRITE; - -final class EgressImpl implements Egress { - - private static final Logger LOGGER = LoggerFactory.getLogger(EgressImpl.class); - - private final EstablishedContext establishedContext; - - private final ConcurrentLinkedQueue queue; - private final ArrayList writeInProgressList; // lock protected - - private final Lock lock; - - // tls - private final AtomicBoolean needRead; - - private final List toWriteList; - - EgressImpl(EstablishedContext establishedContext) { - this.establishedContext = establishedContext; - this.queue = new ConcurrentLinkedQueue<>(); - this.writeInProgressList = new ArrayList<>(); - this.lock = new ReentrantLock(); - this.needRead = new AtomicBoolean(); - - this.toWriteList = new ArrayList<>(); - } - - // this must be thread-safe! - @Override - public void accept(Writeable writeable) { - - if (!writeable.isStub()) { - queue.add(writeable); - } - - while (queue.peek() != null) { - if (lock.tryLock()) { - try { - while (true) { - Writeable w = queue.poll(); - if (w != null) { - if (LOGGER.isTraceEnabled()) { - LOGGER - .trace( - "adding writable to toWriteList.size <{}>, writeInProgressList.size <{}>", - toWriteList.size(), writeInProgressList.size() - ); - } - toWriteList.add(w); - } - else { - break; - } - } - - if (toWriteList.isEmpty()) { - break; - } - - try { - transmit(toWriteList); - } - catch (CancelledKeyException cke) { - LOGGER - .warn( - "CancelledKeyException <{}>. Closing connection for PeerAddress <{}> PeerPort <{}>", - cke.getMessage(), - establishedContext.socket().getTransportInfo().getPeerAddress(), - establishedContext.socket().getTransportInfo().getPeerPort() - ); - establishedContext.close(); - return; - } - catch (IOException ioException) { - LOGGER - .error( - "IOException <{}> while writing to socket. PeerAddress <{}> PeerPort <{}>", - ioException, establishedContext.socket().getTransportInfo().getPeerAddress(), - establishedContext.socket().getTransportInfo().getPeerPort() - ); - establishedContext.close(); - return; - } - - } - finally { - lock.unlock(); - } - } - else { - break; - } - } - } - - private void transmit(final List toWriteList) throws IOException { - - try { - - int numberOfBuffers = 0; - Iterator toWriteIterator = toWriteList.iterator(); - while (toWriteIterator.hasNext()) { - Writeable w = toWriteIterator.next(); - numberOfBuffers += w.buffers().length; - } - - ByteBuffer[] writeBuffers = new ByteBuffer[numberOfBuffers]; - int writeBuffersIndex = 0; - - Iterator toWriteIterator2 = toWriteList.iterator(); - while (toWriteIterator2.hasNext()) { - Writeable w = toWriteIterator2.next(); - - for (ByteBuffer buffer : w.buffers()) { - writeBuffers[writeBuffersIndex] = buffer; - writeBuffersIndex++; - } - - toWriteIterator2.remove(); - writeInProgressList.add(w); - } - - establishedContext.socket().write(writeBuffers); - - // remove written ones - Iterator writeableIterator = writeInProgressList.iterator(); - while (writeableIterator.hasNext()) { - Writeable w = writeableIterator.next(); - if (!w.hasRemaining()) { - LOGGER.debug("complete write, closing written writeable"); - w.close(); - writeableIterator.remove(); - } - else { - LOGGER.debug("writable has still buffers, breaking"); - break; - } - } - } - catch (NeedsReadException nre) { - needRead.set(true); - establishedContext.interestOps().add(OP_READ); - } - catch (NeedsWriteException nwe) { - establishedContext.interestOps().add(OP_WRITE); - } - } - - @Override - public void run() { - accept(new WriteableStub()); - } - - @Override - public AtomicBoolean needRead() { - return needRead; - } - - @Override - public int outstanding() { - return queue.size(); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/EndOfStreamException.java b/src/main/java/com/teragrep/net_01/channel/context/EndOfStreamException.java deleted file mode 100644 index 3beb696..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/EndOfStreamException.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import java.io.IOException; - -public class EndOfStreamException extends IOException { - - public EndOfStreamException() { - super(); - } - - public EndOfStreamException(String message) { - super(message); - } - - public EndOfStreamException(String message, Throwable cause) { - super(message, cause); - } - - public EndOfStreamException(Throwable cause) { - super(cause); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/EstablishedContext.java b/src/main/java/com/teragrep/net_01/channel/context/EstablishedContext.java deleted file mode 100644 index 3c221f6..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/EstablishedContext.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.buffer.writable.Writeable; -import com.teragrep.net_01.channel.socket.Socket; - -/** - * Established type of {@link Context}. It produces ingress data into the provided - * {@link com.teragrep.rlp_03.frame.delegate.FrameDelegate} via {@link Ingress}. Egress data can be written via - * {@link Egress#accept(Writeable)}. - */ -public interface EstablishedContext extends Context { - - /** - * @return current {@link InterestOps} of the underlying {@link java.nio.channels.SelectionKey}. - */ - InterestOps interestOps(); - - /** - * @return underlying {@link Socket} of the connection. - */ - Socket socket(); - - /** - * @return Ingress of the connection - */ - Ingress ingress(); - - /** - * @return Egress of the connection for sending egress data. - */ - Egress egress(); - - boolean isStub(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/EstablishedContextImpl.java b/src/main/java/com/teragrep/net_01/channel/context/EstablishedContextImpl.java deleted file mode 100644 index ab3ee6f..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/EstablishedContextImpl.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.Socket; -import com.teragrep.net_01.channel.buffer.BufferLeasePool; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.nio.channels.CancelledKeyException; -import java.nio.channels.SelectionKey; -import java.nio.channels.spi.AbstractSelectableChannel; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.RejectedExecutionException; - -import static java.nio.channels.SelectionKey.OP_READ; -import static java.nio.channels.SelectionKey.OP_WRITE; - -/** - * Implementation of the {@link EstablishedContext} - */ -final class EstablishedContextImpl implements EstablishedContext { - - private static final Logger LOGGER = LoggerFactory.getLogger(EstablishedContextImpl.class); - - private final ExecutorService executorService; - private final Socket socket; - private final InterestOps interestOps; - - private final BufferLeasePool bufferLeasePool; - private final Ingress ingress; - private final Egress egress; - - EstablishedContextImpl(ExecutorService executorService, Socket socket, InterestOps interestOps) { - this.interestOps = interestOps; - this.executorService = executorService; - this.socket = socket; - - this.bufferLeasePool = new BufferLeasePool(); - this.ingress = new IngressImpl(this, this.bufferLeasePool); - this.egress = new EgressImpl(this); - - } - - @Override - public void close() { - try { - interestOps.removeAll(); - } - catch (CancelledKeyException cke) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("CancelledKeyException <{}> in close", cke.getMessage()); - } - } - - try { - ingress.close(); - } - catch (Exception exception) { - LOGGER.warn("FrameDelegate close threw exception <{}>", exception.getMessage()); - } - - try { - socket.close(); - } - catch (IOException ioe) { - LOGGER.warn("IOException <{}> in close", ioe.getMessage()); - } - - bufferLeasePool.close(); - } - - @Override - public AbstractSelectableChannel socketChannel() { - return socket.socketChannel(); - } - - @Override - public int initialSelectionKey() { - return OP_READ; - } - - @Override - public void handleEvent(SelectionKey selectionKey) { - // TODO refactor this into Strategy pattern - if (!socket.getTransportInfo().getEncryptionInfo().isEncrypted()) { - // plain connection, reads are reads, writes are writes - - if (selectionKey.isReadable()) { // perhaps track read/write needs here per direction too - LOGGER.debug("handleEvent taking read"); - try { - interestOps.remove(OP_READ); - } - catch (CancelledKeyException cke) { - LOGGER - .warn( - "CancelledKeyException <{}> in handleEvent. Closing connection for PeerAddress <{}> PeerPort <{}>", - cke.getMessage(), socket.getTransportInfo().getPeerAddress(), - socket.getTransportInfo().getPeerPort() - ); - close(); - return; - } - LOGGER.debug("handleEvent submitting new runnable for read"); - try { - executorService.submit(ingress); - } - catch (RejectedExecutionException ree) { - LOGGER.error("executorService.submit threw <{}> for read", ree.getMessage()); - throw ree; - } - LOGGER.debug("handleEvent exiting read"); - } - - if (selectionKey.isWritable()) { - LOGGER.debug("handleEvent taking write"); - try { - interestOps.remove(OP_WRITE); - } - catch (CancelledKeyException cke) { - LOGGER - .warn( - "CancelledKeyException <{}> in handleEvent. Closing connection for PeerAddress <{}> PeerPort <{}>", - cke.getMessage(), socket.getTransportInfo().getPeerAddress(), - socket.getTransportInfo().getPeerPort() - ); - close(); - return; - } - LOGGER.debug("handleEvent submitting new runnable for write"); - try { - executorService.submit(egress); - LOGGER.debug("submitted write!"); - } - catch (RejectedExecutionException ree) { - LOGGER.error("executorService.submit threw <{}> for write", ree.getMessage()); - throw ree; - } - LOGGER.debug("handleEvent exiting write"); - } - } - else { - // encrypted connections, reads may need writes too and vice versa - if (selectionKey.isReadable()) { - try { - interestOps.remove(OP_READ); - } - catch (CancelledKeyException cke) { - LOGGER - .warn( - "CancelledKeyException <{}> in handleEvent. Closing connection for PeerAddress <{}> PeerPort <{}>", - cke.getMessage(), socket.getTransportInfo().getPeerAddress(), - socket.getTransportInfo().getPeerPort() - ); - close(); - return; - } - // socket write may be pending a tls read - if (egress.needRead().compareAndSet(true, false)) { - executorService.submit(egress); - } - - // read anyway - executorService.submit(ingress); - } - - if (selectionKey.isWritable()) { - try { - interestOps.remove(OP_WRITE); - } - catch (CancelledKeyException cke) { - LOGGER - .warn( - "CancelledKeyException <{}> in handleEvent. Closing connection for PeerAddress <{}> PeerPort <{}>", - cke.getMessage(), socket.getTransportInfo().getPeerAddress(), - socket.getTransportInfo().getPeerPort() - ); - close(); - return; - } - if (ingress.needWrite().compareAndSet(true, false)) { - // socket read may be pending a tls write - executorService.submit(ingress); - } - - // write anyway - executorService.submit(egress); - } - } - } - - @Override - public InterestOps interestOps() { - return interestOps; - } - - @Override - public Socket socket() { - return socket; - } - - @Override - public Ingress ingress() { - return ingress; - } - - @Override - public Egress egress() { - return egress; - } - - @Override - public boolean isStub() { - return false; - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/EstablishedContextStub.java b/src/main/java/com/teragrep/net_01/channel/context/EstablishedContextStub.java deleted file mode 100644 index 1f9e039..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/EstablishedContextStub.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.Socket; - -import java.nio.channels.SelectionKey; -import java.nio.channels.spi.AbstractSelectableChannel; - -/** - * Stub implementation of {@link EstablishedContext} - */ -final class EstablishedContextStub implements EstablishedContext { - - EstablishedContextStub() { - - } - - @Override - public void close() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public AbstractSelectableChannel socketChannel() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public int initialSelectionKey() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public void handleEvent(SelectionKey selectionKey) { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public InterestOps interestOps() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public Socket socket() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public Ingress ingress() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public Egress egress() { - throw new IllegalArgumentException("EstablishedContextStub does not implement this"); - } - - @Override - public boolean isStub() { - return true; - } - -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/Ingress.java b/src/main/java/com/teragrep/net_01/channel/context/Ingress.java deleted file mode 100644 index dcf674b..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/Ingress.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * Ingress {@link com.teragrep.rlp_03.frame.RelpFrame} are handled by this asynchronously. - */ -public interface Ingress extends Runnable { - - @Override - void run(); - - AtomicBoolean needWrite(); - - void register(Clock clock); - - void unregister(Clock clock); - - void close() throws Exception; - -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/IngressImpl.java b/src/main/java/com/teragrep/net_01/channel/context/IngressImpl.java deleted file mode 100644 index e97c40a..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/IngressImpl.java +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.buffer.BufferLease; -import com.teragrep.net_01.channel.buffer.BufferLeasePool; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import tlschannel.NeedsReadException; -import tlschannel.NeedsWriteException; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.CancelledKeyException; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import static java.nio.channels.SelectionKey.OP_READ; -import static java.nio.channels.SelectionKey.OP_WRITE; - -final class IngressImpl implements Ingress { - - private static final Logger LOGGER = LoggerFactory.getLogger(IngressImpl.class); - private final EstablishedContextImpl establishedContext; - private final BufferLeasePool bufferLeasePool; - - private final LinkedList activeBuffers; - private final Lock lock; - // tls - public final AtomicBoolean needWrite; - - private final List interestedClocks; - - IngressImpl(EstablishedContextImpl establishedContext, BufferLeasePool bufferLeasePool) { - this.establishedContext = establishedContext; - this.bufferLeasePool = bufferLeasePool; - - this.activeBuffers = new LinkedList<>(); - this.lock = new ReentrantLock(); - this.needWrite = new AtomicBoolean(); - - this.interestedClocks = new LinkedList<>(); - } - - @Override - public void run() { - LOGGER.debug("run entry!"); - lock.lock(); - try { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("run lock! with activeBuffers.size() <{}>", activeBuffers.size()); - } - while (true) { - LOGGER.debug("run loop start"); - - // fill buffers for read - long readBytes = readData(); - - if (!isDataAvailable(readBytes)) { - break; - } - - boolean continueReading = true; - while (!activeBuffers.isEmpty()) { - BufferLease bufferLease = activeBuffers.removeFirst(); - LOGGER - .debug( - "submitting buffer <{}> from activeBuffers <{}> to relpFrame", bufferLease, - activeBuffers - ); - - if (!interestedClocks.isEmpty()) { - for (Clock clock : interestedClocks) { - clock.advance(bufferLease); - } - } - - if (interestedClocks.isEmpty()) { - continueReading = false; - } - - LOGGER.debug("clock returned continueReading <{}>", continueReading); - if (!bufferLease.isTerminated() && bufferLease.buffer().hasRemaining()) { - // return back as it has some remaining - LOGGER.debug("pushBack bufferLease id <{}>", bufferLease.id()); - activeBuffers.push(bufferLease); - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "buffer.buffer <{}>, buffer.buffer().hasRemaining() <{}> returned it to activeBuffers <{}>", - bufferLease.buffer(), bufferLease.buffer().hasRemaining(), activeBuffers - ); - } - } - if (!continueReading) { - break; - } - } - if (!continueReading) { - break; - } - } - } - catch (NeedsReadException nre) { - LOGGER.debug("need read", nre); - try { - establishedContext.interestOps().add(OP_READ); - } - catch (CancelledKeyException cke) { - LOGGER.debug("Connection already closed for need read.", cke); - establishedContext.close(); - } - catch (Throwable t) { - LOGGER.error("unexpected error while changing socket interest operations to OP_READ", t); - } - } - catch (NeedsWriteException nwe) { - LOGGER.debug("need write", nwe); - needWrite.set(true); - try { - establishedContext.interestOps().add(OP_WRITE); - } - catch (CancelledKeyException cke) { - LOGGER.debug("Connection already closed for need write.", cke); - establishedContext.close(); - } - catch (Throwable t) { - LOGGER.error("unexpected error while changing socket interest operations to OP_WRITE", t); - } - } - catch (EndOfStreamException eose) { - // close connection - try { - LOGGER - .warn( - "End of stream for PeerAddress <{}> PeerPort <{}>. Closing Connection.", - establishedContext.socket().getTransportInfo().getPeerAddress(), - establishedContext.socket().getTransportInfo().getPeerPort() - ); - } - catch (Exception ignored) { - - } - finally { - establishedContext.close(); - } - } - catch (Throwable t) { - LOGGER.error("run() threw", t); - establishedContext.close(); - } - finally { - lock.unlock(); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("thread <{}> going to pool", Thread.currentThread()); - } - } - } - - private boolean isDataAvailable(long readBytes) throws IOException { - boolean rv; - if (readBytes == 0) { - // socket needs to read more - establishedContext.interestOps().add(OP_READ); - LOGGER.debug("more bytes requested from socket"); - rv = false; - } - else if (readBytes < 0) { - throw new EndOfStreamException("negative readBytes <" + readBytes + ">"); - } - else { - rv = true; - } - return rv; - } - - private long readData() throws IOException { - long readBytes = 0; - - List bufferLeases = bufferLeasePool.take(4); - - List byteBufferList = new LinkedList<>(); - for (BufferLease bufferLease : bufferLeases) { - if (bufferLease.isStub()) { - continue; - } - byteBufferList.add(bufferLease.buffer()); - } - ByteBuffer[] byteBufferArray = byteBufferList.toArray(new ByteBuffer[0]); - - readBytes = establishedContext.socket().read(byteBufferArray); - - activateBuffers(bufferLeases); - - LOGGER.debug("establishedContext.read got <{}> bytes from socket", readBytes); - - return readBytes; - } - - private void activateBuffers(List bufferLeases) { - for (BufferLease bufferLease : bufferLeases) { - if (bufferLease.buffer().position() != 0) { - bufferLease.buffer().flip(); - activeBuffers.add(bufferLease); - } - else { - // unused buffer, releasing back to pool - bufferLease.removeRef(); - } - } - } - - public AtomicBoolean needWrite() { - return needWrite; - } - - @Override - public void register(Clock clock) { - lock.lock(); - try { - if (!interestedClocks.isEmpty()) { - throw new IllegalStateException( - "Unable to register ingress clock, only one interested clock is allowed" - ); - } - interestedClocks.add(clock); - establishedContext.interestOps().add(OP_READ); - } - finally { - lock.unlock(); - } - } - - @Override - public void unregister(Clock clock) { - lock.lock(); - try { - if (!interestedClocks.contains(clock)) { - throw new IllegalStateException("Unable to unregister ingress clock, it is not registered"); - } - interestedClocks.remove(clock); - } - finally { - lock.unlock(); - } - } - - @Override - public void close() throws Exception { - lock.lock(); - try { - for (Clock clock : interestedClocks) { - clock.close(); - } - } - finally { - lock.unlock(); - } - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/InterestOps.java b/src/main/java/com/teragrep/net_01/channel/context/InterestOps.java deleted file mode 100644 index 2b855b6..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/InterestOps.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import java.nio.channels.SelectionKey; - -/** - * Encapsulates {@link SelectionKey} with stateful {@link SelectionKey#interestOps()} for use within {@link Context} - */ -public interface InterestOps { - - void add(int op); - - void remove(int op); - - void removeAll(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/InterestOpsImpl.java b/src/main/java/com/teragrep/net_01/channel/context/InterestOpsImpl.java deleted file mode 100644 index 2e13c1a..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/InterestOpsImpl.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.channels.SelectionKey; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -final class InterestOpsImpl implements InterestOps { - - private static final Logger LOGGER = LoggerFactory.getLogger(InterestOpsImpl.class); - - private final SelectionKey selectionKey; - - private int currentOps; - - private final Lock lock; - - InterestOpsImpl(SelectionKey selectionKey) { - this.selectionKey = selectionKey; - this.currentOps = selectionKey.interestOps(); - this.lock = new ReentrantLock(); - } - - @Override - public void add(int op) { - lock.lock(); - try { - int keysOps = selectionKey.interestOps(); - int newOps = currentOps | op; - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Adding op <{}> to currentOps <{}>, newOps <{}>, keyOps <{}>, validOps <{}>", op, - currentOps, newOps, selectionKey.interestOps(), selectionKey.channel().validOps() - ); - } - currentOps = newOps; - - selectionKey.interestOps(newOps); // CancelledKeyException - - selectionKey.selector().wakeup(); - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Added op <{}>, currentOps <{}>, keyOps <{}>, validOps <{}>", op, currentOps, keysOps, - selectionKey.channel().validOps() - ); - } - } - finally { - lock.unlock(); - } - } - - @Override - public void remove(int op) { - lock.lock(); - try { - int newOps = currentOps & ~op; - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Removing op <{}> from currentOps <{}>, newOps <{}>, keyOps <{}>, validOps <{}>", op, - currentOps, newOps, selectionKey.interestOps(), selectionKey.channel().validOps() - ); - } - currentOps = newOps; - - selectionKey.interestOps(newOps); // CancelledKeyException - - selectionKey.selector().wakeup(); - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Removed op <{}>, currentOps <{}>, keyOps <{}>, validOps <{}>", op, currentOps, - selectionKey.interestOps(), selectionKey.channel().validOps() - ); - } - } - finally { - lock.unlock(); - } - } - - @Override - public void removeAll() { - lock.lock(); - try { - int keysOps = selectionKey.interestOps(); - int newOps = 0; - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Removing all currentOps <{}>, newOps <{}>, keyOps <{}>, validOps <{}>", currentOps, - newOps, keysOps, selectionKey.channel().validOps() - ); - } - - selectionKey.interestOps(newOps); // CancelledKeyException - - selectionKey.selector().wakeup(); - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "Removed all ops. currentOps <{}>, keyOps <{}>, validOps <{}>", currentOps, keysOps, - selectionKey.channel().validOps() - ); - } - } - finally { - lock.unlock(); - } - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/ListenContext.java b/src/main/java/com/teragrep/net_01/channel/context/ListenContext.java deleted file mode 100644 index e72d8fe..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/ListenContext.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.Socket; -import com.teragrep.net_01.channel.socket.SocketFactory; -import com.teragrep.net_01.eventloop.EventLoop; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.net.SocketAddress; -import java.nio.channels.*; -import java.util.concurrent.ExecutorService; - -/** - * Listen type {@link Context} that produces {@link EstablishedContext} for receiving incoming connections. Use - * {@link EventLoop#register(Context)} to register it to the desired {@link EventLoop}. - */ -public final class ListenContext implements Context { - - private static final Logger LOGGER = LoggerFactory.getLogger(ListenContext.class); - private final ServerSocketChannel serverSocketChannel; - private final ExecutorService executorService; - private final SocketFactory socketFactory; - private final ClockFactory clockFactory; - private final EstablishedContextStub establishedContextStub; - - ListenContext( - ServerSocketChannel serverSocketChannel, - ExecutorService executorService, - SocketFactory socketFactory, - ClockFactory clockFactory - ) { - this.serverSocketChannel = serverSocketChannel; - this.executorService = executorService; - this.socketFactory = socketFactory; - this.clockFactory = clockFactory; - this.establishedContextStub = new EstablishedContextStub(); - } - - @Override - public void handleEvent(SelectionKey selectionKey) { - try { - if (selectionKey.isAcceptable()) { - // create the client socket for a newly received connection - - SocketChannel clientSocketChannel = serverSocketChannel.accept(); - - if (LOGGER.isDebugEnabled()) { - // getLocalAddress() can throw so log and ignore as that isn't hard error - try { - SocketAddress localAddress = serverSocketChannel.getLocalAddress(); - SocketAddress remoteAddress = clientSocketChannel.getRemoteAddress(); - LOGGER.debug("ServerSocket <{}> accepting ClientSocket <{}> ", localAddress, remoteAddress); - } - catch (IOException ioException) { - LOGGER.warn("Exception while getAddress", ioException); - } - } - - // tls/plain wrapper - Socket socket = socketFactory.create(clientSocketChannel); - - // non-blocking - clientSocketChannel.configureBlocking(false); - - SelectionKey clientSelectionKey = clientSocketChannel - .register( - selectionKey.selector(), 0, // interestOps: none at this point - establishedContextStub - ); - - InterestOps interestOps = new InterestOpsImpl(clientSelectionKey); - - EstablishedContext establishedContext = new EstablishedContextImpl( - executorService, - socket, - interestOps - ); - - clientSelectionKey.attach(establishedContext); - - establishedContext.ingress().register(clockFactory.create(establishedContext)); - } - } - catch (CancelledKeyException cke) { - // thrown by accessing cancelled SelectionKey - LOGGER.warn("SocketPoll.poll CancelledKeyException caught <{}>", cke.getMessage()); - try { - selectionKey.channel().close(); - } - catch (IOException ignored) { - - } - } - catch (IOException ioException) { - throw new UncheckedIOException(ioException); - } - } - - @Override - public void close() { - if (LOGGER.isDebugEnabled()) { - try { - LOGGER.debug("close serverSocketChannel <{}>", serverSocketChannel.getLocalAddress()); - } - catch (IOException ignored) { - - } - } - - try { - serverSocketChannel.close(); - } - catch (IOException ioException) { - LOGGER.warn("serverSocketChannel <{}> close threw", serverSocketChannel, ioException); - } - - } - - @Override - public ServerSocketChannel socketChannel() { - return serverSocketChannel; - } - - @Override - public int initialSelectionKey() { - return SelectionKey.OP_ACCEPT; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/context/ListenContextFactory.java b/src/main/java/com/teragrep/net_01/channel/context/ListenContextFactory.java deleted file mode 100644 index 36f53ca..0000000 --- a/src/main/java/com/teragrep/net_01/channel/context/ListenContextFactory.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.SocketFactory; -import com.teragrep.net_01.eventloop.EventLoop; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.channels.ServerSocketChannel; -import java.util.concurrent.ExecutorService; - -/** - * Factory for creating {@link ListenContext}s for receiving new connections. - */ -public final class ListenContextFactory { - - private final ExecutorService executorService; - private final SocketFactory socketFactory; - private final ClockFactory clockFactory; - - public ListenContextFactory( - ExecutorService executorService, - SocketFactory socketFactory, - ClockFactory clockFactory - ) { - this.executorService = executorService; - this.socketFactory = socketFactory; - this.clockFactory = clockFactory; - } - - /** - * Opens a listening socket - * - * @param inetSocketAddress address to bind to - * @return {@link ListenContext} to be registered into an {@link EventLoop} - * @throws IOException if unable to bind to the address provided - */ - public ListenContext open(InetSocketAddress inetSocketAddress) throws IOException { - ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); - try { - serverSocketChannel.socket().setReuseAddress(true); - serverSocketChannel.bind(inetSocketAddress); - serverSocketChannel.configureBlocking(false); - } - catch (IOException ioException) { - serverSocketChannel.close(); - throw ioException; - } - return new ListenContext(serverSocketChannel, executorService, socketFactory, clockFactory); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfo.java b/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfo.java deleted file mode 100644 index 0e75046..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfo.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.net.ssl.SSLSession; -import javax.security.cert.X509Certificate; -import java.security.Principal; -import java.security.cert.Certificate; - -/** - * Provides information about a {@link Socket} encryption status. - */ -public interface EncryptionInfo { - - /** - * Information if connection is encrypted - * - * @return true if socket is an encrypted type - */ - boolean isEncrypted(); - - /** - * throws IllegalStateException if isEncrypted returns false - * - * @return see {@link SSLSession#getCipherSuite()} - */ - String getSessionCipherSuite(); - - /** - * throws IllegalStateException if isEncrypted returns false - * - * @return see {@link SSLSession#getLocalCertificates()} - */ - Certificate[] getLocalCertificates(); - - /** - * throws IllegalStateException if isEncrypted returns false - * - * @return see {@link SSLSession#getLocalPrincipal()} - */ - Principal getLocalPrincipal(); - - /** - * throws IllegalStateException if isEncrypted returns false - * - * @return see {@link SSLSession#getPeerCertificateChain()} - * @throws SSLPeerUnverifiedException - */ - X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException; - - /** - * throws IllegalStateException if isEncrypted returns false - * - * @return see {@link SSLSession#getPeerCertificates()} - * @throws SSLPeerUnverifiedException - */ - Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException; - - /** - * throws IllegalStateException if isEncrypted returns false - * - * @return see {@link SSLSession#getPeerPrincipal()} - * @throws SSLPeerUnverifiedException - */ - Principal getPeerPrincipal() throws SSLPeerUnverifiedException; -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfoStub.java b/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfoStub.java deleted file mode 100644 index 95e9380..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfoStub.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.security.cert.X509Certificate; -import java.security.Principal; -import java.security.cert.Certificate; - -/** - * Stub implementation of {@link EncryptionInfo} which is used by {@link PlainSocket} - */ -final class EncryptionInfoStub implements EncryptionInfo { - - EncryptionInfoStub() { - - } - - @Override - public boolean isEncrypted() { - return false; - } - - @Override - public String getSessionCipherSuite() { - throw new IllegalStateException("not encrypted"); - } - - @Override - public Certificate[] getLocalCertificates() { - throw new IllegalStateException("not encrypted"); - } - - @Override - public Principal getLocalPrincipal() { - throw new IllegalStateException("not encrypted"); - } - - @Override - public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException { - throw new IllegalStateException("not encrypted"); - } - - @Override - public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException { - throw new IllegalStateException("not encrypted"); - } - - @Override - public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { - throw new IllegalStateException("not encrypted"); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfoTLS.java b/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfoTLS.java deleted file mode 100644 index 636684a..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/EncryptionInfoTLS.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import tlschannel.TlsChannel; - -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.security.cert.X509Certificate; -import java.security.Principal; -import java.security.cert.Certificate; - -final class EncryptionInfoTLS implements EncryptionInfo { - - private final TlsChannel tlsChannel; - - EncryptionInfoTLS(TlsChannel tlsChannel) { - this.tlsChannel = tlsChannel; - } - - @Override - public boolean isEncrypted() { - return true; - } - - @Override - public String getSessionCipherSuite() { - return tlsChannel.getSslEngine().getSession().getCipherSuite(); - } - - @Override - public Certificate[] getLocalCertificates() { - return tlsChannel.getSslEngine().getSession().getLocalCertificates(); - } - - @Override - public Principal getLocalPrincipal() { - return tlsChannel.getSslEngine().getSession().getLocalPrincipal(); - } - - @Override - public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException { - return tlsChannel.getSslEngine().getSession().getPeerCertificateChain(); - } - - @Override - public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException { - return tlsChannel.getSslEngine().getSession().getPeerCertificates(); - } - - @Override - public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { - return tlsChannel.getSslEngine().getSession().getPeerPrincipal(); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/PlainFactory.java b/src/main/java/com/teragrep/net_01/channel/socket/PlainFactory.java deleted file mode 100644 index c281cc1..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/PlainFactory.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import java.nio.channels.SocketChannel; - -/** - * Factory for creating {@link PlainSocket} - */ -public final class PlainFactory extends SocketFactory { - - @Override - public Socket create(SocketChannel socketChannel) { - return new PlainSocket(socketChannel); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/PlainSocket.java b/src/main/java/com/teragrep/net_01/channel/socket/PlainSocket.java deleted file mode 100644 index f274f7f..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/PlainSocket.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.SocketChannel; - -final class PlainSocket implements Socket { - - private final SocketChannel socketChannel; - private final TransportInfo transportInfo; - - PlainSocket(SocketChannel socketChannel) { - this.socketChannel = socketChannel; - EncryptionInfo encryptionInfo = new EncryptionInfoStub(); - this.transportInfo = new TransportInfoImpl(socketChannel, encryptionInfo); - } - - @Override - public long read(ByteBuffer[] dsts) throws IOException { - return socketChannel.read(dsts); - } - - @Override - public long write(ByteBuffer[] dsts) throws IOException { - return socketChannel.write(dsts); - } - - @Override - public TransportInfo getTransportInfo() { - return transportInfo; - } - - @Override - public void close() throws IOException { - socketChannel.close(); - } - - @Override - public SocketChannel socketChannel() { - return socketChannel; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/Socket.java b/src/main/java/com/teragrep/net_01/channel/socket/Socket.java deleted file mode 100644 index ddc45e1..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/Socket.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.SocketChannel; - -/** - * {@link Socket} provides network connectivity methods - */ -public interface Socket { - - /** - * Read data from a network connection. - * - * @param dsts {@link ByteBuffer}s which are read to from the connection - * @return amount of bytes read - * @throws IOException if read fails - */ - long read(ByteBuffer[] dsts) throws IOException; - - /** - * Write data through a network connection. - * - * @param dsts {@link ByteBuffer}s which are written to the connection - * @return amount of bytes written - * @throws IOException if write fails - */ - long write(ByteBuffer[] dsts) throws IOException; - - /** - * Provides information about a network connection - * - * @return {@link TransportInfo} describing the connection - */ - TransportInfo getTransportInfo(); - - /** - * Closes the connection - * - * @throws IOException if close attempt fails - */ - void close() throws IOException; - - /** - * Provides the underlying {@link SocketChannel} - * - * @return {@link SocketChannel} - */ - SocketChannel socketChannel(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/SocketFactory.java b/src/main/java/com/teragrep/net_01/channel/socket/SocketFactory.java deleted file mode 100644 index 09283c1..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/SocketFactory.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import java.nio.channels.SocketChannel; - -/** - * {@link SocketFactory} is used to create {@link Socket}s - */ -public abstract class SocketFactory { - - abstract public Socket create(SocketChannel socketChannel); -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/TLSFactory.java b/src/main/java/com/teragrep/net_01/channel/socket/TLSFactory.java deleted file mode 100644 index 3779110..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/TLSFactory.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import tlschannel.ServerTlsChannel; -import tlschannel.TlsChannel; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; -import java.nio.channels.SocketChannel; -import java.util.function.Function; - -/** - * {@link TLSFactory} is used to create {@link TLSSocket}s - */ -public final class TLSFactory extends SocketFactory { - - private final SSLContext sslContext; - private final Function sslEngineFunction; - - public TLSFactory(SSLContext sslContext, Function sslEngineFunction) { - this.sslContext = sslContext; - this.sslEngineFunction = sslEngineFunction; - - } - - @Override - public Socket create(SocketChannel socketChannel) { - - TlsChannel tlsChannel = ServerTlsChannel - .newBuilder(socketChannel, sslContext) - .withEngineFactory(sslEngineFunction) - .build(); - - return new TLSSocket(socketChannel, tlsChannel); - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/TLSSocket.java b/src/main/java/com/teragrep/net_01/channel/socket/TLSSocket.java deleted file mode 100644 index f28698b..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/TLSSocket.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import tlschannel.TlsChannel; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.SocketChannel; - -final class TLSSocket implements Socket { - - private final SocketChannel socketChannel; - private final TlsChannel tlsChannel; - private final TransportInfo transportInfo; - - TLSSocket(SocketChannel socketChannel, TlsChannel tlsChannel) { - this.socketChannel = socketChannel; - this.tlsChannel = tlsChannel; - EncryptionInfo encryptionInfo = new EncryptionInfoTLS(tlsChannel); - this.transportInfo = new TransportInfoImpl(socketChannel, encryptionInfo); - } - - @Override - public long read(ByteBuffer[] dsts) throws IOException { - return tlsChannel.read(dsts); - } - - @Override - public long write(ByteBuffer[] dsts) throws IOException { - return tlsChannel.write(dsts); - } - - @Override - public TransportInfo getTransportInfo() { - return transportInfo; - } - - @Override - public void close() throws IOException { - tlsChannel.close(); - } - - @Override - public SocketChannel socketChannel() { - return socketChannel; - } -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/TransportInfo.java b/src/main/java/com/teragrep/net_01/channel/socket/TransportInfo.java deleted file mode 100644 index 1b2e844..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/TransportInfo.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -/** - * Provides information about a {@link Socket} transport. - */ -public interface TransportInfo { - - /** - * See {@link java.net.Socket#getLocalAddress()} - * - * @return local {@link java.net.InetAddress} as a {@link String} - */ - String getLocalAddress(); - - /** - * See {@link java.net.Socket#getLocalPort()} - * - * @return local port as int - */ - int getLocalPort(); - - /** - * See {@link java.net.Socket#getInetAddress()} - * - * @return remote {@link java.net.InetAddress} as a {@link String} - */ - String getPeerAddress(); - - /** - * See {@link java.net.Socket#getPort()} - * - * @return remote port as int - */ - int getPeerPort(); - - /** - * {@link EncryptionInfo} provides information of connection encryption detals, use - * {@link EncryptionInfo#isEncrypted()} to verify if the connection is encrypted, otherwise other methods will - * throw. - * - * @return {@link EncryptionInfo} of the connection - */ - EncryptionInfo getEncryptionInfo(); -} diff --git a/src/main/java/com/teragrep/net_01/channel/socket/TransportInfoImpl.java b/src/main/java/com/teragrep/net_01/channel/socket/TransportInfoImpl.java deleted file mode 100644 index 7aa2ce7..0000000 --- a/src/main/java/com/teragrep/net_01/channel/socket/TransportInfoImpl.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import java.nio.channels.SocketChannel; - -final class TransportInfoImpl implements TransportInfo { - - private final SocketChannel socketChannel; - private final EncryptionInfo encryptionInfo; - - TransportInfoImpl(SocketChannel socketChannel, EncryptionInfo encryptionInfo) { - this.socketChannel = socketChannel; - this.encryptionInfo = encryptionInfo; - } - - @Override - public String getLocalAddress() { - return socketChannel.socket().getLocalAddress().toString(); - } - - @Override - public int getLocalPort() { - return socketChannel.socket().getLocalPort(); - } - - @Override - public String getPeerAddress() { - return socketChannel.socket().getInetAddress().toString(); - } - - @Override - public int getPeerPort() { - return socketChannel.socket().getPort(); - } - - @Override - public EncryptionInfo getEncryptionInfo() { - return encryptionInfo; - } -} diff --git a/src/main/java/com/teragrep/net_01/client/EstablishedContextFactory.java b/src/main/java/com/teragrep/net_01/client/EstablishedContextFactory.java deleted file mode 100644 index 2d2779c..0000000 --- a/src/main/java/com/teragrep/net_01/client/EstablishedContextFactory.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.client; - -import com.teragrep.net_01.channel.context.ClockFactory; -import com.teragrep.net_01.channel.context.ConnectContext; -import com.teragrep.net_01.channel.context.ConnectContextFactory; -import com.teragrep.net_01.channel.context.EstablishedContext; -import com.teragrep.net_01.eventloop.EventLoop; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.concurrent.*; -import java.util.function.Consumer; - -/** - * Factory for creating an {@link EstablishedContext} also known as a connection initiation or a client. - */ -public final class EstablishedContextFactory { - - private static final Logger LOGGER = LoggerFactory.getLogger(EstablishedContextFactory.class); - private final ConnectContextFactory connectContextFactory; - private final EventLoop eventLoop; - private final ClockFactory clockFactory; - - /** - * Main for Constructor for {@link EstablishedContextFactory} - * - * @param connectContextFactory {@link ConnectContextFactory} for creating new connections - * @param eventLoop {@link EventLoop} to register new connections with - */ - public EstablishedContextFactory( - ConnectContextFactory connectContextFactory, - EventLoop eventLoop, - ClockFactory clockFactory - ) { - this.connectContextFactory = connectContextFactory; - this.eventLoop = eventLoop; - this.clockFactory = clockFactory; - } - - /** - * Opens up a new connection. Registers the connection to provided {@link EventLoop}. Note that the - * {@link EventLoop} needs to run in order to proceed with the connection. - * - * @param inetSocketAddress destination {@link InetSocketAddress} to connect to. - * @return an {@link EstablishedContext} {@link CompletableFuture}. - */ - public CompletableFuture open(InetSocketAddress inetSocketAddress) { - // this is for returning ready connection - CompletableFuture readyContextFuture = new CompletableFuture<>(); - Consumer establishedContextConsumer = readyContextFuture::complete; - - ConnectContext connectContext; - try { - connectContext = connectContextFactory.create(inetSocketAddress, clockFactory, establishedContextConsumer); - LOGGER.debug("registering to eventLoop <{}>", eventLoop); - eventLoop.register(connectContext); - LOGGER.debug("registered to eventLoop <{}>", eventLoop); - } - catch (IOException ioException) { - readyContextFuture.completeExceptionally(ioException); - } - - return readyContextFuture; - } -} diff --git a/src/main/java/com/teragrep/net_01/eventloop/EventLoop.java b/src/main/java/com/teragrep/net_01/eventloop/EventLoop.java deleted file mode 100644 index 9c1ee20..0000000 --- a/src/main/java/com/teragrep/net_01/eventloop/EventLoop.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.eventloop; - -import com.teragrep.net_01.channel.context.ConnectContext; -import com.teragrep.net_01.channel.context.EstablishedContext; -import com.teragrep.net_01.channel.context.Context; -import com.teragrep.net_01.channel.context.ListenContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.channels.CancelledKeyException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.util.Set; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * {@link EventLoop} is used to {@link Selector#select()} events from network connections which are registered with it - */ -public final class EventLoop implements AutoCloseable, Runnable { - - private static final Logger LOGGER = LoggerFactory.getLogger(EventLoop.class); - - private final Selector selector; - private final ConcurrentLinkedQueue pendingContextRegistrations; - private final AtomicBoolean stop; - - EventLoop(Selector selector) { - this.selector = selector; - - this.pendingContextRegistrations = new ConcurrentLinkedQueue<>(); - this.stop = new AtomicBoolean(); - } - - /** - * Register network connection with this {@link EventLoop}. Forces the {@link EventLoop} to run once after - * registration. - * - * @param context to register - */ - public void register(Context context) { - pendingContextRegistrations.add(context); - wakeup(); - } - - private void registerPendingRegistrations() { - while (true) { - Context context = pendingContextRegistrations.poll(); - if (context != null) { - try { - context.socketChannel().register(selector, context.initialSelectionKey(), context); - } - catch (ClosedChannelException closedChannelException) { - LOGGER.warn("attempted to register closed context <{}>", context); - context.close(); - } - } - else { - // no more contexts - break; - } - } - } - - /** - * Polls events from network connections via {@link Selector#select()} - * - * @throws IOException If an I/O error occurs during select() - */ - public void poll() throws IOException { - int readyKeys = selector.select(); - - registerPendingRegistrations(); - - LOGGER.debug("readyKeys <{}>", readyKeys); - - Set selectionKeys = selector.selectedKeys(); - LOGGER.debug("selectionKeys <{}> ", selectionKeys); - for (SelectionKey selectionKey : selectionKeys) { - try { - if (LOGGER.isDebugEnabled()) { - LOGGER - .debug( - "selectionKey <{}>: isValid <{}>, isConnectable <{}>, isAcceptable <{}>, isReadable <{}>, isWritable <{}>", - selectionKey, selectionKey.isValid(), selectionKey.isConnectable(), - selectionKey.isAcceptable(), selectionKey.isReadable(), selectionKey.isWritable() - ); - } - if (selectionKey.isAcceptable()) { - // ListenContext - ListenContext listenContext = (ListenContext) selectionKey.attachment(); - listenContext.handleEvent(selectionKey); - } - else if (selectionKey.isConnectable()) { - ConnectContext connectContext = (ConnectContext) selectionKey.attachment(); - connectContext.handleEvent(selectionKey); - } - else { // (selectionKey.isReadable() || selectionKey.isWritable()) - EstablishedContext establishedContext = (EstablishedContext) selectionKey.attachment(); - try { - establishedContext.handleEvent(selectionKey); - } - catch (CancelledKeyException cke) { - if (!establishedContext.isStub()) { - establishedContext.close(); - } - throw cke; - - } - } - } - catch (CancelledKeyException cke) { - // TODO implement better exception handling in ConnectContext, ListenContext and EstablishedContext - LOGGER.warn("Detected unexpectedly closed channel"); - } - } - - selectionKeys.clear(); - } - - /** - * Terminates the {@link EventLoop} - */ - @Override - public void close() { - for (SelectionKey selectionKey : selector.keys()) { - Context context = ((Context) selectionKey.attachment()); - context.close(); - } - try { - selector.close(); - } - catch (IOException ioException) { - LOGGER.warn("Selector.close() threw <{}>", ioException.getMessage(), ioException); - } - } - - /** - * Forces the {@link EventLoop} to execute one cycle - */ - public void wakeup() { - selector.wakeup(); - } - - @Override - public void run() { - try { - LOGGER.debug("Started"); - while (!stop.get()) { - poll(); - } - } - catch (IOException ioException) { - throw new UncheckedIOException(ioException); - } - finally { - close(); - } - LOGGER.debug("Stopped"); - } - - public void stop() { - LOGGER.debug("stopping"); - stop.set(true); - wakeup(); - } -} diff --git a/src/main/java/com/teragrep/net_01/eventloop/EventLoopFactory.java b/src/main/java/com/teragrep/net_01/eventloop/EventLoopFactory.java deleted file mode 100644 index 1c2f0bb..0000000 --- a/src/main/java/com/teragrep/net_01/eventloop/EventLoopFactory.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.eventloop; - -import java.io.IOException; -import java.nio.channels.Selector; - -/** - * Creates new {@link EventLoop}s - */ -public final class EventLoopFactory { - - public EventLoop create() throws IOException { - Selector selector = Selector.open(); - return new EventLoop(selector); - } -} diff --git a/src/main/java/com/teragrep/net_01/server/Server.java b/src/main/java/com/teragrep/net_01/server/Server.java deleted file mode 100644 index efdc07f..0000000 --- a/src/main/java/com/teragrep/net_01/server/Server.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.server; - -import com.teragrep.net_01.channel.context.ListenContext; -import com.teragrep.net_01.eventloop.EventLoop; - -import java.io.Closeable; -import java.io.IOException; - -/** - * {@link Server} is a {@link ListenContext} registered with an {@link EventLoop}. - */ -public final class Server implements Closeable { - - private final ListenContext listenContext; - - Server(ListenContext listenContext) { - this.listenContext = listenContext; - } - - @Override - public void close() throws IOException { - listenContext.close(); - } - -} diff --git a/src/main/java/com/teragrep/net_01/server/ServerFactory.java b/src/main/java/com/teragrep/net_01/server/ServerFactory.java deleted file mode 100644 index 8f42c19..0000000 --- a/src/main/java/com/teragrep/net_01/server/ServerFactory.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.server; - -import com.teragrep.net_01.channel.context.ClockFactory; -import com.teragrep.net_01.eventloop.EventLoop; -import com.teragrep.net_01.channel.context.ListenContext; -import com.teragrep.net_01.channel.context.ListenContextFactory; -import com.teragrep.net_01.channel.socket.SocketFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.concurrent.ExecutorService; - -/** - * Factory for creating {@link Server}s - */ -public final class ServerFactory { - - private static final Logger LOGGER = LoggerFactory.getLogger(ServerFactory.class); - - private final EventLoop eventLoop; - private final ExecutorService executorService; - private final SocketFactory socketFactory; - private final ClockFactory clockFactory; - - /** - * Primary constructor - * - * @param eventLoop which {@link EventLoop} {@link ListenContext} will be registered with - * @param executorService which {@link Server}s use to run received network connection events with - * @param socketFactory which is used to create {@link Server}'s connections - * @param clockFactory which is used to create clocks - */ - public ServerFactory( - EventLoop eventLoop, - ExecutorService executorService, - SocketFactory socketFactory, - ClockFactory clockFactory - ) { - this.eventLoop = eventLoop; - this.executorService = executorService; - this.socketFactory = socketFactory; - this.clockFactory = clockFactory; - } - - public Server create(int port) throws IOException { - - ListenContextFactory listenContextFactory = new ListenContextFactory( - executorService, - socketFactory, - clockFactory - ); - - ListenContext listenContext = listenContextFactory.open(new InetSocketAddress(port)); - LOGGER.debug("registering to eventLoop <{}>", eventLoop); - eventLoop.register(listenContext); - LOGGER.debug("registered to eventLoop <{}>", eventLoop); - return new Server(listenContext); - } -} diff --git a/src/test/java/com/teragrep/net_01/channel/context/EgressFake.java b/src/test/java/com/teragrep/net_01/channel/context/EgressFake.java deleted file mode 100644 index 2fa4779..0000000 --- a/src/test/java/com/teragrep/net_01/channel/context/EgressFake.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.buffer.writable.Writeable; - -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; - -public class EgressFake implements Egress { - - private final List writtenFrames; - - private final AtomicBoolean needRead; - - EgressFake() { - this.writtenFrames = new LinkedList<>(); - this.needRead = new AtomicBoolean(); - } - - @Override - public void accept(Writeable writeable) { - writtenFrames.add(writeable); - } - - @Override - public void run() { - // no-op - } - - @Override - public AtomicBoolean needRead() { - return needRead; - } - - @Override - public int outstanding() { - return 0; - } - - // for testing - List writtenFrames() { - return writtenFrames; - } -} diff --git a/src/test/java/com/teragrep/net_01/channel/context/EstablishedContextFake.java b/src/test/java/com/teragrep/net_01/channel/context/EstablishedContextFake.java deleted file mode 100644 index 6b6a0f3..0000000 --- a/src/test/java/com/teragrep/net_01/channel/context/EstablishedContextFake.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -import com.teragrep.net_01.channel.socket.Socket; - -import java.nio.channels.SelectionKey; -import java.nio.channels.spi.AbstractSelectableChannel; - -public class EstablishedContextFake implements EstablishedContext { - - private final InterestOps interestOps; - private final Socket socket; - - private final Ingress ingress; - private final Egress egress; - - EstablishedContextFake(InterestOps interestOps, Socket socket, Ingress ingress, Egress egress) { - this.interestOps = interestOps; - this.socket = socket; - this.ingress = ingress; - this.egress = egress; - } - - @Override - public void close() { - // no-op - } - - @Override - public AbstractSelectableChannel socketChannel() { - return socket.socketChannel(); - } - - @Override - public int initialSelectionKey() { - return 0; // none - } - - @Override - public void handleEvent(SelectionKey selectionKey) { - // no-op - } - - @Override - public InterestOps interestOps() { - return interestOps; - } - - @Override - public Socket socket() { - return socket; - } - - @Override - public Ingress ingress() { - return ingress; - } - - @Override - public Egress egress() { - return egress; - } - - @Override - public boolean isStub() { - return false; - } - -} diff --git a/src/test/java/com/teragrep/net_01/channel/context/InterestOpsFake.java b/src/test/java/com/teragrep/net_01/channel/context/InterestOpsFake.java deleted file mode 100644 index 698b934..0000000 --- a/src/test/java/com/teragrep/net_01/channel/context/InterestOpsFake.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context; - -public class InterestOpsFake implements InterestOps { - - @Override - public void add(int op) { - // no-op - } - - @Override - public void remove(int op) { - // no-op - } - - @Override - public void removeAll() { - // no-op - } -} diff --git a/src/test/java/com/teragrep/net_01/channel/context/buffer/BufferLeasePoolTest.java b/src/test/java/com/teragrep/net_01/channel/context/buffer/BufferLeasePoolTest.java deleted file mode 100644 index 8150443..0000000 --- a/src/test/java/com/teragrep/net_01/channel/context/buffer/BufferLeasePoolTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.context.buffer; - -import com.teragrep.net_01.channel.buffer.BufferLease; -import com.teragrep.net_01.channel.buffer.BufferLeasePool; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.nio.ByteBuffer; -import java.util.List; - -public class BufferLeasePoolTest { - - @Test - public void testPool() { - BufferLeasePool bufferLeasePool = new BufferLeasePool(); - List leases = bufferLeasePool.take(1); - - Assertions.assertEquals(1, leases.size()); - - Assertions.assertEquals(0, bufferLeasePool.estimatedSize()); // none in the pool - - BufferLease lease = leases.get(0); - - Assertions.assertFalse(lease.isStub()); - - Assertions.assertFalse(lease.isTerminated()); // initially 1 refs - - Assertions.assertEquals(1, lease.refs()); // check initial 1 ref - - lease.addRef(); - - Assertions.assertEquals(2, lease.refs()); - - lease.buffer().put((byte) 'x'); - - Assertions.assertEquals(1, lease.buffer().position()); - - lease.buffer().flip(); - - Assertions.assertEquals(0, lease.buffer().position()); - - Assertions.assertEquals(1, lease.buffer().limit()); - - Assertions.assertEquals((byte) 'x', lease.buffer().get()); - - Assertions.assertEquals(1, lease.buffer().position()); - - Assertions.assertEquals(2, lease.refs()); - - lease.removeRef(); - - Assertions.assertFalse(lease.isTerminated()); // initial ref must be still in place - - Assertions.assertEquals(1, lease.refs()); // initial ref must be still in - - ByteBuffer buffer = lease.buffer(); // get a hold of a reference - - lease.removeRef(); // removes initial ref - - Assertions.assertEquals(1, bufferLeasePool.estimatedSize()); // the one offered must be there - - Assertions.assertTrue(lease.isTerminated()); // no refs - - Assertions.assertThrows(IllegalStateException.class, lease::buffer); - - Assertions.assertEquals(buffer.capacity(), buffer.limit()); - - Assertions.assertEquals(0, buffer.position()); - - bufferLeasePool.close(); - - Assertions.assertEquals(0, bufferLeasePool.estimatedSize()); - } -} diff --git a/src/test/java/com/teragrep/net_01/channel/socket/SocketFake.java b/src/test/java/com/teragrep/net_01/channel/socket/SocketFake.java deleted file mode 100644 index 950a243..0000000 --- a/src/test/java/com/teragrep/net_01/channel/socket/SocketFake.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -import java.nio.ByteBuffer; -import java.nio.channels.SocketChannel; - -public class SocketFake implements Socket { - - private final TransportInfo transportInfo; - - public SocketFake() { - this.transportInfo = new TransportInfoFake(); - } - - @Override - public long read(ByteBuffer[] dsts) { - return 0; - } - - @Override - public long write(ByteBuffer[] dsts) { - return 0; - } - - @Override - public TransportInfo getTransportInfo() { - return transportInfo; - } - - @Override - public void close() { - - } - - @Override - public SocketChannel socketChannel() { - throw new UnsupportedOperationException("socketChannel not supported by SocketFake"); - } -} diff --git a/src/test/java/com/teragrep/net_01/channel/socket/TransportInfoFake.java b/src/test/java/com/teragrep/net_01/channel/socket/TransportInfoFake.java deleted file mode 100644 index 9566e8e..0000000 --- a/src/test/java/com/teragrep/net_01/channel/socket/TransportInfoFake.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Java Reliable Event Logging Protocol Library Server Implementation RLP-03 - * Copyright (C) 2021-2024 Suomen Kanuuna Oy - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * - * Additional permission under GNU Affero General Public License version 3 - * section 7 - * - * If you modify this Program, or any covered work, by linking or combining it - * with other code, such other code is not for that reason alone subject to any - * of the requirements of the GNU Affero GPL version 3 as long as this Program - * is the same Program as licensed from Suomen Kanuuna Oy without any additional - * modifications. - * - * Supplemented terms under GNU Affero General Public License version 3 - * section 7 - * - * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified - * versions must be marked as "Modified version of" The Program. - * - * Names of the licensors and authors may not be used for publicity purposes. - * - * No rights are granted for use of trade names, trademarks, or service marks - * which are in The Program if any. - * - * Licensee must indemnify licensors and authors for any liability that these - * contractual assumptions impose on licensors and authors. - * - * To the extent this program is licensed as part of the Commercial versions of - * Teragrep, the applicable Commercial License may apply to this file if you as - * a licensee so wish it. - */ -package com.teragrep.net_01.channel.socket; - -final class TransportInfoFake implements TransportInfo { - - private final EncryptionInfo encryptionInfo; - - TransportInfoFake() { - this.encryptionInfo = new EncryptionInfoStub(); - } - - @Override - public String getLocalAddress() { - return "fake.local.example.com"; - } - - @Override - public int getLocalPort() { - return 601; - } - - @Override - public String getPeerAddress() { - return "fake.remote.example.com"; - } - - @Override - public int getPeerPort() { - return 65535; - } - - @Override - public EncryptionInfo getEncryptionInfo() { - return encryptionInfo; - } -}