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.util;
006
007import java.io.IOException;
008import java.util.concurrent.atomic.AtomicBoolean;
009
010public final class WPIUtilJNI {
011  static boolean libraryLoaded = false;
012  static RuntimeLoader<WPIUtilJNI> loader = null;
013
014  public static class Helper {
015    private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
016
017    public static boolean getExtractOnStaticLoad() {
018      return extractOnStaticLoad.get();
019    }
020
021    public static void setExtractOnStaticLoad(boolean load) {
022      extractOnStaticLoad.set(load);
023    }
024  }
025
026  static {
027    if (Helper.getExtractOnStaticLoad()) {
028      try {
029        loader =
030            new RuntimeLoader<>(
031                "wpiutiljni", RuntimeLoader.getDefaultExtractionRoot(), WPIUtilJNI.class);
032        loader.loadLibrary();
033      } catch (IOException ex) {
034        ex.printStackTrace();
035        System.exit(1);
036      }
037      libraryLoaded = true;
038    }
039  }
040
041  /**
042   * Force load the library.
043   *
044   * @throws IOException if the library failed to load
045   */
046  public static synchronized void forceLoad() throws IOException {
047    if (libraryLoaded) {
048      return;
049    }
050    loader =
051        new RuntimeLoader<>(
052            "wpiutiljni", RuntimeLoader.getDefaultExtractionRoot(), WPIUtilJNI.class);
053    loader.loadLibrary();
054    libraryLoaded = true;
055  }
056
057  public static native void enableMockTime();
058
059  public static native void disableMockTime();
060
061  public static native void setMockTime(long time);
062
063  public static native long now();
064
065  public static native long getSystemTime();
066
067  public static native void addPortForwarder(int port, String remoteHost, int remotePort);
068
069  public static native void removePortForwarder(int port);
070
071  public static native int createEvent(boolean manualReset, boolean initialState);
072
073  public static native void destroyEvent(int eventHandle);
074
075  public static native void setEvent(int eventHandle);
076
077  public static native void resetEvent(int eventHandle);
078
079  public static native int createSemaphore(int initialCount, int maximumCount);
080
081  public static native void destroySemaphore(int semHandle);
082
083  public static native boolean releaseSemaphore(int semHandle, int releaseCount);
084
085  /**
086   * Waits for an handle to be signaled.
087   *
088   * @param handle handle to wait on
089   * @throws InterruptedException on failure (e.g. object was destroyed)
090   */
091  public static native void waitForObject(int handle) throws InterruptedException;
092
093  /**
094   * Waits for an handle to be signaled, with timeout.
095   *
096   * @param handle handle to wait on
097   * @param timeout timeout in seconds
098   * @return True if timeout reached without handle being signaled
099   * @throws InterruptedException on failure (e.g. object was destroyed)
100   */
101  public static native boolean waitForObjectTimeout(int handle, double timeout)
102      throws InterruptedException;
103
104  /**
105   * Waits for one or more handles to be signaled.
106   *
107   * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit
108   * set for any invalid handles.
109   *
110   * @param handles array of handles to wait on
111   * @return array of signaled handles
112   * @throws InterruptedException on failure (e.g. no objects were signaled)
113   */
114  public static native int[] waitForObjects(int[] handles) throws InterruptedException;
115
116  /**
117   * Waits for one or more handles to be signaled, with timeout.
118   *
119   * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit
120   * set for any invalid handles.
121   *
122   * @param handles array of handles to wait on
123   * @param timeout timeout in seconds
124   * @return array of signaled handles; empty if timeout reached without any handle being signaled
125   * @throws InterruptedException on failure (e.g. no objects were signaled and no timeout)
126   */
127  public static native int[] waitForObjectsTimeout(int[] handles, double timeout)
128      throws InterruptedException;
129
130  public static native int createMulticastServiceAnnouncer(
131      String serviceName, String serviceType, int port, String[] keys, String[] values);
132
133  public static native void freeMulticastServiceAnnouncer(int handle);
134
135  public static native void startMulticastServiceAnnouncer(int handle);
136
137  public static native void stopMulticastServiceAnnouncer(int handle);
138
139  public static native boolean getMulticastServiceAnnouncerHasImplementation(int handle);
140
141  public static native int createMulticastServiceResolver(String serviceType);
142
143  public static native void freeMulticastServiceResolver(int handle);
144
145  public static native void startMulticastServiceResolver(int handle);
146
147  public static native void stopMulticastServiceResolver(int handle);
148
149  public static native boolean getMulticastServiceResolverHasImplementation(int handle);
150
151  public static native int getMulticastServiceResolverEventHandle(int handle);
152
153  public static native ServiceData[] getMulticastServiceResolverData(int handle);
154}