001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.net;
006
007import edu.wpi.first.util.RuntimeLoader;
008import java.io.IOException;
009import java.util.concurrent.atomic.AtomicBoolean;
010
011public class WPINetJNI {
012  static boolean libraryLoaded = false;
013  static RuntimeLoader<WPINetJNI> loader = null;
014
015  public static class Helper {
016    private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
017
018    public static boolean getExtractOnStaticLoad() {
019      return extractOnStaticLoad.get();
020    }
021
022    public static void setExtractOnStaticLoad(boolean load) {
023      extractOnStaticLoad.set(load);
024    }
025  }
026
027  static {
028    if (Helper.getExtractOnStaticLoad()) {
029      try {
030        loader =
031            new RuntimeLoader<>(
032                "wpinetjni", RuntimeLoader.getDefaultExtractionRoot(), WPINetJNI.class);
033        loader.loadLibrary();
034      } catch (IOException ex) {
035        ex.printStackTrace();
036        System.exit(1);
037      }
038      libraryLoaded = true;
039    }
040  }
041
042  /**
043   * Force load the library.
044   *
045   * @throws IOException if the library failed to load
046   */
047  public static synchronized void forceLoad() throws IOException {
048    if (libraryLoaded) {
049      return;
050    }
051    loader =
052        new RuntimeLoader<>("wpinetjni", RuntimeLoader.getDefaultExtractionRoot(), WPINetJNI.class);
053    loader.loadLibrary();
054    libraryLoaded = true;
055  }
056
057  public static native void addPortForwarder(int port, String remoteHost, int remotePort);
058
059  public static native void removePortForwarder(int port);
060
061  public static native int createMulticastServiceAnnouncer(
062      String serviceName, String serviceType, int port, String[] keys, String[] values);
063
064  public static native void freeMulticastServiceAnnouncer(int handle);
065
066  public static native void startMulticastServiceAnnouncer(int handle);
067
068  public static native void stopMulticastServiceAnnouncer(int handle);
069
070  public static native boolean getMulticastServiceAnnouncerHasImplementation(int handle);
071
072  public static native int createMulticastServiceResolver(String serviceType);
073
074  public static native void freeMulticastServiceResolver(int handle);
075
076  public static native void startMulticastServiceResolver(int handle);
077
078  public static native void stopMulticastServiceResolver(int handle);
079
080  public static native boolean getMulticastServiceResolverHasImplementation(int handle);
081
082  public static native int getMulticastServiceResolverEventHandle(int handle);
083
084  public static native ServiceData[] getMulticastServiceResolverData(int handle);
085}