Search in sources :

Example 6 with ChannelUri

use of io.aeron.ChannelUri in project Aeron by real-logic.

the class UdpChannel method parse.

/**
 * Parse channel URI and create a {@link UdpChannel}.
 *
 * @param channelUriString to parse.
 * @param nameResolver     to use for resolving names.
 * @param isDestination    to identify if it is a destination within a channel.
 * @return a new {@link UdpChannel} as the result of parsing.
 * @throws InvalidChannelException if an error occurs.
 */
@SuppressWarnings("MethodLength")
public static UdpChannel parse(final String channelUriString, final NameResolver nameResolver, final boolean isDestination) {
    try {
        final ChannelUri channelUri = ChannelUri.parse(channelUriString);
        validateConfiguration(channelUri);
        InetSocketAddress endpointAddress = getEndpointAddress(channelUri, nameResolver);
        final InetSocketAddress controlAddress = getExplicitControlAddress(channelUri, nameResolver);
        final String tagIdStr = channelUri.channelTag();
        final String controlMode = channelUri.get(CommonContext.MDC_CONTROL_MODE_PARAM_NAME);
        final boolean isManualControlMode = CommonContext.MDC_CONTROL_MODE_MANUAL.equals(controlMode);
        final boolean isDynamicControlMode = CommonContext.MDC_CONTROL_MODE_DYNAMIC.equals(controlMode);
        final int socketRcvbufLength = parseBufferLength(channelUri, CommonContext.SOCKET_RCVBUF_PARAM_NAME);
        final int socketSndbufLength = parseBufferLength(channelUri, CommonContext.SOCKET_SNDBUF_PARAM_NAME);
        final int receiverWindowLength = parseBufferLength(channelUri, CommonContext.RECEIVER_WINDOW_LENGTH_PARAM_NAME);
        final boolean requiresAdditionalSuffix = !isDestination && (null == endpointAddress && null == controlAddress || (null != endpointAddress && endpointAddress.getPort() == 0) || (null != controlAddress && controlAddress.getPort() == 0));
        final boolean hasNoDistinguishingCharacteristic = null == endpointAddress && null == controlAddress && null == tagIdStr;
        if (isDynamicControlMode && null == controlAddress) {
            throw new IllegalArgumentException("explicit control expected with dynamic control mode: " + channelUriString);
        }
        if (hasNoDistinguishingCharacteristic && !isManualControlMode) {
            throw new IllegalArgumentException("URIs for UDP must specify an endpoint, control, tags, or control-mode=manual: " + channelUriString);
        }
        if (null != endpointAddress && endpointAddress.isUnresolved()) {
            throw new UnknownHostException("could not resolve endpoint address: " + endpointAddress);
        }
        if (null != controlAddress && controlAddress.isUnresolved()) {
            throw new UnknownHostException("could not resolve control address: " + controlAddress);
        }
        boolean hasExplicitEndpoint = true;
        if (null == endpointAddress) {
            hasExplicitEndpoint = false;
            endpointAddress = null != controlAddress && controlAddress.getAddress() instanceof Inet6Address ? ANY_IPV6 : ANY_IPV4;
        }
        final Context context = new Context().uriStr(channelUriString).channelUri(channelUri).isManualControlMode(isManualControlMode).isDynamicControlMode(isDynamicControlMode).hasExplicitEndpoint(hasExplicitEndpoint).hasNoDistinguishingCharacteristic(hasNoDistinguishingCharacteristic).socketRcvbufLength(socketRcvbufLength).socketSndbufLength(socketSndbufLength).receiverWindowLength(receiverWindowLength);
        if (null != tagIdStr) {
            context.hasTagId(true).tagId(Long.parseLong(tagIdStr));
        }
        if (endpointAddress.getAddress().isMulticastAddress()) {
            final InterfaceSearchAddress searchAddress = getInterfaceSearchAddress(channelUri);
            final NetworkInterface localInterface = findInterface(searchAddress);
            final InetSocketAddress resolvedAddress = resolveToAddressOfInterface(localInterface, searchAddress);
            context.isMulticast(true).localControlAddress(resolvedAddress).remoteControlAddress(getMulticastControlAddress(endpointAddress)).localDataAddress(resolvedAddress).remoteDataAddress(endpointAddress).localInterface(localInterface).protocolFamily(getProtocolFamily(endpointAddress.getAddress())).canonicalForm(canonicalise(null, resolvedAddress, null, endpointAddress));
            final String ttlValue = channelUri.get(CommonContext.TTL_PARAM_NAME);
            if (null != ttlValue) {
                context.hasMulticastTtl(true).multicastTtl(Integer.parseInt(ttlValue));
            }
        } else if (null != controlAddress) {
            final String controlVal = channelUri.get(CommonContext.MDC_CONTROL_PARAM_NAME);
            final String endpointVal = channelUri.get(CommonContext.ENDPOINT_PARAM_NAME);
            String suffix = "";
            if (requiresAdditionalSuffix) {
                suffix = null != tagIdStr ? "#" + tagIdStr : "-" + UNIQUE_CANONICAL_FORM_VALUE.getAndAdd(1);
            }
            final String canonicalForm = canonicalise(controlVal, controlAddress, endpointVal, endpointAddress) + suffix;
            context.hasExplicitControl(true).remoteControlAddress(endpointAddress).remoteDataAddress(endpointAddress).localControlAddress(controlAddress).localDataAddress(controlAddress).protocolFamily(getProtocolFamily(endpointAddress.getAddress())).canonicalForm(canonicalForm);
        } else {
            final InterfaceSearchAddress searchAddress = getInterfaceSearchAddress(channelUri);
            final InetSocketAddress localAddress = searchAddress.getInetAddress().isAnyLocalAddress() ? searchAddress.getAddress() : resolveToAddressOfInterface(findInterface(searchAddress), searchAddress);
            final String endpointVal = channelUri.get(CommonContext.ENDPOINT_PARAM_NAME);
            String suffix = "";
            if (requiresAdditionalSuffix) {
                suffix = (null != tagIdStr) ? "#" + tagIdStr : ("-" + UNIQUE_CANONICAL_FORM_VALUE.getAndAdd(1));
            }
            context.remoteControlAddress(endpointAddress).remoteDataAddress(endpointAddress).localControlAddress(localAddress).localDataAddress(localAddress).protocolFamily(getProtocolFamily(endpointAddress.getAddress())).canonicalForm(canonicalise(null, localAddress, endpointVal, endpointAddress) + suffix);
        }
        context.channelReceiveTimestampOffset(parseTimestampOffset(channelUri, CHANNEL_RECEIVE_TIMESTAMP_OFFSET_PARAM_NAME));
        context.channelSendTimestampOffset(parseTimestampOffset(channelUri, CHANNEL_SEND_TIMESTAMP_OFFSET_PARAM_NAME));
        return new UdpChannel(context);
    } catch (final Exception ex) {
        throw new InvalidChannelException(ex);
    }
}
Also used : CommonContext(io.aeron.CommonContext) InvalidChannelException(io.aeron.driver.exceptions.InvalidChannelException) InvalidChannelException(io.aeron.driver.exceptions.InvalidChannelException) ChannelUri(io.aeron.ChannelUri)

