df3b0e7056cdacc6777c2179b086e551a65667db
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / test / CallbackJVppFacadeTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openvpp.jvpp.test;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import org.openvpp.jvpp.JVpp;
22 import org.openvpp.jvpp.JVppImpl;
23 import org.openvpp.jvpp.VppJNIConnection;
24 import org.openvpp.jvpp.callback.JVppCallback;
25 import org.openvpp.jvpp.callback.ShowVersionCallback;
26 import org.openvpp.jvpp.callfacade.CallbackJVppFacade;
27 import org.openvpp.jvpp.callfacade.CallbackJVppFacadeCallback;
28
29 /**
30  * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request.
31  * This is more convenient than the approach shown in CallbackApiTest.
32  */
33 public class CallbackJVppFacadeTest {
34
35     private static ShowVersionCallback showVersionCallback1 = msg ->
36             System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, retval=%d, program=%s," +
37                             "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, msg.retval, new String(msg.program),
38                     new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
39
40     private static ShowVersionCallback showVersionCallback2 = msg ->
41             System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, retval=%d, program=%s," +
42                             "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, msg.retval, new String(msg.program),
43                     new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
44
45     private static void testCallbackFacade() throws Exception {
46         System.out.println("Testing CallbackJVppFacade");
47
48         final Map<Integer, JVppCallback> callbackMap = new HashMap<>();
49         JVpp impl = new JVppImpl(VppJNIConnection.create("CallbackApiTest", new CallbackJVppFacadeCallback(callbackMap)));
50         CallbackJVppFacade jvpp = new CallbackJVppFacade(impl, callbackMap);
51         System.out.println("Successfully connected to VPP");
52
53         jvpp.showVersion(showVersionCallback1);
54         jvpp.showVersion(showVersionCallback2);
55
56
57         Thread.sleep(2000);
58
59         System.out.println("Disconnecting...");
60         impl.close();
61         Thread.sleep(1000);
62     }
63
64     public static void main(String[] args) throws Exception {
65         testCallbackFacade();
66     }
67 }