/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *     http://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 org.apache.stratos.haproxy.extension;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.common.util.CommandUtils;
import org.apache.stratos.load.balancer.common.statistics.LoadBalancerStatisticsReader;
import org.apache.stratos.messaging.domain.topology.Cluster;
import org.apache.stratos.messaging.domain.topology.Member;
import org.apache.stratos.messaging.domain.topology.Port;
import org.apache.stratos.messaging.domain.topology.Service;
import org.apache.stratos.messaging.message.receiver.topology.TopologyManager;

import java.io.IOException;

/**
 * HAProxy statistics reader.
 */
public class HAProxyStatisticsReader implements LoadBalancerStatisticsReader {
    private static final Log log = LogFactory.getLog(HAProxyStatisticsReader.class);

    private String scriptsPath;
    private String statsSocketFilePath;

    public HAProxyStatisticsReader() {
        this.scriptsPath = HAProxyContext.getInstance().getScriptsPath();
        this.statsSocketFilePath = HAProxyContext.getInstance().getStatsSocketFilePath();
    }

    @Override
    public int getInFlightRequestCount(String clusterId) {
        String frontendId, backendId, command, output;
        String[] array;
        int totalWeight, weight;

        for (Service service : TopologyManager.getTopology().getServices()) {
            for (Cluster cluster : service.getClusters()) {
                if (cluster.getClusterId().equals(clusterId)) {
                    totalWeight = 0;
                    if ((service.getPorts() == null) || (service.getPorts().size() == 0)) {
                        throw new RuntimeException(String.format("No ports found in service: %s", service.getServiceName()));
                    }

                    for (Port port : service.getPorts()) {
                        frontendId = cluster.getClusterId() + "-host-" + HAProxyContext.getInstance().getHAProxyPrivateIp() + "-proxy-" + port.getProxy();
                        //frontendId = cluster.getClusterId() + "-proxy-" + port.getProxy();
                        backendId = frontendId + "-members";
                        
                        for (Member member : cluster.getMembers()) {
                            // echo "get weight <backend>/<server>" | socat stdio <stats-socket>
                            command = String.format("%s/get-weight.sh %s %s %s", scriptsPath, backendId, member.getMemberId(), statsSocketFilePath);
                            try {
                                output = CommandUtils.executeCommand(command);
                                if ((output != null) && (output.length() > 0)) {
                                    array = output.split(" ");
                                    if ((array != null) && (array.length > 0)) {
                                        weight = Integer.parseInt(array[0]);
                                        if (log.isDebugEnabled()) {
                                            log.debug(String.format("Member weight found: [cluster] %s [member] %s [weight] %d", member.getClusterId(), member.getMemberId(), weight));
                                        }
                                        totalWeight += weight;
                                    }
                                }
                            } catch (IOException e) {
                                if (log.isErrorEnabled()) {
                                    log.error(e);
                                }
                            }
                        }
                    }
                    if (log.isInfoEnabled()) {
                        log.info(String.format("Cluster weight found: [cluster] %s [weight] %d", cluster.getClusterId(), totalWeight));
                    }
                    return totalWeight;
                }
            }
        }
        return 0;
    }
}

@@ -2665,231 +2665,59 @@
    f
-rontendId = cluster.getClusterId() + %22-host-%22 + HAProxyContext.getInstance().getHAProxyPrivateIp() + %22-proxy-%22 + port.getProxy();%0A                        //frontendId = cluster.getClusterId() + %22-proxy-%22 + port.getProxy();%0A
+or(String hostname : cluster.getHostNames()) %7B%0A    
     
@@ -2752,22 +2752,23 @@
 d = 
-frontendId + %22
+hostname+%22-http
 -mem
@@ -2798,37 +2798,16 @@
         
-%0A                
-    
     for 
@@ -2839,32 +2839,36 @@
 getMembers()) %7B%0A
+    
                 
@@ -2976,16 +2976,20 @@
         
+   
+ 
 command 
@@ -3133,14 +3133,22 @@
     
+    
 try %7B%0A
+    
     
@@ -3250,24 +3250,28 @@
             
+  
+  
 if ((output 
@@ -3299,32 +3299,36 @@
 ength() %3E 0)) %7B%0A
+    
                 
@@ -3406,24 +3406,28 @@
             
+    
 if ((array !
@@ -3499,16 +3499,20 @@
         
+ 
+   
 weight =
@@ -3572,32 +3572,36 @@
                 
+    
 if (log.isDebugE
@@ -3604,32 +3604,36 @@
 bugEnabled()) %7B%0A
+    
                 
@@ -3832,34 +3832,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -3941,34 +3941,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -4021,16 +4021,20 @@
         
+    
 %7D catch 
@@ -4075,32 +4075,36 @@
                 
+  
+  
 if (log.isErrorE
@@ -4147,24 +4147,28 @@
             
+    
 log.error(e)
@@ -4169,16 +4169,54 @@
 ror(e);%0A
+                                    %7D%0A
         

fix for STRATOS-703