/*
 * 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)
        {
            final int strategyEndIndex = fcStr.indexOf(',');
            final String strategyStr = (-1 == strategyEndIndex) ?
                    fcStr : fcStr.substring(0, strategyEndIndex);

            if (MaxMulticastFlowControl.FC_PARAM_VALUE.equals(strategyStr))
            {
                System.out.println("max");
                return MaxMulticastFlowControl.INSTANCE;
            }
            else if (MinMulticastFlowControl.FC_PARAM_VALUE.equals(strategyStr))
            {
                System.out.println("min");
                return new MinMulticastFlowControl();
            }
            else if (TaggedMulticastFlowControl.FC_PARAM_VALUE.equals(strategyStr))
            {
                System.out.println("tagged");
                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 + "}";
    }
}

@@ -1602,36 +1602,88 @@
 );%0A%0A            
-if (
+switch (strategyStr)%0A            %7B%0A                case 
 MaxMulticastFlow
@@ -1704,38 +1704,22 @@
 AM_VALUE
-.equals(strategyStr))%0A
+:%0A    
         
@@ -1740,47 +1740,8 @@
     
-    
-System.out.println(%22max%22);%0A        
     
@@ -1789,33 +1789,38 @@
 CE;%0A            
-%7D
+    %7D%0A
 %0A            els
@@ -1812,33 +1812,33 @@
             
-else if (
+    case 
 MinMulticast
@@ -1863,38 +1863,22 @@
 AM_VALUE
-.equals(strategyStr))%0A
+:%0A    
         
@@ -1891,47 +1891,8 @@
     
-            System.out.println(%22min%22);%0A
     
@@ -1945,34 +1945,40 @@
 ();%0A            
-%7D%0A
+    %7D%0A%0A 
             else
@@ -1969,33 +1969,32 @@
             
-else if (
+   case 
 TaggedMultic
@@ -2026,29 +2026,9 @@
 ALUE
-.equals(strategyStr))
+:
 %0A   
@@ -2028,38 +2028,32 @@
 UE:%0A        
-    %7B%0A
         
         Syst
@@ -2044,46 +2044,14 @@
     
-   
+%7B%0A
     
- System.out.println(%22tagged%22);%0A
     
@@ -2095,32 +2095,36 @@
 tFlowControl();%0A
+    
             %7D%0A  
@@ -2113,32 +2113,33 @@
               %7D%0A
+%0A
             else
@@ -2138,13 +2138,25 @@
     
-else%0A
+    default:%0A    
     
@@ -2157,32 +2157,36 @@
               %7B%0A
+    
                 
@@ -2282,16 +2282,34 @@
 fcStr);%0A
+                %7D%0A
         

clean up.