@Override
    synchronized public void onManagerEvent(final ManagerEvent event)
    {

        if (event instanceof HangupEvent)
        {
            final HangupEvent hangupEvt = (HangupEvent) event;
            final Channel hangupChannel = hangupEvt.getChannel();

            if (channelId.equals(hangupEvt.getUniqueId()))
            {
                this.originateSuccess = false;
                OriginateBaseClass.logger.warn("Dest channel " + this.newChannel + " hungup");
                originateLatch.countDown();
            }
            if ((this.monitorChannel1 != null) && (hangupChannel.isSame(this.monitorChannel1)))
            {
                this.originateSuccess = false;
                this.hungup = true;
                if (this.newChannel != null)
                {
                    OriginateBaseClass.logger.debug("hanging up " + this.newChannel);
                    this.result.setChannelHungup(true);

                    PBX pbx = PBXFactory.getActivePBX();
                    try
                    {
                        logger.warn("Hanging up");
                        pbx.hangup(this.newChannel);
                    }
                    catch (IllegalArgumentException | IllegalStateException | PBXException e)
                    {
                        logger.error(e, e);

                    }
                }
                OriginateBaseClass.logger.debug("notify channel 1 hungup");
                originateLatch.countDown();
            }
            if ((this.monitorChannel2 != null) && (hangupChannel.isSame(this.monitorChannel2)))
            {
                this.originateSuccess = false;
                this.hungup = true;
                if (this.newChannel != null)
                {
                    OriginateBaseClass.logger.debug("Hanging up channel " + this.newChannel);
                    this.result.setChannelHungup(true);

                    PBX pbx = PBXFactory.getActivePBX();
                    try
                    {
                        pbx.hangup(this.newChannel);
                    }
                    catch (IllegalArgumentException | IllegalStateException | PBXException e)
                    {
                        logger.error(e, e);

                    }
                }
                OriginateBaseClass.logger.debug("Notify channel 2 (" + this.monitorChannel2 + ") hungup");//$NON-NLS-2$
                originateLatch.countDown();
            }

        }
        if (event instanceof OriginateResponseEvent)
        {
            OriginateBaseClass.logger.debug("response : " + this.newChannel);

            final OriginateResponseEvent response = (OriginateResponseEvent) event;
            OriginateBaseClass.logger.debug("OriginateResponseEvent: channel="
                    + (response.isChannel() ? response.getChannel() : response.getEndPoint()) + " originateID:"
                    + this.originateID);
            OriginateBaseClass.logger.debug("{" + response.getReason() + ":" + response.getResponse() + "}"); //$NON-NLS-2$ //$NON-NLS-3$
            if (this.originateID != null)
            {
                if (this.originateID.compareToIgnoreCase(response.getActionId()) == 0)
                {
                    this.originateSuccess = response.isSuccess();
                    OriginateBaseClass.logger.debug("OriginateResponse: matched actionId, success=" + this.originateSuccess
                            + " channelSeen=" + this.channelSeen);

                    this.originateSeen = true;

                    if (originateSuccess)
                    {
                        if (newChannel == null)
                        {
                            this.newChannel = response.getChannel();
                        }
                        channelSeen = newChannel != null;

                        // if we have also seen the channel then we can notify
                        // the
                        // originate() method
                        // that the call is up. Otherwise we will rely on the
                        // NewChannelEvent doing the
                        // notify.
                        if (this.channelSeen == true)
                        {
                            OriginateBaseClass.logger.info("notify originate response event " + this.originateSuccess);
                            originateLatch.countDown();
                        }
                        else
                        {
                            logger.error("Originate Response didn't contain the channel");
                        }
                    }
                }
            }
            else
            {
                OriginateBaseClass.logger.warn("actionid is null");
            }
        }

        if (event instanceof NewChannelEvent)
        {
            final NewChannelEvent newState = (NewChannelEvent) event;
            if (channelId.equalsIgnoreCase(newState.getChannel().getUniqueId()))
            {
                final Channel channel = newState.getChannel();

                OriginateBaseClass.logger.debug("new channel event :" + channel + " context = " + newState.getContext() //$NON-NLS-2$
                        + " state =" + newState.getChannelStateDesc() + " state =" + newState.getChannelState()); //$NON-NLS-2$

                handleId(channel);
            }
        }

        if (event instanceof BridgeEvent)
        {
            if (((BridgeEvent) event).isLink())
            {
                final BridgeEvent bridgeEvent = (BridgeEvent) event;
                Channel channel = bridgeEvent.getChannel1();
                if (bridgeEvent.getChannel1().isLocal())
                {
                    channel = bridgeEvent.getChannel2();
                }
                if (channelId.equalsIgnoreCase(bridgeEvent.getChannel1().getUniqueId())
                        || channelId.equalsIgnoreCase(bridgeEvent.getChannel2().getUniqueId()))
                {

                    OriginateBaseClass.logger
                            .debug("new bridge event :" + channel + " channel1 = " + bridgeEvent.getChannel1() //$NON-NLS-2$
                                    + " channel2 =" + bridgeEvent.getChannel2());

                    handleId(channel);
                }
            }

        }

        if (event instanceof DialEndEvent)
        {
            if (channelId.equalsIgnoreCase(((DialEndEvent) event).getDestChannel().getUniqueId()))
            {
                String forward = ((DialEndEvent) event).getForward();
                if (forward != null && forward.length() > 0)
                {
                    originateLatch.countDown();
                }
            }
        }
    }
