<commit_before>/*
 * Copyright (c) 2014 mucaho (https://github.com/mucaho).
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.github.mucaho.jnetrobust.example;


import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferInput;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import com.esotericsoftware.kryo.io.KryoObjectInput;
import com.esotericsoftware.kryo.io.KryoObjectOutput;
import com.github.mucaho.jnetrobust.Logger;
import com.github.mucaho.jnetrobust.Protocol;
import com.github.mucaho.jnetrobust.ProtocolListener;
import com.github.mucaho.jnetrobust.control.Metadata;
import com.github.mucaho.jnetrobust.controller.Packet;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.*;

/**
 * Abstracts away much of the protocol's functionality with a default configuration,
 * and sets-up a <code>DatagramChannel</code> for sending/receiving data,
 * as well as a <code>Kryo</code> instance for serialization.
 * <br />
 * This is the preferred way to get your feet wet and be introduced to the protocol.
 * <br />
 * See the various examples for usage scenarios.
 */
public class ProtocolHost {
    public interface DataListener<T> {
        void handleOrderedData(T orderedData);
        void handleNewestData(T newestData);
        //TODO add exceptional callback
    }

    public static class ProtocolHandle<T> {
        private final ProtocolId protocolId;
        private final ProtocolHost host;
        private ProtocolHandle(ProtocolId protocolId, ProtocolHost host) {
            this.host = host;
            this.protocolId = protocolId;
        }

        private List<T> sendDatas = new ArrayList<T>();

        public void send() throws IOException {
            sendDatas.clear();
            host.send(protocolId, sendDatas);
        }
        public void send(T data) throws IOException {
            sendDatas.clear();
            sendDatas.add(data);

            host.send(protocolId, sendDatas);
        }
        public void send(List<T> datas) throws IOException {
            host.send(protocolId, datas);
        }

        public T receive() throws IOException, ClassNotFoundException {
            host.receive();
            return host.receive(protocolId);
        }
    }

    private final String hostName;

    // protocol fields
    private final Map<ProtocolId, Protocol<?>> protocols = new HashMap<ProtocolId, Protocol<?>>();
    private final Map<ProtocolId, DataListener<?>> listeners = new HashMap<ProtocolId, DataListener<?>>();

    // serialization fields
    private final Kryo kryo;
    private final ByteBuffer buffer = ByteBuffer.allocate(4096);
    private final ByteBufferInput bufferInput = new ByteBufferInput();
    private final ByteBufferOutput bufferOutput = new ByteBufferOutput();
    private final KryoObjectInput objectInput;
    private final KryoObjectOutput objectOutput;

    // network communication fields
    private final DatagramChannel channel;

    public ProtocolHost(String hostName, SocketAddress localAddress, Class<?>... dataClasses) throws IOException {
        // setup network communication
        channel = DatagramChannel.open();
        channel.configureBlocking(false);
        channel.socket().bind(localAddress);

        // setup serialization
        kryo = new Kryo();
        kryo.register(Packet.class); // add argument `new ExternalizableSerializer()` if needed
        kryo.register(Metadata.class); // add argument `new ExternalizableSerializer()` if needed
        for (Class<?> dataClass : dataClasses)
            kryo.register(dataClass);
        objectInput = new KryoObjectInput(kryo, bufferInput);
        objectOutput = new KryoObjectOutput(kryo, bufferOutput);

        this.hostName = hostName;
    }

    public <T> ProtocolHandle<T> register(byte topic, SocketAddress remoteAddress) {
        return register(topic, remoteAddress, null);
    }

