001package edu.wpi.first.wpilibj.networktables; 002 003import edu.wpi.first.wpilibj.tables.*; 004 005import java.io.File; 006import java.io.InputStream; 007import java.io.OutputStream; 008import java.io.FileOutputStream; 009import java.io.IOException; 010import java.nio.ByteBuffer; 011 012public class NetworkTablesJNI { 013 static boolean libraryLoaded = false; 014 static File jniLibrary = null; 015 static { 016 if (!libraryLoaded) { 017 try { 018 System.loadLibrary("ntcore"); 019 } catch (UnsatisfiedLinkError e) { 020 try { 021 String osname = System.getProperty("os.name"); 022 String resname; 023 if (osname.startsWith("Windows")) 024 resname = "/Windows/" + System.getProperty("os.arch") + "/"; 025 else 026 resname = "/" + osname + "/" + System.getProperty("os.arch") + "/"; 027 System.out.println("platform: " + resname); 028 if (osname.startsWith("Windows")) 029 resname += "ntcore.dll"; 030 else if (osname.startsWith("Mac")) 031 resname += "libntcore.dylib"; 032 else 033 resname += "libntcore.so"; 034 InputStream is = NetworkTablesJNI.class.getResourceAsStream(resname); 035 if (is != null) { 036 // create temporary file 037 if (System.getProperty("os.name").startsWith("Windows")) 038 jniLibrary = File.createTempFile("NetworkTablesJNI", ".dll"); 039 else if (System.getProperty("os.name").startsWith("Mac")) 040 jniLibrary = File.createTempFile("libNetworkTablesJNI", ".dylib"); 041 else 042 jniLibrary = File.createTempFile("libNetworkTablesJNI", ".so"); 043 // flag for delete on exit 044 jniLibrary.deleteOnExit(); 045 OutputStream os = new FileOutputStream(jniLibrary); 046 047 byte[] buffer = new byte[1024]; 048 int readBytes; 049 try { 050 while ((readBytes = is.read(buffer)) != -1) { 051 os.write(buffer, 0, readBytes); 052 } 053 } finally { 054 os.close(); 055 is.close(); 056 } 057 System.load(jniLibrary.getAbsolutePath()); 058 } else { 059 System.loadLibrary("ntcore"); 060 } 061 } catch (IOException ex) { 062 ex.printStackTrace(); 063 System.exit(1); 064 } 065 } 066 libraryLoaded = true; 067 } 068 } 069 070 public static native boolean containsKey(String key); 071 public static native int getType(String key); 072 073 public static native boolean putBoolean(String key, boolean value); 074 public static native boolean putDouble(String key, double value); 075 public static native boolean putString(String key, String value); 076 public static native boolean putRaw(String key, byte[] value); 077 public static native boolean putRaw(String key, ByteBuffer value, int len); 078 public static native boolean putBooleanArray(String key, boolean[] value); 079 public static native boolean putDoubleArray(String key, double[] value); 080 public static native boolean putStringArray(String key, String[] value); 081 082 public static native void forcePutBoolean(String key, boolean value); 083 public static native void forcePutDouble(String key, double value); 084 public static native void forcePutString(String key, String value); 085 public static native void forcePutRaw(String key, byte[] value); 086 public static native void forcePutRaw(String key, ByteBuffer value, int len); 087 public static native void forcePutBooleanArray(String key, boolean[] value); 088 public static native void forcePutDoubleArray(String key, double[] value); 089 public static native void forcePutStringArray(String key, String[] value); 090 091 public static native Object getValue(String key) throws TableKeyNotDefinedException; 092 public static native boolean getBoolean(String key) throws TableKeyNotDefinedException; 093 public static native double getDouble(String key) throws TableKeyNotDefinedException; 094 public static native String getString(String key) throws TableKeyNotDefinedException; 095 public static native byte[] getRaw(String key) throws TableKeyNotDefinedException; 096 public static native boolean[] getBooleanArray(String key) throws TableKeyNotDefinedException; 097 public static native double[] getDoubleArray(String key) throws TableKeyNotDefinedException; 098 public static native String[] getStringArray(String key) throws TableKeyNotDefinedException; 099 100 public static native Object getValue(String key, Object defaultValue); 101 public static native boolean getBoolean(String key, boolean defaultValue); 102 public static native double getDouble(String key, double defaultValue); 103 public static native String getString(String key, String defaultValue); 104 public static native byte[] getRaw(String key, byte[] defaultValue); 105 public static native boolean[] getBooleanArray(String key, boolean[] defaultValue); 106 public static native double[] getDoubleArray(String key, double[] defaultValue); 107 public static native String[] getStringArray(String key, String[] defaultValue); 108 109 public static native boolean setDefaultBoolean(String key, boolean defaultValue); 110 public static native boolean setDefaultDouble(String key, double defaultValue); 111 public static native boolean setDefaultString(String key, String defaultValue); 112 public static native boolean setDefaultRaw(String key, byte[] defaultValue); 113 public static native boolean setDefaultBooleanArray(String key, boolean[] defaultValue); 114 public static native boolean setDefaultDoubleArray(String key, double[] defaultValue); 115 public static native boolean setDefaultStringArray(String key, String[] defaultValue); 116 117 public static native void setEntryFlags(String key, int flags); 118 public static native int getEntryFlags(String key); 119 120 public static native void deleteEntry(String key); 121 public static native void deleteAllEntries(); 122 123 public static native EntryInfo[] getEntries(String prefix, int types); 124 125 public static native void flush(); 126 127 @FunctionalInterface 128 public interface EntryListenerFunction { 129 void apply(int uid, String key, Object value, int flags); 130 } 131 public static native int addEntryListener(String prefix, EntryListenerFunction listener, int flags); 132 public static native void removeEntryListener(int entryListenerUid); 133 134 @FunctionalInterface 135 public interface ConnectionListenerFunction { 136 void apply(int uid, boolean connected, ConnectionInfo conn); 137 } 138 public static native int addConnectionListener(ConnectionListenerFunction listener, boolean immediateNotify); 139 public static native void removeConnectionListener(int connListenerUid); 140 141 // public static native void createRpc(String key, byte[] def, IRpc rpc); 142 // public static native void createRpc(String key, ByteBuffer def, int def_len, IRpc rpc); 143 public static native byte[] getRpc(String key) throws TableKeyNotDefinedException; 144 public static native byte[] getRpc(String key, byte[] defaultValue); 145 public static native int callRpc(String key, byte[] params); 146 public static native int callRpc(String key, ByteBuffer params, int params_len); 147 // public static native byte[] getRpcResultBlocking(int callUid); 148 // public static native byte[] getRpcResultNonblocking(int callUid) throws RpcNoResponseException; 149 150 public static native void setNetworkIdentity(String name); 151 public static native void startServer(String persistFilename, String listenAddress, int port); 152 public static native void stopServer(); 153 public static native void startClient(); 154 public static native void startClient(String serverName, int port); 155 public static native void startClient(String[] serverNames, int[] ports); 156 public static native void stopClient(); 157 public static native void setServer(String serverName, int port); 158 public static native void setServer(String[] serverNames, int[] ports); 159 public static native void startDSClient(int port); 160 public static native void stopDSClient(); 161 public static native void setUpdateRate(double interval); 162 163 public static native ConnectionInfo[] getConnections(); 164 165 public static native void savePersistent(String filename) throws PersistentException; 166 public static native String[] loadPersistent(String filename) throws PersistentException; // returns warnings 167 168 public static native long now(); 169 170 @FunctionalInterface 171 public interface LoggerFunction { 172 void apply(int level, String file, int line, String msg); 173 } 174 public static native void setLogger(LoggerFunction func, int minLevel); 175}