// 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/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);
//         }
//     }
// 
// 
// }


import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.direct.DirectPayload;
import org.tools4j.hoverraft.message.Message;
import java.util.List;
import java.util.Objects;
/**
 * 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;



public class DefaultConnections<M extends Message> implements Connections<M> {

    private final Receiver<Command>[] sourceReceivers;
    private final Receiver<M>[] serverReceivers;
    private final Sender<M> serverMulticastSender;

    public DefaultConnections(final List<Receiver<? extends Command>> sourceReceivers,
                              final List<Receiver<? extends M>> serverReceivers,
                              final Sender<? super M> serverMulticastSender) {
        Objects.requireNonNull(serverMulticastSender);
        this.sourceReceivers = receivers(sourceReceivers);
        this.serverReceivers = receivers(serverReceivers);
        this.serverMulticastSender = (m) -> serverMulticastSender.offer(m);
    }

    //safely wraps Receiver<? super M> to Receiver<M> and converts List to array
// 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/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/DefaultConnections.java
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.direct.DirectPayload;
import org.tools4j.hoverraft.message.Message;
import java.util.List;
import java.util.Objects;

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



public class DefaultConnections<M extends Message> implements Connections<M> {

    private final Receiver<Command>[] sourceReceivers;
    private final Receiver<M>[] serverReceivers;
    private final Sender<M> serverMulticastSender;

    public DefaultConnections(final List<Receiver<? extends Command>> sourceReceivers,
                              final List<Receiver<? extends M>> serverReceivers,
                              final Sender<? super M> serverMulticastSender) {
        Objects.requireNonNull(serverMulticastSender);
        this.sourceReceivers = receivers(sourceReceivers);
        this.serverReceivers = receivers(serverReceivers);
        this.serverMulticastSender = (m) -> serverMulticastSender.offer(m);
    }

    //safely wraps Receiver<? super M> to Receiver<M> and converts List to array
    private static <M extends DirectPayload> Receiver<M>[] receivers(final List<Receiver<? extends M>> receivers) {