    @SuppressWarnings("unchecked")
    public <T> ProtocolHandle<T> register(byte topic, SocketAddress remoteAddress, final DataListener<T> listener) {
        final ProtocolId protocolId = new ProtocolId(topic, remoteAddress);

        listeners.put(protocolId, listener);

        ProtocolListener<T> protocolListener = new ProtocolListener<T>() {
            @Override
            public void handleOrderedData(short dataId, T orderedData) {
                Queue<T> orderedQueue = (Queue<T>) orderedQueues.get(protocolId);<commit_msg>ProtocolHost: Set TrafficClass options<commit_after>/*
 * Copyright (c) 2014 mucaho (https://github.com/mucaho).
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.github.mucaho.jnetrobust.example;


import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferInput;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import com.esotericsoftware.kryo.io.KryoObjectInput;
import com.esotericsoftware.kryo.io.KryoObjectOutput;
import com.github.mucaho.jnetrobust.Logger;
import com.github.mucaho.jnetrobust.Protocol;
import com.github.mucaho.jnetrobust.ProtocolListener;
import com.github.mucaho.jnetrobust.control.Metadata;
import com.github.mucaho.jnetrobust.controller.Packet;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.*;

/**
 * Abstracts away much of the protocol's functionality with a default configuration,
 * and sets-up a <code>DatagramChannel</code> for sending/receiving data,
 * as well as a <code>Kryo</code> instance for serialization.
 * <br />
 * This is the preferred way to get your feet wet and be introduced to the protocol.
 * <br />
 * See the various examples for usage scenarios.
 */
public class ProtocolHost {

    private static final int IPTOS_LOWCOST = 0x02;
    private static final int IPTOS_RELIABILITY = 0x04;
    private static final int IPTOS_THROUGHPUT = 0x08;
    private static final int IPTOS_LOWDELAY = 0x10;

    public interface DataListener<T> {
        void handleOrderedData(T orderedData);
        void handleNewestData(T newestData);
        //TODO add exceptional callback
    }

    public static class ProtocolHandle<T> {
        private final ProtocolId protocolId;
        private final ProtocolHost host;
        private ProtocolHandle(ProtocolId protocolId, ProtocolHost host) {
            this.host = host;
            this.protocolId = protocolId;
        }

        private List<T> sendDatas = new ArrayList<T>();

        public void send() throws IOException {
            sendDatas.clear();
            host.send(protocolId, sendDatas);
        }
        public void send(T data) throws IOException {
            sendDatas.clear();
            sendDatas.add(data);

            host.send(protocolId, sendDatas);
        }
        public void send(List<T> datas) throws IOException {
            host.send(protocolId, datas);
        }

        public T receive() throws IOException, ClassNotFoundException {
            host.receive();
            return host.receive(protocolId);
        }
    }

    private final String hostName;

    // protocol fields
    private final Map<ProtocolId, Protocol<?>> protocols = new HashMap<ProtocolId, Protocol<?>>();
    private final Map<ProtocolId, DataListener<?>> listeners = new HashMap<ProtocolId, DataListener<?>>();

    // serialization fields
    private final Kryo kryo;
    private final ByteBuffer buffer = ByteBuffer.allocate(4096);
    private final ByteBufferInput bufferInput = new ByteBufferInput();
    private final ByteBufferOutput bufferOutput = new ByteBufferOutput();
    private final KryoObjectInput objectInput;
    private final KryoObjectOutput objectOutput;

    // network communication fields
    private final DatagramChannel channel;

    public ProtocolHost(String hostName, SocketAddress localAddress, Class<?>... dataClasses) throws IOException {
        // setup network communication
        channel = DatagramChannel.open();
        channel.configureBlocking(false);
        channel.socket().setTrafficClass(IPTOS_LOWDELAY | IPTOS_THROUGHPUT | IPTOS_RELIABILITY);
        channel.socket().bind(localAddress);

        // setup serialization
        kryo = new Kryo();
        kryo.register(Packet.class); // add argument `new ExternalizableSerializer()` if needed
        kryo.register(Metadata.class); // add argument `new ExternalizableSerializer()` if needed
        for (Class<?> dataClass : dataClasses)
            kryo.register(dataClass);
        objectInput = new KryoObjectInput(kryo, bufferInput);
        objectOutput = new KryoObjectOutput(kryo, bufferOutput);

        this.hostName = hostName;
    }

    public <T> ProtocolHandle<T> register(byte topic, SocketAddress remoteAddress) {
        return register(topic, remoteAddress, null);
    }

    @SuppressWarnings("unchecked")
    public <T> ProtocolHandle<T> register(byte topic, SocketAddress remoteAddress, final DataListener<T> listener) {
        final ProtocolId protocolId = new ProtocolId(topic, remoteAddress);

        listeners.put(protocolId, listener);

        ProtocolListener<T> protocolListener = new ProtocolListener<T>() {
            @Override
            public void handleOrderedData(short dataId, T orderedData) {
                Queue<T> orderedQueue = (Queue<T>) orderedQueues.get(protocolId);
{'fraction_non_alphanumeric': 0.091804287619243, 'fraction_numerical': 0.003692686429377372, 'mean_word_length': 3.3084401237295626, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5567458', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2619, 'n_words': 741}