// Path: src/main/java/org/tools4j/hoverraft/command/Command.java
// public interface Command extends DirectPayload, Event {
// 
//     CommandKey commandKey();
// 
//     CommandPayload commandPayload();
// 
//     default Command sourceId(final int sourceId) {
//         commandKey().sourceId(sourceId);
//         return this;
//     }
// 
//     default Command commandIndex(final long commandIndex) {
//         commandKey().commandIndex(commandIndex);
//         return this;
//     }
// 
//     default void copyFrom(final Command command) {
//         writeBufferOrNull().putBytes(offset(), command.readBufferOrNull(), command.offset(), command.byteLength());
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onCommand(serverContext, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/config/ConsensusConfig.java
// public interface ConsensusConfig {
//     long minElectionTimeoutMillis();
//     long maxElectionTimeoutMillis();
//     long heartbeatTimeoutMillis();
//     Optional<String> ipcMulticastChannel();
//     int serverCount();
//     ServerConfig serverConfig(int index);
//     int sourceCount();
//     SourceConfig sourceConfig(int index);
//     ThreadingMode threadingMode();
// 
//     default ServerConfig serverConfigByIdOrNull(int id) {
//         for (int i = 0; i < serverCount(); i++) {
//             final ServerConfig config = serverConfig(i);
//             if (config.id() == id) {
//                 return config;
//             }
//         }
//         return null;
//     }
// 
//     default SourceConfig sourceConfigByIdOrNull(int id) {
//         for (int i = 0; i < sourceCount(); i++) {
//             final SourceConfig config = sourceConfig(i);
//             if (config.id() == id) {
//                 return config;
//             }
//         }
//         return null;
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/direct/DirectPayload.java
// public interface DirectPayload {
// 
//     int byteLength();
// 
//     int offset();
// 
//     DirectBuffer readBufferOrNull();
// 
//     MutableDirectBuffer writeBufferOrNull();
// 
//     void wrap(DirectBuffer buffer, int offset);
// 
//     void wrap(MutableDirectBuffer buffer, int offset);
// 
//     void unwrap();
// 
//     default boolean isWrapped() {
//         return readBufferOrNull() != null || writeBufferOrNull() != 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/Receivers.java
// public final class Receivers {
// 
//     public static <M extends DirectPayload> Receiver<M> roundRobinReceiver(final Receiver<? extends M>... receivers) {
//         return new Receiver<M>() {
//             private int index = -1;
// 
//             @Override
//             public int poll(final Consumer<? super M> messageMandler, final int limit) {
//                 final Receiver<? extends M>[] r = receivers;
//                 final int len = r.length;
//                 int cnt = 0;
//                 for (int i = 0; i < len && cnt < limit; i++) {
//                     index++;
//                     if (index >= len) {
//                         index -= len;
//                     }
//                     cnt += r[index].poll(messageMandler, limit - cnt);
//                 }
//                 return cnt;
//             }
//         };
//     }
// 
// }


import java.util.Objects;
import java.util.function.Consumer;
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.config.ConsensusConfig;
import org.tools4j.hoverraft.direct.DirectPayload;
import org.tools4j.hoverraft.message.Message;
import org.tools4j.hoverraft.transport.Receiver;
import org.tools4j.hoverraft.transport.Receivers;
/**
 * 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.server;



public class RoundRobinMessagePoller<M extends DirectPayload> {

    private final Receiver<M> roundRobinReceiver;
    private final Consumer<? super M> messageHandler;

    private RoundRobinMessagePoller(final Receiver<M> roundRobinReceiver,
                                    final Consumer<? super M> messageHandler) {
        this.roundRobinReceiver = Objects.requireNonNull(roundRobinReceiver);
        this.messageHandler = Objects.requireNonNull(messageHandler);
    }

// Path: src/main/java/org/tools4j/hoverraft/command/Command.java
// public interface Command extends DirectPayload, Event {
// 
//     CommandKey commandKey();
// 
//     CommandPayload commandPayload();
// 
//     default Command sourceId(final int sourceId) {
//         commandKey().sourceId(sourceId);
//         return this;
//     }
// 
//     default Command commandIndex(final long commandIndex) {
//         commandKey().commandIndex(commandIndex);
//         return this;
//     }
// 
//     default void copyFrom(final Command command) {
//         writeBufferOrNull().putBytes(offset(), command.readBufferOrNull(), command.offset(), command.byteLength());
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onCommand(serverContext, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/config/ConsensusConfig.java
// public interface ConsensusConfig {
//     long minElectionTimeoutMillis();
//     long maxElectionTimeoutMillis();
//     long heartbeatTimeoutMillis();
//     Optional<String> ipcMulticastChannel();
//     int serverCount();
//     ServerConfig serverConfig(int index);
//     int sourceCount();
//     SourceConfig sourceConfig(int index);
//     ThreadingMode threadingMode();
// 
//     default ServerConfig serverConfigByIdOrNull(int id) {
//         for (int i = 0; i < serverCount(); i++) {
//             final ServerConfig config = serverConfig(i);
//             if (config.id() == id) {
//                 return config;
//             }
//         }
//         return null;
//     }
// 
//     default SourceConfig sourceConfigByIdOrNull(int id) {
//         for (int i = 0; i < sourceCount(); i++) {
//             final SourceConfig config = sourceConfig(i);
//             if (config.id() == id) {
//                 return config;
//             }
//         }
//         return null;
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/direct/DirectPayload.java
// public interface DirectPayload {
// 
//     int byteLength();
// 
//     int offset();
// 
//     DirectBuffer readBufferOrNull();
// 
//     MutableDirectBuffer writeBufferOrNull();
// 
//     void wrap(DirectBuffer buffer, int offset);
// 
//     void wrap(MutableDirectBuffer buffer, int offset);
// 
//     void unwrap();
// 
//     default boolean isWrapped() {
//         return readBufferOrNull() != null || writeBufferOrNull() != 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/Receivers.java
// public final class Receivers {
// 
//     public static <M extends DirectPayload> Receiver<M> roundRobinReceiver(final Receiver<? extends M>... receivers) {
//         return new Receiver<M>() {
//             private int index = -1;
// 
//             @Override
//             public int poll(final Consumer<? super M> messageMandler, final int limit) {
//                 final Receiver<? extends M>[] r = receivers;
//                 final int len = r.length;
//                 int cnt = 0;
//                 for (int i = 0; i < len && cnt < limit; i++) {
//                     index++;
//                     if (index >= len) {
//                         index -= len;
//                     }
//                     cnt += r[index].poll(messageMandler, limit - cnt);
//                 }
//                 return cnt;
//             }
//         };
//     }
// 
// }


// Path: src/main/java/org/tools4j/hoverraft/server/RoundRobinMessagePoller.java
import java.util.Objects;
import java.util.function.Consumer;
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.config.ConsensusConfig;
import org.tools4j.hoverraft.direct.DirectPayload;
import org.tools4j.hoverraft.message.Message;
import org.tools4j.hoverraft.transport.Receiver;
import org.tools4j.hoverraft.transport.Receivers;

/**
 * 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.server;



public class RoundRobinMessagePoller<M extends DirectPayload> {

    private final Receiver<M> roundRobinReceiver;
    private final Consumer<? super M> messageHandler;

    private RoundRobinMessagePoller(final Receiver<M> roundRobinReceiver,
                                    final Consumer<? super M> messageHandler) {
        this.roundRobinReceiver = Objects.requireNonNull(roundRobinReceiver);
        this.messageHandler = Objects.requireNonNull(messageHandler);
    }

    public static RoundRobinMessagePoller<Message> forServerMessages(final ServerContext serverContext,