<commit_before>/*
 * Copyright 2012 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package net.tomp2p.dht;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.Random;
import java.util.TreeSet;

import net.tomp2p.connection.Bindings;
import net.tomp2p.futures.FutureBootstrap;
import net.tomp2p.futures.FutureDiscover;
import net.tomp2p.message.Message.Type;
import net.tomp2p.message.Message;
import net.tomp2p.p2p.AutomaticFuture;
import net.tomp2p.p2p.Peer;
import net.tomp2p.p2p.PeerBuilder;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.peers.PeerMap;
import net.tomp2p.peers.PeerMapConfiguration;
import net.tomp2p.peers.PeerSocketAddress;

public class UtilsDHT2 {
    /**
     * Used to make the testcases predictable. Used as an input for {@link Random}.
     */
    public static final long THE_ANSWER = 42L;

    /**
     * Having two peers in a network, the seed needs to be different, otherwise we create a peer with the same id twice.
     */
    public static final long THE_ANSWER2 = 43L;

    public static Message createDummyMessage() throws UnknownHostException {
        return createDummyMessage(false, false);
    }

    public static Message createDummyMessage(boolean firewallUDP, boolean firewallTCP)
            throws UnknownHostException {
        return createDummyMessage(new Number160("0x4321"), "127.0.0.1", 8001, 8002, new Number160("0x1234"),
                "127.0.0.1", 8003, 8004, (byte) 0, Type.REQUEST_1, firewallUDP, firewallTCP);
    }

    public static PeerAddress createAddress(Number160 id) throws UnknownHostException {
        return createAddress(id, "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress() throws UnknownHostException {
        return createAddress(new Number160("0x5678"), "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress(int id) throws UnknownHostException {
        return createAddress(new Number160(id), "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress(String id) throws UnknownHostException {
        return createAddress(new Number160(id), "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress(Number160 idSender, String inetSender, int tcpPortSender,
            int udpPortSender, boolean firewallUDP, boolean firewallTCP) throws UnknownHostException {
        InetAddress inetSend = InetAddress.getByName(inetSender);
        PeerSocketAddress peerSocketAddress = new PeerSocketAddress(inetSend, tcpPortSender, udpPortSender);
        PeerAddress n1 = new PeerAddress(idSender, peerSocketAddress, firewallTCP, firewallUDP, false, false, false,
                PeerAddress.EMPTY_PEER_SOCKET_ADDRESSES);
        return n1;
    }

    public static Message createDummyMessage(Number160 idSender, String inetSender, int tcpPortSendor,
            int udpPortSender, Number160 idRecipien, String inetRecipient, int tcpPortRecipient,
            int udpPortRecipient, byte command, Type type, boolean firewallUDP, boolean firewallTCP)
            throws UnknownHostException {
        Message message = new Message();
        PeerAddress n1 = createAddress(idSender, inetSender, tcpPortSendor, udpPortSender, firewallUDP,
                firewallTCP);
        message.sender(n1);
        //
        PeerAddress n2 = createAddress(idRecipien, inetRecipient, tcpPortRecipient, udpPortRecipient,
                firewallUDP, firewallTCP);
        message.recipient(n2);
        message.type(type);
        message.command(command);
        return message;
    }

    public static PeerDHT[] createNodes(int nrOfPeers, Random rnd, int port) throws Exception {
        return createNodes(nrOfPeers, rnd, port, null);
    }

    public static PeerDHT[] createNodes(int nrOfPeers, Random rnd, int port, AutomaticFuture automaticFuture)
            throws Exception {
        return createNodes(nrOfPeers, rnd, port, automaticFuture, false);
    }

    /**
     * Creates peers for testing. The first peer (peer[0]) will be used as the master. This means that shutting down
     * peer[0] will shut down all other peers
     * 
     * @param nrOfPeers
     *            The number of peers to create including the master
     * @param rnd
     *            The random object to create random peer IDs
     * @param port
     *            The port where the master peer will listen to
     * @return All the peers, with the master peer at position 0 -> peer[0]
     * @throws Exception
     *             If the creation of nodes fail.
     */
    public static PeerDHT[] createNodes(int nrOfPeers, Random rnd, int port, AutomaticFuture automaticFuture,
            boolean maintenance) throws Exception {
        if (nrOfPeers < 1) {
            throw new IllegalArgumentException("Cannot create less than 1 peer");
        }
        Bindings bindings = new Bindings().addInterface("lo");
        PeerDHT[] peers = new PeerDHT[nrOfPeers];
        final Peer master;
        if (automaticFuture != null) {
        	Number160 peerId = new Number160(rnd);
        	PeerMap peerMap = new PeerMap(new PeerMapConfiguration(peerId));
        	master = new PeerBuilder(peerId)
                    .ports(port).enableMaintenance(maintenance)
                    .bindings(bindings).peerMap(peerMap).start().addAutomaticFuture(automaticFuture);
            peers[0] = new PeerBuilderDHT(master).start(); 
            
        } else {
        	Number160 peerId = new Number160(rnd);
        	PeerMap peerMap = new PeerMap(new PeerMapConfiguration(peerId));
        	master = new PeerBuilder(peerId).enableMaintenance(maintenance).bindings(bindings)
                    .peerMap(peerMap).ports(port).start();
        	peers[0] = new PeerBuilderDHT(master).start(); 
        }

        for (int i = 1; i < nrOfPeers; i++) {
            if (automaticFuture != null) {
            	Number160 peerId = new Number160(rnd);
            	PeerMap peerMap = new PeerMap(new PeerMapConfiguration(peerId));
                Peer peer = new PeerBuilder(peerId)
                        .masterPeer(master)
                        .enableMaintenance(maintenance).enableMaintenance(maintenance).peerMap(peerMap).bindings(bindings).start().addAutomaticFuture(automaticFuture);<commit_msg>removed bindings<commit_after>/*
 * Copyright 2012 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package net.tomp2p.dht;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.Random;
import java.util.TreeSet;

import net.tomp2p.connection.Bindings;
import net.tomp2p.futures.FutureBootstrap;
import net.tomp2p.futures.FutureDiscover;
import net.tomp2p.message.Message.Type;
import net.tomp2p.message.Message;
import net.tomp2p.p2p.AutomaticFuture;
import net.tomp2p.p2p.Peer;
import net.tomp2p.p2p.PeerBuilder;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.peers.PeerMap;
import net.tomp2p.peers.PeerMapConfiguration;
import net.tomp2p.peers.PeerSocketAddress;

public class UtilsDHT2 {
    /**
     * Used to make the testcases predictable. Used as an input for {@link Random}.
     */
    public static final long THE_ANSWER = 42L;

    /**
     * Having two peers in a network, the seed needs to be different, otherwise we create a peer with the same id twice.
     */
    public static final long THE_ANSWER2 = 43L;

    public static Message createDummyMessage() throws UnknownHostException {
        return createDummyMessage(false, false);
    }

    public static Message createDummyMessage(boolean firewallUDP, boolean firewallTCP)
            throws UnknownHostException {
        return createDummyMessage(new Number160("0x4321"), "127.0.0.1", 8001, 8002, new Number160("0x1234"),
                "127.0.0.1", 8003, 8004, (byte) 0, Type.REQUEST_1, firewallUDP, firewallTCP);
    }

    public static PeerAddress createAddress(Number160 id) throws UnknownHostException {
        return createAddress(id, "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress() throws UnknownHostException {
        return createAddress(new Number160("0x5678"), "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress(int id) throws UnknownHostException {
        return createAddress(new Number160(id), "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress(String id) throws UnknownHostException {
        return createAddress(new Number160(id), "127.0.0.1", 8005, 8006, false, false);
    }

    public static PeerAddress createAddress(Number160 idSender, String inetSender, int tcpPortSender,
            int udpPortSender, boolean firewallUDP, boolean firewallTCP) throws UnknownHostException {
        InetAddress inetSend = InetAddress.getByName(inetSender);
        PeerSocketAddress peerSocketAddress = new PeerSocketAddress(inetSend, tcpPortSender, udpPortSender);
        PeerAddress n1 = new PeerAddress(idSender, peerSocketAddress, firewallTCP, firewallUDP, false, false, false,
                PeerAddress.EMPTY_PEER_SOCKET_ADDRESSES);
        return n1;
    }

    public static Message createDummyMessage(Number160 idSender, String inetSender, int tcpPortSendor,
            int udpPortSender, Number160 idRecipien, String inetRecipient, int tcpPortRecipient,
            int udpPortRecipient, byte command, Type type, boolean firewallUDP, boolean firewallTCP)
            throws UnknownHostException {
        Message message = new Message();
        PeerAddress n1 = createAddress(idSender, inetSender, tcpPortSendor, udpPortSender, firewallUDP,
                firewallTCP);
        message.sender(n1);
        //
        PeerAddress n2 = createAddress(idRecipien, inetRecipient, tcpPortRecipient, udpPortRecipient,
                firewallUDP, firewallTCP);
        message.recipient(n2);
        message.type(type);
        message.command(command);
        return message;
    }

    public static PeerDHT[] createNodes(int nrOfPeers, Random rnd, int port) throws Exception {
        return createNodes(nrOfPeers, rnd, port, null);
    }

    public static PeerDHT[] createNodes(int nrOfPeers, Random rnd, int port, AutomaticFuture automaticFuture)
            throws Exception {
        return createNodes(nrOfPeers, rnd, port, automaticFuture, false);
    }

    /**
     * Creates peers for testing. The first peer (peer[0]) will be used as the master. This means that shutting down
     * peer[0] will shut down all other peers
     * 
     * @param nrOfPeers
     *            The number of peers to create including the master
     * @param rnd
     *            The random object to create random peer IDs
     * @param port
     *            The port where the master peer will listen to
     * @return All the peers, with the master peer at position 0 -> peer[0]
     * @throws Exception
     *             If the creation of nodes fail.
     */
    public static PeerDHT[] createNodes(int nrOfPeers, Random rnd, int port, AutomaticFuture automaticFuture,
            boolean maintenance) throws Exception {
        if (nrOfPeers < 1) {
            throw new IllegalArgumentException("Cannot create less than 1 peer");
        }
        Bindings bindings = new Bindings();
        PeerDHT[] peers = new PeerDHT[nrOfPeers];
        final Peer master;
        if (automaticFuture != null) {
        	Number160 peerId = new Number160(rnd);
        	PeerMap peerMap = new PeerMap(new PeerMapConfiguration(peerId));
        	master = new PeerBuilder(peerId)
                    .ports(port).enableMaintenance(maintenance)
                    .bindings(bindings).peerMap(peerMap).start().addAutomaticFuture(automaticFuture);
            peers[0] = new PeerBuilderDHT(master).start(); 
            
        } else {
        	Number160 peerId = new Number160(rnd);
        	PeerMap peerMap = new PeerMap(new PeerMapConfiguration(peerId));
        	master = new PeerBuilder(peerId).enableMaintenance(maintenance).bindings(bindings)
                    .peerMap(peerMap).ports(port).start();
        	peers[0] = new PeerBuilderDHT(master).start(); 
        }

        for (int i = 1; i < nrOfPeers; i++) {
            if (automaticFuture != null) {
            	Number160 peerId = new Number160(rnd);
            	PeerMap peerMap = new PeerMap(new PeerMapConfiguration(peerId));
                Peer peer = new PeerBuilder(peerId)
                        .masterPeer(master)
                        .enableMaintenance(maintenance).enableMaintenance(maintenance).peerMap(peerMap).bindings(bindings).start().addAutomaticFuture(automaticFuture);
{'fraction_non_alphanumeric': 0.08598726114649681, 'fraction_numerical': 0.027600849256900213, 'mean_word_length': 3.080565983251516, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 12, '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': '4145187', 'n_tokens_mistral': 4322, 'n_tokens_neox': 3923, 'n_words': 1160}