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
011/** WPINet JNI. */
012public class WPINetJNI {
013  static boolean libraryLoaded = false;
014  static RuntimeLoader<WPINetJNI> loader = null;
015
016  /** Sets whether JNI should be loaded in the static block. */
017  public static class Helper {
018    private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
019
020    /**
021     * Returns true if the JNI should be loaded in the static block.
022     *
023     * @return True if the JNI should be loaded in the static block.
024     */
025    public static boolean getExtractOnStaticLoad() {
026      return extractOnStaticLoad.get();
027    }
028
029    /**
030     * Sets whether the JNI should be loaded in the static block.
031     *
032     * @param load Whether the JNI should be loaded in the static block.
033     */
034    public static void setExtractOnStaticLoad(boolean load) {
035      extractOnStaticLoad.set(load);
036    }
037
038    /** Utility class. */
039    private Helper() {}
040  }
041
042  static {
043    if (Helper.getExtractOnStaticLoad()) {
044      try {
045        loader =
046            new RuntimeLoader<>(
047                "wpinetjni", RuntimeLoader.getDefaultExtractionRoot(), WPINetJNI.class);
048        loader.loadLibrary();
049      } catch (IOException ex) {
050        ex.printStackTrace();
051        System.exit(1);
052      }
053      libraryLoaded = true;
054    }
055  }
056
057  /**
058   * Force load the library.
059   *
060   * @throws IOException if the library failed to load
061   */
062  public static synchronized void forceLoad() throws IOException {
063    if (libraryLoaded) {
064      return;
065    }
066    loader =
067        new RuntimeLoader<>("wpinetjni", RuntimeLoader.getDefaultExtractionRoot(), WPINetJNI.class);
068    loader.loadLibrary();
069    libraryLoaded = true;
070  }
071
072  /**
073   * Forward a local TCP port to a remote host and port. Note that local ports less than 1024 won't
074   * work as a normal user.
075   *
076   * @param port local port number
077   * @param remoteHost remote IP address / DNS name
078   * @param remotePort remote port number
079   */
080  public static native void addPortForwarder(int port, String remoteHost, int remotePort);
081
082  /**
083   * Stop TCP forwarding on a port.
084   *
085   * @param port local port number
086   */
087  public static native void removePortForwarder(int port);
088
089  /**
090   * Creates a MulticastServiceAnnouncer.
091   *
092   * @param serviceName service name
093   * @param serviceType service type
094   * @param port port
095   * @param keys keys
096   * @param values values
097   * @return MulticastServiceAnnouncer handle.
098   */
099  public static native int createMulticastServiceAnnouncer(
100      String serviceName, String serviceType, int port, String[] keys, String[] values);
101
102  /**
103   * Frees a MulticastServiceAnnouncer.
104   *
105   * @param handle MulticastServiceAnnouncer handle.
106   */
107  public static native void freeMulticastServiceAnnouncer(int handle);
108
109  /**
110   * Starts MulticastServiceAnnouncer.
111   *
112   * @param handle MulticastServiceAnnouncer handle.
113   */
114  public static native void startMulticastServiceAnnouncer(int handle);
115
116  /**
117   * Stops MulticastServiceAnnouncer.
118   *
119   * @param handle MulticastServiceAnnouncer handle.
120   */
121  public static native void stopMulticastServiceAnnouncer(int handle);
122
123  /**
124   * Returns true if MulticastServiceAnnouncer has an implementation.
125   *
126   * @param handle MulticastServiceAnnouncer handle.
127   * @return True if MulticastServiceAnnouncer has an implementation.
128   */
129  public static native boolean getMulticastServiceAnnouncerHasImplementation(int handle);
130
131  /**
132   * Creates a MulticastServiceResolver.
133   *
134   * @param serviceType Service type.
135   * @return MulticastServiceResolver handle.
136   */
137  public static native int createMulticastServiceResolver(String serviceType);
138
139  /**
140   * Frees MulticastServiceResolver.
141   *
142   * @param handle MulticastServiceResolver handle.
143   */
144  public static native void freeMulticastServiceResolver(int handle);
145
146  /**
147   * Starts MulticastServiceResolver.
148   *
149   * @param handle MulticastServiceResolver handle.
150   */
151  public static native void startMulticastServiceResolver(int handle);
152
153  /**
154   * Stops MulticastServiceResolver.
155   *
156   * @param handle MulticastServiceResolver handle.
157   */
158  public static native void stopMulticastServiceResolver(int handle);
159
160  /**
161   * Returns true if MulticastServiceResolver has an implementation.
162   *
163   * @param handle MulticastServiceResolver handle.
164   * @return True if MulticastServiceResolver has an implementation.
165   */
166  public static native boolean getMulticastServiceResolverHasImplementation(int handle);
167
168  /**
169   * Returns event handle for MulticastServiceResolver.
170   *
171   * @param handle MulticastServiceResolver handle.
172   * @return Event handle for MulticastServiceResolver.
173   */
174  public static native int getMulticastServiceResolverEventHandle(int handle);
175
176  /**
177   * Returns service data for MulticastServiceResolver.
178   *
179   * @param handle MulticastServiceResolver handle.
180   * @return Service data for MulticastServiceResolver.
181   */
182  public static native ServiceData[] getMulticastServiceResolverData(int handle);
183
184  /** Utility class. */
185  private WPINetJNI() {}
186}