#!/usr/bin/env python
#
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from string import Template
import dto_gen
import util
jvpp_facade_callback_template = Template("""
package $base_package.$future_package;
/**
*
Async facade callback setting values to future objects
*
It was generated by jvpp_future_facade_gen.py based on $inputfile
*
(python representation of vpe.api generated by vppapigen).
*/
public final class FutureJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback {
private final java.util.Map>> requests;
private final $base_package.$notification_package.GlobalNotificationCallback notificationCallback;
public FutureJVppFacadeCallback(final java.util.Map>> requestMap,
final $base_package.$notification_package.GlobalNotificationCallback notificationCallback) {
this.requests = requestMap;
this.notificationCallback = notificationCallback;
}
@Override
@SuppressWarnings("unchecked")
public void onError(org.openvpp.jvpp.VppCallbackException reply) {
final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>> completableFuture;
synchronized(requests) {
completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>>) requests.get(reply.getCtxId());
}
if(completableFuture != null) {
completableFuture.completeExceptionally(reply);
synchronized(requests) {
requests.remove(reply.getCtxId());
}
}
}
$methods
}
""")
jvpp_facade_callback_method_template = Template("""
@Override
@SuppressWarnings("unchecked")
public void on$callback_dto($base_package.$dto_package.$callback_dto reply) {
final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>> completableFuture;
synchronized(requests) {
completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>>) requests.get(reply.context);
}
if(completableFuture != null) {
completableFuture.complete(reply);
synchronized(requests) {
requests.remove(reply.context);
}
}
}
""")
jvpp_facade_callback_notification_method_template = Template("""
@Override
public void on$callback_dto($base_package.$dto_package.$callback_dto notification) {
notificationCallback.on$callback_dto(notification);
}
""")
# TODO reuse common parts with generic method callback
jvpp_facade_control_ping_method_template = Template("""
@Override
@SuppressWarnings("unchecked")
public void on$callback_dto($base_package.$dto_package.$callback_dto reply) {
final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>> completableFuture;
synchronized(requests) {
completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>>) requests.get(reply.context);
}
if(completableFuture != null) {
// Finish dump call
if (completableFuture instanceof $base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) {
completableFuture.complete((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getReplyDump());
// Remove future mapped to dump call context id
synchronized(requests) {
requests.remove((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getContextId());
}
} else {
completableFuture.compimport zlib
import struct
class ZippedMsg:
MSG_COMPRESS_THRESHOLD = 256
MSG_COMPRESS_HEADER_MAGIC = 0xABE85CEA
def check_threshold (self, msg):
return len(msg) >= self.MSG_COMPRESS_THRESHOLD
def compress (self, msg):
# compress
compressed = zlib.compress(msg)
new_msg = struct.pack(">II", self.MSG_COMPRESS_HEADER_MAGIC, len(msg)) + compressed
return new_msg
def decompress (self, msg):
if len(msg) < 8:
return None
t = struct.unpack(">II", msg[:8])
if (t[0] != self.MSG_COMPRESS_HEADER_MAGIC):
return None
x = zlib.decompress(msg[8:])
if len(x) != t[1]:
return None
return x