VPP-1525: additional fixes for strings in Japi
[vpp.git] / extras / japi / java / jvpp-core / io / fd / vpp / jvpp / core / examples / CallbackCliApiExample.java
1 /*
2  * Copyright (c) 2018 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 io.fd.vpp.jvpp.core.examples;
18
19 import io.fd.vpp.jvpp.JVpp;
20 import io.fd.vpp.jvpp.JVppRegistry;
21 import io.fd.vpp.jvpp.JVppRegistryImpl;
22 import io.fd.vpp.jvpp.VppCallbackException;
23 import io.fd.vpp.jvpp.core.JVppCoreImpl;
24 import io.fd.vpp.jvpp.core.callback.CliInbandReplyCallback;
25 import io.fd.vpp.jvpp.core.dto.CliInband;
26 import io.fd.vpp.jvpp.core.dto.CliInbandReply;
27
28 import java.nio.charset.StandardCharsets;
29
30 public class CallbackCliApiExample {
31
32     public static void main(String[] args) throws Exception {
33         testCallbackApi();
34     }
35
36     private static void testCallbackApi() throws Exception {
37         System.out.println("Testing Java callback API for Cli with JVppRegistry");
38         try (final JVppRegistry registry = new JVppRegistryImpl("CallbackCliApiExample");
39              final JVpp jvpp = new JVppCoreImpl()) {
40             registry.register(jvpp, new TestCallback());
41
42             System.out.println("Sending CliInband request...");
43             CliInband req = new CliInband();
44             req.cmd = "create loopback interface";
45             final int result = jvpp.send(req);
46             System.out.printf("CliInband send result = %d%n", result);
47
48             Thread.sleep(1000);
49             System.out.println("Disconnecting...");
50         }
51         Thread.sleep(1000);
52     }
53
54     static class TestCallback implements CliInbandReplyCallback {
55
56         @Override
57         public void onCliInbandReply(final CliInbandReply msg) {
58             System.out.printf("Received CliInbandReply: context=%d, reply=%s", msg.context, msg.reply);
59         }
60
61         @Override
62         public void onError(VppCallbackException ex) {
63             System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
64                 ex.getCtxId(), ex.getErrorCode());
65         }
66     }
67 }