API: Use string type instead of u8.
[vpp.git] / extras / japi / java / jvpp-core / io / fd / vpp / jvpp / core / examples / CallbackJVppFacadeExample.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 io.fd.vpp.jvpp.core.examples;
18
19 import io.fd.vpp.jvpp.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.VppCallbackException;
22 import io.fd.vpp.jvpp.core.JVppCoreImpl;
23 import io.fd.vpp.jvpp.core.callback.GetNodeIndexReplyCallback;
24 import io.fd.vpp.jvpp.core.callback.ShowVersionReplyCallback;
25 import io.fd.vpp.jvpp.core.callfacade.CallbackJVppCoreFacade;
26 import io.fd.vpp.jvpp.core.dto.GetNodeIndex;
27 import io.fd.vpp.jvpp.core.dto.GetNodeIndexReply;
28 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
29 import java.nio.charset.StandardCharsets;
30
31 /**
32  * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request.
33  * This is more convenient than the approach shown in CallbackApiExample.
34  */
35 public class CallbackJVppFacadeExample {
36
37     private static ShowVersionReplyCallback showVersionCallback1 = new ShowVersionReplyCallback() {
38         @Override
39         public void onShowVersionReply(final ShowVersionReply msg) {
40             System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s,"
41                     + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context,
42                 msg.program,
43                 msg.version,
44                 msg.buildDate,
45                 msg.buildDirectory);
46         }
47
48         @Override
49         public void onError(VppCallbackException ex) {
50             System.out.printf("Received onError exception in showVersionCallback1: call=%s, reply=%d, context=%d%n",
51                 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
52         }
53     };
54
55     private static ShowVersionReplyCallback showVersionCallback2 = new ShowVersionReplyCallback() {
56         @Override
57         public void onShowVersionReply(final ShowVersionReply msg) {
58             System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, program=%s,"
59                     + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context,
60                 msg.program,
61                 msg.version,
62                 msg.buildDate,
63                 msg.buildDirectory);
64         }
65
66         @Override
67         public void onError(VppCallbackException ex) {
68             System.out.printf("Received onError exception in showVersionCallback2: call=%s, reply=%d, context=%d%n",
69                 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
70         }
71
72     };
73
74     private static GetNodeIndexReplyCallback getNodeIndexCallback = new GetNodeIndexReplyCallback() {
75         @Override
76         public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
77             System.out.printf("Received GetNodeIndexReply: %s%n", msg);
78         }
79
80         @Override
81         public void onError(VppCallbackException ex) {
82             System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d%n",
83                 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
84         }
85     };
86
87     private static void testCallbackFacade() throws Exception {
88         System.out.println("Testing CallbackJVppFacade");
89
90         try (final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeExample");
91              final CallbackJVppCoreFacade callbackFacade = new CallbackJVppCoreFacade(registry, new JVppCoreImpl())) {
92             System.out.println("Successfully connected to VPP");
93
94             callbackFacade.showVersion(showVersionCallback1);
95             callbackFacade.showVersion(showVersionCallback2);
96
97             GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
98             getNodeIndexRequest.nodeName = "dummyNode".getBytes(StandardCharsets.UTF_8);
99             callbackFacade.getNodeIndex(getNodeIndexRequest, getNodeIndexCallback);
100
101             Thread.sleep(2000);
102             System.out.println("Disconnecting...");
103         }
104         Thread.sleep(1000);
105     }
106
107     public static void main(String[] args) throws Exception {
108         testCallbackFacade();
109     }
110 }