@ Override synchronized public void onManagerEvent ( final ManagerEvent event ) { if ( event instanceof HangupEvent ) { final HangupEvent hangupEvt = ( HangupEvent ) event ; final Channel hangupChannel = hangupEvt . getChannel ( ) ; if ( channelId . equals ( hangupEvt . getUniqueId ( ) ) ) { this . originateSuccess = false ; OriginateBaseClass . logger . warn ( "Dest channel " + this . newChannel + " hungup" ) ; originateLatch . countDown ( ) ; } if ( ( this . monitorChannel1 != null ) && ( hangupChannel . isSame ( this . monitorChannel1 ) ) ) { this . originateSuccess = false ; this . hungup = true ; if ( this . newChannel != null ) { OriginateBaseClass . logger . debug ( "hanging up " + this . newChannel ) ; this . result . setChannelHungup ( true ) ; PBX pbx = PBXFactory . getActivePBX ( ) ; try { logger . warn ( "Hanging up" ) ; pbx . hangup ( this . newChannel ) ; } catch ( IllegalArgumentException | IllegalStateException | PBXException e ) { logger . error ( e , e ) ; } } OriginateBaseClass . logger . debug ( "notify channel 1 hungup" ) ; originateLatch . countDown ( ) ; } if ( ( this . monitorChannel2 != null ) && ( hangupChannel . isSame ( this . monitorChannel2 ) ) ) { this . originateSuccess = false ; this . hungup = true ; if ( this . newChannel != null ) { OriginateBaseClass . logger . debug ( "Hanging up channel " + this . newChannel ) ; this . result . setChannelHungup ( true ) ; PBX pbx = PBXFactory . getActivePBX ( ) ; try { pbx . hangup ( this . newChannel ) ; } catch ( IllegalArgumentException | IllegalStateException | PBXException e ) { logger . error ( e , e ) ; } } OriginateBaseClass . logger . debug ( "Notify channel 2 (" + this . monitorChannel2 + ") hungup" ) ; originateLatch . countDown ( ) ; } } if ( event instanceof OriginateResponseEvent ) { OriginateBaseClass . logger . debug ( "response : " + this . newChannel ) ; final OriginateResponseEvent response = ( OriginateResponseEvent ) event ; OriginateBaseClass . logger . debug ( "OriginateResponseEvent: channel=" + ( response . isChannel ( ) ? response . getChannel ( ) : response . getEndPoint ( ) ) + " originateID:" + this . originateID ) ; OriginateBaseClass . logger . debug ( "{" + response . getReason ( ) + ":" + response . getResponse ( ) + "}" ) ; if ( this . originateID != null ) { if ( this . originateID . compareToIgnoreCase ( response . getActionId ( ) ) == 0 ) { this . originateSuccess = response . isSuccess ( ) ; OriginateBaseClass . logger . debug ( "OriginateResponse: matched actionId, success=" + this . originateSuccess + " channelSeen=" + this . channelSeen ) ; this . originateSeen = true ; if ( originateSuccess ) { if ( newChannel == null ) { this . newChannel = response . getChannel ( ) ; } channelSeen = newChannel != null ; if ( this . channelSeen == true ) { OriginateBaseClass . logger . info ( "notify originate response event " + this . originateSuccess ) ; originateLatch . countDown ( ) ; } else { logger . error ( "Originate Response didn't contain the channel" ) ; } } } } else { OriginateBaseClass . logger . warn ( "actionid is null" ) ; } } if ( event instanceof NewChannelEvent ) { final NewChannelEvent newState = ( NewChannelEvent ) event ; if ( channelId . equalsIgnoreCase ( newState . getChannel ( ) . getUniqueId ( ) ) ) { final Channel channel = newState . getChannel ( ) ; OriginateBaseClass . logger . debug ( "new channel event :" + channel + " context = " + newState . getContext ( ) + " state =" + newState . getChannelStateDesc ( ) + " state =" + newState . getChannelState ( ) ) ; handleId ( channel ) ; } } if ( event instanceof BridgeEvent ) { if ( ( ( BridgeEvent ) event ) . isLink ( ) ) { final BridgeEvent bridgeEvent = ( BridgeEvent ) event ; Channel channel = bridgeEvent . getChannel1 ( ) ; if ( bridgeEvent . getChannel1 ( ) . isLocal ( ) ) { channel = bridgeEvent . getChannel2 ( ) ; } if ( channelId . equalsIgnoreCase ( bridgeEvent . getChannel1 ( ) . getUniqueId ( ) ) || channelId . equalsIgnoreCase ( bridgeEvent . getChannel2 ( ) . getUniqueId ( ) ) ) { OriginateBaseClass . logger . debug ( "new bridge event :" + channel + " channel1 = " + bridgeEvent . getChannel1 ( ) + " channel2 =" + bridgeEvent . getChannel2 ( ) ) ; handleId ( channel ) ; } } } if ( event instanceof DialEndEvent ) { if ( channelId . equalsIgnoreCase ( ( ( DialEndEvent ) event ) . getDestChannel ( ) . getUniqueId ( ) ) ) { String forward = ( ( DialEndEvent ) event ) . getForward ( ) ; if ( forward != null && forward . length ( ) > 0 ) { originateLatch . countDown ( ) ; } } } }
It is important that this method is synchronised as there is some
interaction between the events and we need to ensure we process one at a
time.