Example 7 with ChannelUri

use of io.aeron.ChannelUri in project aeron by real-logic.

the class DriverConductor method onAddNetworkPublication.

void onAddNetworkPublication(final String channel, final int streamId, final long correlationId, final long clientId, final boolean isExclusive) {
    final UdpChannel udpChannel = UdpChannel.parse(channel, nameResolver);
    final ChannelUri channelUri = udpChannel.channelUri();
    final PublicationParams params = getPublicationParams(channelUri, ctx, this, false);
    validateEndpointForPublication(udpChannel);
    validateMtuForMaxMessage(params, channel);
    final SendChannelEndpoint channelEndpoint = getOrCreateSendChannelEndpoint(params, udpChannel, correlationId);
    NetworkPublication publication = null;
    if (!isExclusive) {
        publication = findPublication(networkPublications, streamId, channelEndpoint);
    }
    boolean isNewPublication = false;
    if (null == publication) {
        if (params.hasSessionId) {
            checkForSessionClash(params.sessionId, streamId, udpChannel.canonicalForm(), channel);
        }
        publication = newNetworkPublication(correlationId, clientId, streamId, channel, udpChannel, channelEndpoint, params, isExclusive);
        isNewPublication = true;
    } else {
        confirmMatch(channelUri, params, publication.rawLog(), publication.sessionId(), publication.channel(), publication.initialTermId(), publication.startingTermId(), publication.startingTermOffset());
        validateSpiesSimulateConnection(params, publication.spiesSimulateConnection(), channel, publication.channel());
    }
    publicationLinks.add(new PublicationLink(correlationId, getOrAddClient(clientId), publication));
    clientProxy.onPublicationReady(correlationId, publication.registrationId(), streamId, publication.sessionId(), publication.rawLog().fileName(), publication.publisherLimitId(), channelEndpoint.statusIndicatorCounterId(), isExclusive);
    if (isNewPublication) {
        linkSpies(subscriptionLinks, publication);
    }
}
Also used : ChannelUri(io.aeron.ChannelUri) PublicationParams(io.aeron.driver.PublicationParams) SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint) UdpChannel(io.aeron.driver.media.UdpChannel)

Example 8 with ChannelUri

use of io.aeron.ChannelUri in project aeron by real-logic.

the class DriverConductor method onAddIpcPublication.

