GenericMgcpEndpoint implements MgcpEndpoint { @Override public synchronized void requestNotification(NotificationRequest request) { if (request.getNotifiedEntity() != null) { this.notifiedEntity = request.getNotifiedEntity(); } this.requestedEndpointEvents = EMPTY_ENDPOINT_EVENTS; this.requestedConnectionEvents.clear(); int eventCount = request.getRequestedEvents().length; List<MgcpRequestedEvent> endpointEvents = new ArrayList<>(eventCount); for (MgcpRequestedEvent requestedEvent : request.getRequestedEvents()) { if(requestedEvent.getConnectionId() > 0) { int connectionId = requestedEvent.getConnectionId(); MgcpConnection connection = this.connections.get(connectionId); if(connection == null) { log.warn("Requested event " + requestedEvent.toString() + " was dropped because connection " + Integer.toHexString(connectionId) + "was not found."); } else { try { connection.listen(requestedEvent); this.requestedConnectionEvents.put(connectionId, requestedEvent); if (log.isDebugEnabled()) { log.debug("Endpoint " + this.endpointId + " requested event " + requestedEvent.getQualifiedName() + " to connection " + requestedEvent.getConnectionId()); } } catch (UnsupportedMgcpEventException e) { log.warn("Requested event " + requestedEvent.toString() + " was dropped because it was not supported by connection " + connection.getHexIdentifier(), e); } } } else { endpointEvents.add(requestedEvent); if(log.isDebugEnabled()) { log.debug("Endpoint " + this.endpointId + " is listening for event " + requestedEvent.getQualifiedName()); } } } if(endpointEvents.size() > 0) { this.requestedEndpointEvents = endpointEvents.toArray(new MgcpRequestedEvent[endpointEvents.size()]); } if (request.countSignals() == 0) { cancelSignals(); } else { List<String> retained = new ArrayList<>(request.countSignals()); for (MgcpSignal signal = request.pollSignal(); signal != null; signal = request.pollSignal()) { final SignalType signalType = signal.getSignalType(); switch (signalType) { case TIME_OUT: String signalName = signal.getName(); retained.add(signalName); MgcpSignal original = this.signals.putIfAbsent(signalName, signal); if(original != null && !original.equals(signal)) { original.cancel(); } if(original == null || !original.equals(signal)) { signal.observe(this); signal.execute(); } break; case BRIEF: signal.execute(); break; default: log.warn("Dropping signal " + signal.toString() + " on endpoint " + getEndpointId().toString() + " because signal type " + signalType + "is not supported."); break; } } Iterator<String> keys = this.signals.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); if(!retained.contains(key)) { cancelSignal(key); } } } } GenericMgcpEndpoint(EndpointIdentifier endpointId, MgcpConnectionProvider connectionProvider, MediaGroup mediaGroup); @Override EndpointIdentifier getEndpointId(); @Override MediaGroup getMediaGroup(); boolean hasConnections(); @Override MgcpConnection getConnection(int callId, int connectionId); @Override MgcpConnection createConnection(int callId, boolean local); @Override MgcpConnection deleteConnection(int callId, int connectionId); @Override List<MgcpConnection> deleteConnections(int callId); @Override List<MgcpConnection> deleteConnections(); boolean isActive(); @Override synchronized void requestNotification(NotificationRequest request); @Override void cancelSignal(String signal); @Override void observe(MgcpMessageObserver observer); @Override void forget(MgcpMessageObserver observer); @Override void notify(Object originator, InetSocketAddress from, InetSocketAddress to, MgcpMessage message, MessageDirection direction); @Override void onEvent(Object originator, MgcpEvent event); @Override void observe(MgcpEndpointObserver observer); @Override void forget(MgcpEndpointObserver observer); @Override void notify(MgcpEndpoint endpoint, MgcpEndpointState state);  }
@Test public void testExecuteTimeoutSignal() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final MgcpSignal signal = mock(MgcpSignal.class); final NotificationRequest rqnt = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); when(signal.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal.getName()).thenReturn("AU/pa"); genericMgcpEndpoint.requestNotification(rqnt); verify(signal, times(1)).execute(); }
@Test public void testExecuteMultipleTimeoutSignals() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final MgcpSignal signal1 = mock(MgcpSignal.class); final MgcpSignal signal2 = mock(MgcpSignal.class); final NotificationRequest rqnt = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal1, signal2); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); when(signal1.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal1.getName()).thenReturn("AU/pa"); when(signal2.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal2.getName()).thenReturn("AU/pc"); genericMgcpEndpoint.requestNotification(rqnt); verify(signal1, times(1)).execute(); verify(signal2, times(1)).execute(); }
@Test public void testOverriedExecutingSignals() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final MgcpSignal signal1 = mock(MgcpSignal.class); final MgcpSignal signal2 = mock(MgcpSignal.class); final NotificationRequest rqnt1 = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal1, signal2); final MgcpSignal signal3 = mock(MgcpSignal.class); final NotificationRequest rqnt2 = new NotificationRequest(2, "1b", notifiedEntity, requestedEvents, signal1, signal3); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); when(signal1.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal1.getName()).thenReturn("AU/pa"); when(signal2.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal2.getName()).thenReturn("AU/pc"); when(signal3.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal3.getName()).thenReturn("AU/pr"); genericMgcpEndpoint.requestNotification(rqnt1); genericMgcpEndpoint.requestNotification(rqnt2); verify(signal1, times(1)).execute(); verify(signal1, times(0)).cancel(); verify(signal2, times(1)).execute(); verify(signal2, times(1)).cancel(); verify(signal3, times(1)).execute(); verify(signal3, times(0)).cancel(); }
@Test public void testCancelAllExecutingSignals() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final MgcpSignal signal1 = mock(MgcpSignal.class); final MgcpSignal signal2 = mock(MgcpSignal.class); final NotificationRequest rqnt1 = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal1, signal2); final NotificationRequest rqnt2 = new NotificationRequest(2, "1b", notifiedEntity, requestedEvents); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); when(signal1.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal1.getName()).thenReturn("AU/pa"); when(signal2.getSignalType()).thenReturn(SignalType.TIME_OUT); when(signal2.getName()).thenReturn("AU/pc"); genericMgcpEndpoint.requestNotification(rqnt1); genericMgcpEndpoint.requestNotification(rqnt2); verify(signal1, times(1)).execute(); verify(signal1, times(1)).cancel(); verify(signal2, times(1)).execute(); verify(signal2, times(1)).cancel(); }
@Test public void testExecuteTimeoutSignalDuringSignalExecution() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final MockSignal signal1 = new MockSignal("AU", "pa", SignalType.TIME_OUT, 1); final MockSignal signal2 = new MockSignal("AU", "pc", SignalType.TIME_OUT, 2); final NotificationRequest rqnt1 = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal1); final NotificationRequest rqnt2 = new NotificationRequest(2, "1b", notifiedEntity, requestedEvents, signal2); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); genericMgcpEndpoint.requestNotification(rqnt1); genericMgcpEndpoint.requestNotification(rqnt2); assertTrue(signal1.calledExecute); assertTrue(signal1.calledCancel); assertTrue(signal2.calledExecute); assertFalse(signal2.calledCancel); }
@Test public void testExecuteTimeoutSignalWhileEquivalentSignalIsExecuting() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final MockSignal signal1 = new MockSignal("AU", "pa", SignalType.TIME_OUT, 1); final MockSignal signal2 = new MockSignal("AU", "pa", SignalType.TIME_OUT, 2); final NotificationRequest rqnt1 = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal1); final NotificationRequest rqnt2 = new NotificationRequest(2, "1b", notifiedEntity, requestedEvents, signal2); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); genericMgcpEndpoint.requestNotification(rqnt1); genericMgcpEndpoint.requestNotification(rqnt2); assertTrue(signal1.calledExecute); assertFalse(signal1.calledCancel); assertFalse(signal2.calledExecute); assertFalse(signal2.calledCancel); }
@Test public void testExecuteTimeoutSignalWhileInequivalentSignalIsExecuting() { final NotifiedEntity notifiedEntity = new NotifiedEntity("call-agent", "127.0.0.1", 2727); final MgcpRequestedEvent[] requestedEvents = new MgcpRequestedEvent[] { mock(MgcpRequestedEvent.class), mock(MgcpRequestedEvent.class) }; final Map<String, String> signal1Parameters = new HashMap<>(1); final Map<String, String> signal2Parameters = new HashMap<>(1); final MockSignal signal1 = new MockSignal("AU", "pa", SignalType.TIME_OUT, 1, signal1Parameters); final MockSignal signal2 = new MockSignal("AU", "pa", SignalType.TIME_OUT, 2, signal2Parameters); final NotificationRequest rqnt1 = new NotificationRequest(1, "1a", notifiedEntity, requestedEvents, signal1); final NotificationRequest rqnt2 = new NotificationRequest(2, "1b", notifiedEntity, requestedEvents, signal2); final MgcpConnectionProvider connectionProvider = mock(MgcpConnectionProvider.class); final MediaGroup mediaGroup = mock(MediaGroup.class); final EndpointIdentifier endpointId = new EndpointIdentifier("mobicents/endpoint/1", "127.0.0.1"); final MgcpEndpoint genericMgcpEndpoint = new GenericMgcpEndpoint(endpointId, connectionProvider, mediaGroup); signal1Parameters.put("prompt", "promtpX"); signal2Parameters.put("prompt", "promtpY"); genericMgcpEndpoint.requestNotification(rqnt1); genericMgcpEndpoint.requestNotification(rqnt2); assertTrue(signal1.calledExecute); assertTrue(signal1.calledCancel); assertTrue(signal2.calledExecute); assertFalse(signal2.calledCancel); }