Development Guide

Example - First Connection

Let's create our own connection that will do the following two things:

  1. Pretend to receive data from client (server) and send it through the PETEP core.
  2. Pretend to write the data to server (client) and instead write it to logger.
connection.ExampleConnection
public class ExampleConnection extends Connection {
  private String name;
  private ExecutorService executor;
  private boolean running;

  public ExampleConnection(int id, Proxy proxy, String name) {
    super(id, proxy);
    this.name = name;
    executor = Executors.newFixedThreadPool(4);
  }

  @Override
  public boolean start() {
    Logger.getGlobal().info("Example connection '" + name + "' (ID: " + id + ") started!");

    running = true;

    // Pretend reading data from client.
    executor.execute(
        () -> {
          while (running) {
            ExamplePdu pdu =
                new ExamplePdu(
                    proxy,
                    this,
                    PduDestination.SERVER,
                    new byte[] {01, 02, 03, 04, 00, 00, 00, 00},
                    4,
                    "Request from client",
                    1234);

            process(pdu);

            try {
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              break;
            }
          }
        });

    // Pretend reading data from server.
    executor.execute(
        () -> {
          while (running) {
            ExamplePdu pdu =
                new ExamplePdu(
                    proxy,
                    this,
                    PduDestination.CLIENT,
                    new byte[] {04, 03, 02, 01, 00, 00, 00, 00},
                    4,
                    "Request from server",
                    4321);

            process(pdu);

            try {
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              break;
            }
          }
        });

    // Pretend writing data to server.
    executor.execute(
        () -> {
          ExamplePdu pdu;

          try {
            while ((pdu = (ExamplePdu) queueC2S.take()) != null) {
              Logger.getGlobal()
                  .info(
                      "Example connection '"
                          + name
                          + "' (ID: "
                          + id
                          + ") read data: '"
                          + PduUtils.bufferToString(pdu)
                          + "' ['"
                          + pdu.getStringParam()
                          + "', "
                          + pdu.getIntegerParam()
                          + "] from  client");
            }
          } catch (InterruptedException e) {
            // Interrupted.
          }
        });

    // Pretend writing data to server.
    executor.execute(
        () -> {
          ExamplePdu pdu;

          try {
            while ((pdu = (ExamplePdu) queueS2C.take()) != null) {
              Logger.getGlobal()
                  .info(
                      "Example connection '"
                          + name
                          + "' (ID: "
                          + id
                          + ") read data: '"
                          + PduUtils.bufferToString(pdu)
                          + "' ['"
                          + pdu.getStringParam()
                          + "', "
                          + pdu.getIntegerParam()
                          + "] from  server");
            }
          } catch (InterruptedException e) {
            // Interrupted.
          }
        });

    return true;
  }

  @Override
  public void stop() {
    running = false;

    executor.shutdownNow();

    Logger.getGlobal().info("Example connection '" + name + "' (ID: " + id + ") stopped!");
  }

  @Override
  public String toString() {
    return "Example connection '" + name + "' (ID: " + id + ")";
  }
}

Connection Manager

Now, let's create connection manager that will manage our connections by simply extending DefaultConnectionManager, so we can easily change it in the future.

connection.ExampleConnectionManager
public class ExampleConnectionManager extends DefaultConnectionManager {}
Získejte registraci domén s tld .online, .space, .store, .tech zdarma!
Stačí si k jedné z těchto domén vybrat hosting Plus nebo Mega a registraci domény od nás dostanete za 0 Kč!
Objednat