void onAddIpcPublication(final String channel, final int streamId, final long correlationId, final long clientId, final boolean isExclusive) {
    IpcPublication publication = null;
    final ChannelUri channelUri = ChannelUri.parse(channel);
    final PublicationParams params = getPublicationParams(channelUri, ctx, this, true);
    if (!isExclusive) {
        publication = findSharedIpcPublication(ipcPublications, streamId);
    }
    boolean isNewPublication = false;
    if (null == publication) {
        if (params.hasSessionId) {
            checkForSessionClash(params.sessionId, streamId, IPC_MEDIA, channel);
        }
        validateMtuForMaxMessage(params, channel);
        publication = addIpcPublication(correlationId, clientId, streamId, channel, isExclusive, params);
        isNewPublication = true;
    } else {
        confirmMatch(channelUri, params, publication.rawLog(), publication.sessionId(), publication.channel(), publication.initialTermId(), publication.startingTermId(), publication.startingTermOffset());
    }
    publicationLinks.add(new PublicationLink(correlationId, getOrAddClient(clientId), publication));
    clientProxy.onPublicationReady(correlationId, publication.registrationId(), streamId, publication.sessionId(), publication.rawLog().fileName(), publication.publisherLimitId(), ChannelEndpointStatus.NO_ID_ALLOCATED, isExclusive);
    if (isNewPublication) {
        linkIpcSubscriptions(publication);
    }
}
Also used : ChannelUri(io.aeron.ChannelUri) PublicationParams(io.aeron.driver.PublicationParams)

Example 9 with ChannelUri

use of io.aeron.ChannelUri in project aeron by real-logic.

the class MultiNodeTest method shouldBecomeFollowerStaticThreeNodeConfigWithElection.

@Test(timeout = 10_000L)
public void shouldBecomeFollowerStaticThreeNodeConfigWithElection() {
    final ClusteredService mockService = mock(ClusteredService.class);
    final ConsensusModule.Context context = new ConsensusModule.Context().clusterMembers(THREE_NODE_MEMBERS).memberStatusChannel("aeron:udp?endpoint=localhost:9020").appointedLeaderId(1);
    try (ConsensusModuleHarness harness = new ConsensusModuleHarness(context, mockService, mockMemberStatusListeners, true, true)) {
        harness.memberStatusPublisher().requestVote(harness.memberStatusPublication(1), 0, 0, 0, 1);
        harness.awaitMemberStatusMessage(1);
        verify(mockMemberStatusListeners[1]).onVote(0, 0, 0, 1, 0, true);
        final int logSessionId = 123456;
        final ChannelUri channelUri = ChannelUri.parse(context.logChannel());
        channelUri.put(CommonContext.ENDPOINT_PARAM_NAME, harness.member(0).logEndpoint());
        channelUri.put(CommonContext.SESSION_ID_PARAM_NAME, Integer.toString(logSessionId));
        final Publication logPublication = harness.aeron().addExclusivePublication(channelUri.toString(), context.logStreamId());
        harness.memberStatusPublisher().commitPosition(harness.memberStatusPublication(1), 0, 0, 1, logSessionId);
        harness.awaitMemberStatusMessage(1);
        verify(mockMemberStatusListeners[1]).onAppendedPosition(0, 0, 0);
        harness.awaitServiceOnStart();
    }
}
Also used : ChannelUri(io.aeron.ChannelUri) Publication(io.aeron.Publication) ClusteredService(io.aeron.cluster.service.ClusteredService) Test(org.junit.Test)

Example 10 with ChannelUri

use of io.aeron.ChannelUri in project aeron by real-logic.

the class DriverConductor method onAddDestination.

void onAddDestination(final long registrationId, final String destinationChannel, final long correlationId) {
    SendChannelEndpoint sendChannelEndpoint = null;
    for (int i = 0, size = networkPublications.size(); i < size; i++) {
        final NetworkPublication publication = networkPublications.get(i);
        if (registrationId == publication.registrationId()) {
            sendChannelEndpoint = publication.channelEndpoint();
            break;
        }
    }
    if (null == sendChannelEndpoint) {
        throw new ControlProtocolException(UNKNOWN_PUBLICATION, "Unknown publication: " + registrationId);
    }
    sendChannelEndpoint.validateAllowsManualControl();
    final ChannelUri channelUri = ChannelUri.parse(destinationChannel);
    final InetSocketAddress dstAddress = UdpChannel.destinationAddress(channelUri);
    senderProxy.addDestination(sendChannelEndpoint, dstAddress);
    clientProxy.operationSucceeded(correlationId);
}
Also used : ChannelUri(io.aeron.ChannelUri) SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint) InetSocketAddress(java.net.InetSocketAddress) ControlProtocolException(io.aeron.driver.exceptions.ControlProtocolException) ReceiveChannelEndpoint(io.aeron.driver.media.ReceiveChannelEndpoint) SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint)

Aggregations

ChannelUri (io.aeron.ChannelUri)16 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)8 ReceiveChannelEndpoint (io.aeron.driver.media.ReceiveChannelEndpoint)6 InetSocketAddress (java.net.InetSocketAddress)6 PublicationParams (io.aeron.driver.PublicationParams)5 ControlProtocolException (io.aeron.exceptions.ControlProtocolException)4 CommonContext (io.aeron.CommonContext)3 InvalidChannelException (io.aeron.driver.exceptions.InvalidChannelException)3 Publication (io.aeron.Publication)2 ClusteredService (io.aeron.cluster.service.ClusteredService)2 ControlProtocolException (io.aeron.driver.exceptions.ControlProtocolException)2 UdpChannel (io.aeron.driver.media.UdpChannel)2 Test (org.junit.Test)2