summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/jvpp-stats/io/fd/jvpp/stats/JVppClientStatsImpl.java60
-rw-r--r--java/jvpp-stats/io/fd/jvpp/stats/test/JvppStatsApiTest.java13
-rw-r--r--java/jvpp-stats/jvpp_stats.c23
3 files changed, 73 insertions, 23 deletions
diff --git a/java/jvpp-stats/io/fd/jvpp/stats/JVppClientStatsImpl.java b/java/jvpp-stats/io/fd/jvpp/stats/JVppClientStatsImpl.java
index 821373a..32d2345 100644
--- a/java/jvpp-stats/io/fd/jvpp/stats/JVppClientStatsImpl.java
+++ b/java/jvpp-stats/io/fd/jvpp/stats/JVppClientStatsImpl.java
@@ -19,20 +19,17 @@ package io.fd.jvpp.stats;
import static io.fd.jvpp.NativeLibraryLoader.loadLibrary;
import static java.lang.String.format;
+import io.fd.jvpp.stats.dto.InterfaceStatistics;
import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
-import java.nio.file.attribute.PosixFilePermission;
-import java.nio.file.attribute.PosixFilePermissions;
-import java.util.Set;
+import java.util.Arrays;
+import java.util.List;
+import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
-import io.fd.jvpp.stats.dto.InterfaceStatistics;
+public class JVppClientStatsImpl implements AutoCloseable {
-public class JVppClientStatsImpl {
+ private boolean connected;
private static final Logger LOG = Logger.getLogger(JVppClientStatsImpl.class.getName());
@@ -46,5 +43,48 @@ public class JVppClientStatsImpl {
}
}
- public static native InterfaceStatistics[] interfaceStatisticsDump();
+ private static native InterfaceStatistics[] interfaceStatisticsDump();
+ private static native int statSegmentConnect();
+ private static native void statSegmentDisconnect();
+
+ public synchronized List<InterfaceStatistics> getInterfaceStatistics() {
+ if (!this.connected) {
+ LOG.severe("Unable to dump statistics. Client isn't connected. Try reconnecting.");
+ return null;
+ }
+ InterfaceStatistics[] statDump = interfaceStatisticsDump();
+ List<InterfaceStatistics> statistics = new LinkedList();
+ if (statDump != null) {
+ statistics = Arrays.asList(statDump);
+ }
+ return statistics;
+ }
+
+ public JVppClientStatsImpl() {
+ connectClient();
+ }
+
+ public boolean reconnect() {
+ connectClient();
+ return this.connected;
+ }
+
+ private void connectClient() {
+ if (!this.connected) {
+ if (statSegmentConnect() == 0) {
+ this.connected = true;
+ }
+ }
+ }
+
+ public boolean isConnected() {
+ return connected;
+ }
+
+ public void close() {
+ if (this.connected) {
+ statSegmentDisconnect();
+ this.connected = false;
+ }
+ }
}
diff --git a/java/jvpp-stats/io/fd/jvpp/stats/test/JvppStatsApiTest.java b/java/jvpp-stats/io/fd/jvpp/stats/test/JvppStatsApiTest.java
index 5077509..2f9cf9f 100644
--- a/java/jvpp-stats/io/fd/jvpp/stats/test/JvppStatsApiTest.java
+++ b/java/jvpp-stats/io/fd/jvpp/stats/test/JvppStatsApiTest.java
@@ -18,26 +18,27 @@ package io.fd.jvpp.stats.test;
import io.fd.jvpp.stats.JVppClientStatsImpl;
import io.fd.jvpp.stats.dto.InterfaceStatistics;
+import java.util.List;
class JvppStatsApiTest {
public static void main(String[] args) {
JVppClientStatsImpl myImpl = new JVppClientStatsImpl();
- InterfaceStatistics[] statistics = myImpl.interfaceStatisticsDump();
+ List<InterfaceStatistics> statistics = myImpl.getInterfaceStatistics();
if (statistics == null) {
System.out.println("in java - dump was null");
- return;
+ System.exit(1);
}
- if (statistics.length == 0) {
+ if (statistics.size() == 0) {
System.out.println("in java - dump was empty");
- return;
+ System.exit(2);
}
- for (int i = 0; i < statistics.length; i++) {
- System.out.println(statistics[i].toString());
+ for (InterfaceStatistics stat: statistics) {
+ System.out.println(stat.toString());
}
}
}
diff --git a/java/jvpp-stats/jvpp_stats.c b/java/jvpp-stats/jvpp_stats.c
index 404fe0a..78d69f5 100644
--- a/java/jvpp-stats/jvpp_stats.c
+++ b/java/jvpp-stats/jvpp_stats.c
@@ -47,15 +47,8 @@ void set_field(int_stats_t* stats, int index, const char* field_name, int packet
stat_segment_data_t* get_statistics_dump() {
u8 **patterns = 0;
u32 *dir;
- u8 * stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
vec_add1(patterns, (u8*)"/if/rx");
vec_add1(patterns, (u8*)"/if/tx");
- int rv = stat_segment_connect((char*)stat_segment_name);
- if (rv < 0) {
- fprintf(stderr, "Couldn't connect to %s. Check if socket exists/permissions.(ret stat: %d)\n",
- stat_segment_name, rv);
- return NULL;
- }
dir = stat_segment_ls(patterns);
return stat_segment_dump(dir);
}
@@ -125,3 +118,19 @@ JNIEXPORT jobjectArray JNICALL Java_io_fd_jvpp_stats_JVppClientStatsImpl_interfa
}
return retArray;
}
+
+JNIEXPORT jint JNICALL Java_io_fd_jvpp_stats_JVppClientStatsImpl_statSegmentConnect(JNIEnv *env, jclass jclazz) {
+ u8 * stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
+ int rv = stat_segment_connect((char*)stat_segment_name);
+ if (rv < 0) {
+ fprintf(stderr, "Couldn't connect to %s. Check if socket exists/permissions.(ret stat: %d)\n",
+ stat_segment_name, rv);
+ return -1;
+ }
+ return 0;
+}
+
+JNIEXPORT void JNICALL Java_io_fd_jvpp_stats_JVppClientStatsImpl_statSegmentDisconnect(JNIEnv *env, jclass jclazz) {
+ stat_segment_disconnect();
+ return;
+}