2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openvpp.jvpp;
19 import static org.openvpp.jvpp.NativeLibraryLoader.loadLibrary;
21 import java.io.IOException;
22 import java.util.HashMap;
24 import java.util.Objects;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
29 * JNI based representation of a management connection to VPP.
31 public final class VppJNIConnection implements VppConnection {
32 private static final Logger LOG = Logger.getLogger(VppJNIConnection.class.getName());
35 final String libName = "libjvpp_registry.so.0.0.0";
37 loadLibrary(libName, VppJNIConnection.class);
38 } catch (IOException e) {
39 LOG.log(Level.SEVERE, String.format("Can't find vpp jni library: %s", libName), e);
40 throw new ExceptionInInitializerError(e);
44 private ConnectionInfo connectionInfo;
46 private final String clientName;
47 private volatile boolean disconnected = false;
50 * Create VPPJNIConnection instance for client connecting to VPP.
52 * @param clientName client name instance to be used for communication. Single connection per clientName is
55 public VppJNIConnection(final String clientName) {
56 this.clientName = Objects.requireNonNull(clientName, "Null clientName");
60 * Guarded by VppJNIConnection.class
62 private static final Map<String, VppJNIConnection> connections = new HashMap<>();
65 * Initiate VPP connection for current instance
67 * Multiple instances are allowed since this class is not a singleton (VPP allows multiple management connections).
69 * However only a single connection per clientName is allowed.
71 * @throws IOException in case the connection could not be established
75 public void connect() throws IOException {
79 private void _connect() throws IOException {
80 synchronized (VppJNIConnection.class) {
81 if (connections.containsKey(clientName)) {
82 throw new IOException("Client " + clientName + " already connected");
85 connectionInfo = clientConnect(clientName);
86 if (connectionInfo.status != 0) {
87 throw new IOException("Connection returned error " + connectionInfo.status);
89 connections.put(clientName, this);
94 public final void checkActive() {
96 throw new IllegalStateException("Disconnected client " + clientName);
101 public final synchronized void close() {
107 synchronized (VppJNIConnection.class) {
108 connections.remove(clientName);
114 public ConnectionInfo getConnectionInfo() {
115 return connectionInfo;
119 * VPP connection information used by plugins to reuse the connection.
121 public static final class ConnectionInfo {
122 public final long queueAddress;
123 public final int clientIndex;
124 public final int status; // FIXME throw exception instead
126 public ConnectionInfo(long queueAddress, int clientIndex, int status) {
127 this.queueAddress = queueAddress;
128 this.clientIndex = clientIndex;
129 this.status = status;
133 private static native ConnectionInfo clientConnect(String clientName);
135 private static native void clientDisconnect();