@Override public void setUp() { AsteriskServerImpl server = new AsteriskServerImpl(); channel = new AsteriskChannelImpl(server, "SIP/1234", "0123456789.123", DateUtil.getDate()); channel.stateChanged(DateUtil.getDate(), ChannelState.DOWN); numberOfChanges = 0; }
void handleStatusEvent(StatusEvent event) { AsteriskChannelImpl channel; final Extension extension; boolean isNew = false; Map<String, String> variables = event.getVariables(); channel = getChannelImplById(event.getUniqueId()); if (channel == null) { Date dateOfCreation; if (event.getSeconds() != null) { dateOfCreation = new Date(DateUtil.getDate().getTime() - (event.getSeconds() * 1000L)); } else { dateOfCreation = DateUtil.getDate(); } channel = new AsteriskChannelImpl(server, event.getChannel(), event.getUniqueId(), dateOfCreation); isNew = true; if (variables != null) { for (String variable : variables.keySet()) { channel.updateVariable(variable, variables.get(variable)); } } } if (event.getContext() == null && event.getExtension() == null && event.getPriority() == null) { extension = null; } else { extension = new Extension(event.getContext(), event.getExtension(), event.getPriority()); } synchronized (channel) { channel.setCallerId(new CallerId(event.getCallerIdName(), event.getCallerIdNum())); channel.setAccount(event.getAccountCode()); if (event.getChannelState() != null) { channel.stateChanged( event.getDateReceived(), ChannelState.valueOf(event.getChannelState())); } channel.extensionVisited(event.getDateReceived(), extension); if (event.getBridgedChannel() != null) { final AsteriskChannelImpl linkedChannel = getChannelImplByName(event.getBridgedChannel()); if (linkedChannel != null) { // the date used here is not correct! channel.channelLinked(event.getDateReceived(), linkedChannel); synchronized (linkedChannel) { linkedChannel.channelLinked(event.getDateReceived(), channel); } } } } if (isNew) { logger.info("Adding new channel " + channel.getName()); addChannel(channel); server.fireNewAsteriskChannel(channel); } }
public void testStateChange() { channel.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { assertEquals("wrong propertyName", "state", evt.getPropertyName()); assertEquals("wrong oldValue", ChannelState.DOWN, evt.getOldValue()); assertEquals("wrong newValue", ChannelState.DIALING, evt.getNewValue()); assertEquals("wrong source", channel, evt.getSource()); numberOfChanges++; } }); channel.stateChanged(DateUtil.getDate(), ChannelState.DIALING); assertEquals("wrong number of propagated changes", 1, numberOfChanges); }
/** Removes channels that have been hung more than {@link #REMOVAL_THRESHOLD} milliseconds. */ private void removeOldChannels() { Iterator<AsteriskChannelImpl> i; synchronized (channels) { i = channels.iterator(); while (i.hasNext()) { final AsteriskChannel channel = i.next(); final Date dateOfRemoval = channel.getDateOfRemoval(); if (channel.getState() == ChannelState.HUNGUP && dateOfRemoval != null) { final long diff = DateUtil.getDate().getTime() - dateOfRemoval.getTime(); if (diff >= REMOVAL_THRESHOLD) { i.remove(); } } } } }