// Path: src/main/java/org/tools4j/hoverraft/message/Message.java
// public interface Message extends DirectPayload, Event {
// 
//     MessageType type();
// 
//     default void sendTo(final Sender<? super Message> sender, final ResendStrategy resendStrategy) {
//         final long res = sender.offer(this);
//         if (res < 0) {
//             resendStrategy.onRejectedOffer(sender, this, res);
//         }
//     }
// 
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/transport/Receiver.java
// public interface Receiver<M extends DirectPayload> {
//     /**
//      * Polls messages in non-blocking mode. Messages are passed to the specified
//      * message event.
//      *
//      * @param messageMandler the event invoked for each message
//      * @param limit maximum number of messages to receive
//      * @return number of messages received, zero to at most {@code limit} messages
//      */
//     int poll(Consumer<? super M> messageMandler, int limit);
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/transport/Sender.java
// public interface Sender<M extends DirectPayload> {
//     /**
//      * Terminates composing and offsers the message to the transport in
//      * non-blocking mode. Can be called multiple times for same message if sending
//      * failed which is indicated through a negative return value.
//      *
//      * @param message the message to send
//      * @return Non-negative transpport state such as position, otherwise zero if
//      * not applicable and successful or a negative error value
//      * {@link RejectReason#NOT_CONNECTED}, {@link RejectReason#BACK_PRESSURED},
//      * {@link RejectReason#ADMIN_ACTION} or {@link RejectReason#CLOSED}.
//      */
//     long offer(M message);
// }


import org.tools4j.hoverraft.message.Message;
import org.tools4j.hoverraft.transport.Receiver;
import org.tools4j.hoverraft.transport.Sender;
import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 hover-raft (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.hoverraft.transport.embedded;



/**
 * In-process message bus for simple messages.
 */
public class InMemoryMessageBus<M extends Message> implements Sender<M>,Receiver<M> {

    private final Deque<M> queue = new ConcurrentLinkedDeque<M>();

    @Override
    public long offer(final M message) {
        return queue.offerLast(message) ? 1 : 0;
    }

    @Override
    public int poll(final Consumer<? super M> messageMandler, final int limit) {
        int cnt = 0;
        while (cnt < limit) {
            M msg = queue.pollFirst();
            if (msg == null) {
// Path: src/main/java/org/tools4j/hoverraft/message/Message.java
// public interface Message extends DirectPayload, Event {
// 
//     MessageType type();
// 
//     default void sendTo(final Sender<? super Message> sender, final ResendStrategy resendStrategy) {
//         final long res = sender.offer(this);
//         if (res < 0) {
//             resendStrategy.onRejectedOffer(sender, this, res);
//         }
//     }
// 
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/transport/Receiver.java
// public interface Receiver<M extends DirectPayload> {
//     /**
//      * Polls messages in non-blocking mode. Messages are passed to the specified
//      * message event.
//      *
//      * @param messageMandler the event invoked for each message
//      * @param limit maximum number of messages to receive
//      * @return number of messages received, zero to at most {@code limit} messages
//      */
//     int poll(Consumer<? super M> messageMandler, int limit);
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/transport/Sender.java
// public interface Sender<M extends DirectPayload> {
//     /**
//      * Terminates composing and offsers the message to the transport in
//      * non-blocking mode. Can be called multiple times for same message if sending
//      * failed which is indicated through a negative return value.
//      *
//      * @param message the message to send
//      * @return Non-negative transpport state such as position, otherwise zero if
//      * not applicable and successful or a negative error value
//      * {@link RejectReason#NOT_CONNECTED}, {@link RejectReason#BACK_PRESSURED},
//      * {@link RejectReason#ADMIN_ACTION} or {@link RejectReason#CLOSED}.
//      */
//     long offer(M message);
// }


// Path: src/main/java/org/tools4j/hoverraft/transport/embedded/InMemoryMessageBus.java
import org.tools4j.hoverraft.message.Message;
import org.tools4j.hoverraft.transport.Receiver;
import org.tools4j.hoverraft.transport.Sender;
import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 hover-raft (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.hoverraft.transport.embedded;



/**
 * In-process message bus for simple messages.
 */
public class InMemoryMessageBus<M extends Message> implements Sender<M>,Receiver<M> {

    private final Deque<M> queue = new ConcurrentLinkedDeque<M>();

    @Override
    public long offer(final M message) {
        return queue.offerLast(message) ? 1 : 0;
    }

    @Override
    public int poll(final Consumer<? super M> messageMandler, final int limit) {
        int cnt = 0;
        while (cnt < limit) {
            M msg = queue.pollFirst();
            if (msg == null) {
                return cnt;