All Downloads are FREE. Search and download functionalities are using the official Maven repository.

link.thingscloud.freeswitch.esl.inbound.NettyInboundClient Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package link.thingscloud.freeswitch.esl.inbound;

import link.thingscloud.freeswitch.esl.InboundClient;
import link.thingscloud.freeswitch.esl.constant.EslConstant;
import link.thingscloud.freeswitch.esl.exception.InboundTimeoutExcetion;
import link.thingscloud.freeswitch.esl.inbound.handler.InboundChannelHandler;
import link.thingscloud.freeswitch.esl.inbound.option.InboundClientOption;
import link.thingscloud.freeswitch.esl.transport.CommandResponse;
import link.thingscloud.freeswitch.esl.transport.SendEvent;
import link.thingscloud.freeswitch.esl.transport.SendMsg;
import link.thingscloud.freeswitch.esl.transport.message.EslMessage;
import link.thingscloud.freeswitch.esl.util.StringUtils;

import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;


/**
 * 

NettyInboundClient class.

* * @author : zhouhailin * @version 1.0.0 */ public class NettyInboundClient extends AbstractInboundClientCommand { /** *

Constructor for NettyInboundClient.

* * @param option a {@link link.thingscloud.freeswitch.esl.inbound.option.InboundClientOption} object. */ public NettyInboundClient(InboundClientOption option) { super(option); } /** * {@inheritDoc} */ @Override public EslMessage sendSyncApiCommand(String addr, String command, String arg) { InboundChannelHandler handler = getAuthedHandler(addr); StringBuilder sb = new StringBuilder(); if (command != null && !command.isEmpty()) { sb.append("api "); sb.append(command); } if (arg != null && !arg.isEmpty()) { sb.append(' '); sb.append(arg); } log.debug("sendSyncApiCommand addr : {}, command : {}, arg : {}", addr, command, arg); return handler.sendSyncSingleLineCommand(sb.toString()); } /** * {@inheritDoc} */ @Override public EslMessage sendSyncApiCommand(String addr, String command, String arg, long timeoutSeconds) throws InboundTimeoutExcetion { try { return publicExecutor.submit(() -> sendSyncApiCommand(addr, command, arg)).get(timeoutSeconds, TimeUnit.SECONDS); } catch (Exception e) { throw new InboundTimeoutExcetion(String.format("sendSyncApiCommand addr : %s, command : %s, arg : %s, timeoutSeconds : %s", addr, command, arg, timeoutSeconds), e); } } /** * {@inheritDoc} */ @Override public void sendSyncApiCommand(String addr, String command, String arg, Consumer consumer) { publicExecutor.execute(() -> { EslMessage msg = sendSyncApiCommand(addr, command, arg); if (consumer != null) { consumer.accept(msg); } }); } /** * {@inheritDoc} */ @Override public String sendAsyncApiCommand(String addr, String command, String arg) { InboundChannelHandler handler = getAuthedHandler(addr); StringBuilder sb = new StringBuilder(); if (command != null && !command.isEmpty()) { sb.append("bgapi "); sb.append(command); } if (arg != null && !arg.isEmpty()) { sb.append(' '); sb.append(arg); } return handler.sendAsyncCommand(sb.toString()); } /** * {@inheritDoc} */ @Override public void sendAsyncApiCommand(String addr, String command, String arg, Consumer consumer) { publicExecutor.execute(() -> { String msg = sendAsyncApiCommand(addr, command, arg); if (consumer != null) { consumer.accept(msg); } }); } /** * {@inheritDoc} */ @Override public CommandResponse setEventSubscriptions(String addr, String format, String events) { if (!StringUtils.equals(format, EslConstant.PLAIN)) { throw new IllegalStateException("Only 'plain' event format is supported at present"); } InboundChannelHandler handler = getAuthedHandler(addr); StringBuilder sb = new StringBuilder(); sb.append("event "); sb.append(format); if (events != null && !events.isEmpty()) { sb.append(' '); sb.append(events); } EslMessage response = handler.sendSyncSingleLineCommand(sb.toString()); return new CommandResponse(sb.toString(), response); } /** * {@inheritDoc} */ @Override public CommandResponse cancelEventSubscriptions(String addr) { InboundChannelHandler handler = getAuthedHandler(addr); EslMessage response = handler.sendSyncSingleLineCommand("noevents"); return new CommandResponse("noevents", response); } /** * {@inheritDoc} */ @Override public CommandResponse addEventFilter(String addr, String eventHeader, String valueToFilter) { InboundChannelHandler handler = getAuthedHandler(addr); StringBuilder sb = new StringBuilder(); if (eventHeader != null && !eventHeader.isEmpty()) { sb.append("filter "); sb.append(eventHeader); } if (valueToFilter != null && !valueToFilter.isEmpty()) { sb.append(' '); sb.append(valueToFilter); } EslMessage response = handler.sendSyncSingleLineCommand(sb.toString()); return new CommandResponse(sb.toString(), response); } /** * {@inheritDoc} */ @Override public CommandResponse deleteEventFilter(String addr, String eventHeader, String valueToFilter) { InboundChannelHandler handler = getAuthedHandler(addr); StringBuilder sb = new StringBuilder(); if (eventHeader != null && !eventHeader.isEmpty()) { sb.append("filter delete "); sb.append(eventHeader); } if (valueToFilter != null && !valueToFilter.isEmpty()) { sb.append(' '); sb.append(valueToFilter); } EslMessage response = handler.sendSyncSingleLineCommand(sb.toString()); return new CommandResponse(sb.toString(), response); } /** * {@inheritDoc} */ @Override public CommandResponse sendEvent(String addr, SendEvent sendEvent) { InboundChannelHandler handler = getAuthedHandler(addr); EslMessage response = handler.sendSyncMultiLineCommand(sendEvent.getMsgLines()); return new CommandResponse(sendEvent.toString(), response); } /** * {@inheritDoc} */ @Override public void sendEvent(String addr, SendEvent sendEvent, Consumer consumer) { publicExecutor.execute(() -> { CommandResponse response = sendEvent(addr, sendEvent); if (consumer != null) { consumer.accept(response); } }); } /** * {@inheritDoc} */ @Override public CommandResponse sendMessage(String addr, SendMsg sendMsg) { InboundChannelHandler handler = getAuthedHandler(addr); EslMessage response = handler.sendSyncMultiLineCommand(sendMsg.getMsgLines()); return new CommandResponse(sendMsg.toString(), response); } /** * {@inheritDoc} */ @Override public void sendMessage(String addr, SendMsg sendMsg, Consumer consumer) { publicExecutor.execute(() -> { CommandResponse response = sendMessage(addr, sendMsg); if (consumer != null) { consumer.accept(response); } }); } /** * {@inheritDoc} */ @Override public CommandResponse setLoggingLevel(String addr, String level) { InboundChannelHandler handler = getAuthedHandler(addr); StringBuilder sb = new StringBuilder(); if (level != null && !level.isEmpty()) { sb.append("log "); sb.append(level); } EslMessage response = handler.sendSyncSingleLineCommand(sb.toString()); return new CommandResponse(sb.toString(), response); } /** * {@inheritDoc} */ @Override public CommandResponse cancelLogging(String addr) { InboundChannelHandler handler = getAuthedHandler(addr); EslMessage response = handler.sendSyncSingleLineCommand("nolog"); return new CommandResponse("nolog", response); } /** * {@inheritDoc} */ @Override public CommandResponse close(String addr) { InboundChannelHandler handler = getAuthedHandler(addr); EslMessage response = handler.sendSyncSingleLineCommand("exit"); return new CommandResponse("exit", response); } /** * {@inheritDoc} */ @Override public InboundClient closeChannel(String addr) { getAuthedHandler(addr).close(); return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy