/*
 * Copyright 2014-2020 Real Logic Limited.
 *
 * Licensed 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
 *
 * https://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 io.aeron.driver;

import io.aeron.CommonContext;
import io.aeron.driver.media.UdpChannel;
import org.agrona.LangUtil;

import static io.aeron.driver.Configuration.MULTICAST_FLOW_CONTROL_STRATEGY;

/**
 * Default supplier of {@link FlowControl} strategies for multicast streams which supports defining the strategy in
 * the channel URI as a priority over {@link Configuration#MULTICAST_FLOW_CONTROL_STRATEGY_PROP_NAME}.
 */
public class DefaultMulticastFlowControlSupplier implements FlowControlSupplier
{
    public FlowControl newInstance(final UdpChannel udpChannel, final int streamId, final long registrationId)
    {
        final String fcStr = udpChannel.channelUri().get(CommonContext.FLOW_CONTROL_PARAM_NAME);
        FlowControl flowControl = null;

        if (null != fcStr)
        {
            if (fcStr.startsWith(MaxMulticastFlowControl.FC_PARAM_VALUE))
            {
                return MaxMulticastFlowControl.INSTANCE;
            }
            else if (fcStr.startsWith(MinMulticastFlowControl.FC_PARAM_VALUE))
            {
                return new MinMulticastFlowControl();
            }
            else if (fcStr.startsWith(TaggedMulticastFlowControl.FC_PARAM_VALUE))
            {
                return new TaggedMulticastFlowControl();
            }
            else
            {
                throw new IllegalArgumentException("unsupported multicast flow control strategy : fc=" + fcStr);
            }
        }

        if (MaxMulticastFlowControl.class.getName().equals(MULTICAST_FLOW_CONTROL_STRATEGY))
        {
            return MaxMulticastFlowControl.INSTANCE;
        }
        else if (MinMulticastFlowControl.class.getName().equals(MULTICAST_FLOW_CONTROL_STRATEGY))
        {
            return new MinMulticastFlowControl();
        }
        else if (TaggedMulticastFlowControl.class.getName().equals(MULTICAST_FLOW_CONTROL_STRATEGY))
        {
            return new TaggedMulticastFlowControl();
        }

        try
        {
            flowControl = (FlowControl)Class.forName(MULTICAST_FLOW_CONTROL_STRATEGY)
                .getConstructor()
                .newInstance();
        }
        catch (final Exception ex)
        {
            LangUtil.rethrowUnchecked(ex);
        }

        return flowControl;
    }

    public String toString()
    {
        return "DefaultMulticastFlowControlSupplier{flowControlClass=" +
            MULTICAST_FLOW_CONTROL_STRATEGY + "}";
    }
}

@@ -1416,36 +1416,213 @@
         
-if (fcStr.startsWith
+final int strategyEndIndex = fcStr.indexOf(',');%0A            final String strategyStr = (-1 == strategyEndIndex) ?%0A                    fcStr : fcStr.substring(0, strategyEndIndex);%0A%0A            if 
 (MaxMult
@@ -1644,32 +1644,51 @@
 l.FC_PARAM_VALUE
+.equals(strategyStr
 ))%0A            %7B
@@ -1680,32 +1680,75 @@
 )%0A            %7B%0A
+                System.out.println(%22max%22);%0A
                 
@@ -1822,33 +1822,16 @@
 else if 
-(fcStr.startsWith
 (MinMult
@@ -1853,32 +1853,51 @@
 l.FC_PARAM_VALUE
+.equals(strategyStr
 ))%0A            %7B
@@ -1889,32 +1889,75 @@
 )%0A            %7B%0A
+                System.out.println(%22min%22);%0A
                 
@@ -2032,25 +2032,8 @@
  if 
-(fcStr.startsWith
 (Tag
@@ -2070,32 +2070,97 @@
 AM_VALUE
-))%0A            %7B
+.equals(strategyStr))%0A            %7B%0A                System.out.println(%22tagged%22);
 %0A       

make fc parsing for strategy stricter.