All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tron.p2p.example.ImportUsing Maven / Gradle / Ivy

The newest version!
package org.tron.p2p.example;

import org.apache.commons.lang3.ArrayUtils;
import org.tron.p2p.P2pConfig;
import org.tron.p2p.P2pEventHandler;
import org.tron.p2p.P2pService;
import org.tron.p2p.connection.Channel;
import org.tron.p2p.discover.Node;
import org.tron.p2p.exception.P2pException;
import org.tron.p2p.stats.P2pStats;
import org.tron.p2p.utils.ByteArray;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ImportUsing {

  private P2pService p2pService = new P2pService();
  private Map channels = new ConcurrentHashMap<>();

  public void startP2pService() {
    // config p2p parameters
    P2pConfig config = new P2pConfig();
    initConfig(config);

    // register p2p event handler
    MyP2pEventHandler myP2pEventHandler = new MyP2pEventHandler();
    try {
      p2pService.register(myP2pEventHandler);
    } catch (P2pException e) {
      // todo process exception
    }

    // start p2p service
    p2pService.start(config);

    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }

    // send message
    TestMessage testMessage = new TestMessage(ByteArray.fromString("hello"));
    for (Channel channel : channels.values()) {
      channel.send(ByteArray.fromObject(testMessage));
    }

    // close channel
    for (Channel channel : channels.values()) {
      channel.close();
    }
  }

  public void closeP2pService() {
    p2pService.close();
  }

  public void connect(InetSocketAddress address) {
    p2pService.connect(address);
  }

  public P2pStats getP2pStats() {
    return p2pService.getP2pStats();
  }

  public List getAllNodes() {
    return p2pService.getAllNodes();
  }

  public List getTableNodes() {
    return p2pService.getTableNodes();
  }

  public List getConnectableNodes() {
    return p2pService.getConnectableNodes();
  }

  private void initConfig(P2pConfig config) {
    // set p2p version
    config.setVersion(11111);

    // set tcp and udp listen port
    config.setPort(18888);

    // turn node discovery on or off
    config.setDiscoverEnable(true);

    // set discover seed nodes
    List seedNodeList = new ArrayList<>();
    seedNodeList.add(new InetSocketAddress("13.124.62.58", 18888));
    seedNodeList.add(new InetSocketAddress("18.196.99.16", 18888));
    seedNodeList.add(new InetSocketAddress("35.180.51.163", 18888));
    seedNodeList.add(new InetSocketAddress("34.253.187.192", 18888));
    seedNodeList.add(new InetSocketAddress("52.53.189.99", 18888));
    config.setSeedNodes(seedNodeList);

    // set active nodes
    List activeNodeList = new ArrayList<>();
    activeNodeList.add(new InetSocketAddress("127.0.0.2", 18888));
    activeNodeList.add(new InetSocketAddress("127.0.0.3", 18888));
    config.setActiveNodes(activeNodeList);

    // set trust nodes
    List trustNodeList = new ArrayList<>();
    trustNodeList.add((new InetSocketAddress("127.0.0.2", 18888)).getAddress());
    config.setTrustNodes(trustNodeList);

    // set the minimum number of connections
    config.setMinConnections(8);

    // set the minimum number of actively established connections
    config.setMinActiveConnections(2);

    // set the maximum number of connections
    config.setMaxConnections(30);

    // set the maximum number of connections with the same IP
    config.setMaxConnectionsWithSameIp(2);
  }

  public class MyP2pEventHandler extends P2pEventHandler {

    public MyP2pEventHandler() {
      this.messageTypes = new HashSet<>();
      this.messageTypes.add(MessageTypes.TEST.getType());
    }

    @Override
    public void onConnect(Channel channel) {
      channels.put(channel.getInetSocketAddress(), channel);
    }

    @Override
    public void onDisconnect(Channel channel) {
      channels.remove(channel.getInetSocketAddress());
    }

    @Override
    public void onMessage(Channel channel, byte[] data) {
      byte type = data[0];
      byte[] messageData = ArrayUtils.subarray(data, 1, data.length);
      switch (MessageTypes.fromByte(type)) {
        case TEST:
          TestMessage message = new TestMessage(messageData);
          // process TestMessage
          break;
        default:
          // todo
      }
    }

  }

  public enum MessageTypes {

    FIRST((byte)0x00),

    TEST((byte)0x01),

    LAST((byte)0x8f);

    private final byte type;

    MessageTypes(byte type) {
      this.type = type;
    }

    public byte getType() {
      return type;
    }

    private static final Map map = new HashMap<>();

    static {
      for (MessageTypes value : values()) {
        map.put(value.type, value);
      }
    }

    public static MessageTypes fromByte(byte type) {
      return map.get(type);
    }
  }

  public class TestMessage {
    protected MessageTypes type;
    protected byte[] data;
    public TestMessage(byte[] data) {
      this.type = MessageTypes.TEST;
      this.data = data;
    }

  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy