/**
 * *****************************************************************************
 * Copyright (c) 2016
 *
 * 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
 *     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 com.exalttech.trex.ui.views.services;

import com.exalttech.trex.core.ConnectionManager;
import com.exalttech.trex.ui.models.Port;
import com.exalttech.trex.ui.models.PortStatus;
import com.exalttech.trex.ui.models.datastore.CaptureStatus;
import com.exalttech.trex.util.Constants;
import com.exalttech.trex.util.Util;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author GeorgeKh
 */
public class UpdatePortStatusService extends ScheduledService<List<Port>> {

    private static final Logger LOG = Logger.getLogger(UpdatePortStatusService.class.getName());
    List<Port> portList;
    Task portStatTask;
    ObjectMapper mapper = new ObjectMapper();

    /**
     *
     * @param portList
     */
    public UpdatePortStatusService(List<Port> portList) {
        this.portList = portList;
    }

    @Override
    protected Task<List<Port>> createTask() {
        return new Task<List<Port>>() {
            @Override
            protected List<Port> call() throws Exception {
                return updatePortList();
            }
        };
    }

    /**
     * Update port list
     *
     * @return
     */
    private List<Port> updatePortList() {
        try {
            String response = ConnectionManager.getInstance().sendPortStatusRequest(portList);
            if (response == null) {
                return new ArrayList<>();
            }
            List<PortStatus> portStatusList = mapper.readValue(response, mapper.getTypeFactory().constructCollectionType(List.class, PortStatus.class));
            PortStatus portStatus = null;
            for (Port port : portList) {
                portStatus = portStatusList.get(port.getIndex());
                port.setOwner(portStatus.getResult().getOwner());
                port.setStatus(portStatus.getResult().getState());
                port.setAttr(portStatus.getResult().getAttr());
                port.setRx_info(portStatus.getResult().getRx_info());

                String response1 = ConnectionManager.getInstance().sendRequest(Constants.CAPTURE_METHOD,
                        "\"command\":\"status\"");
                CaptureStatus[] capture = Util.getCaptureStatusFromJSONString(response1);
                port.setCaptureStatus(null);
                for (int i = 0; i < capture.length; i++) {
                    int port_index_mask = 1 << port.getIndex();
                    if ((capture[i].getFilter().getRx() & port_index_mask) != 0) {
                        port.setCaptureStatus(capture);
                        break;
                    }
                    if ((capture[i].getFilter().getTx() & port_index_mask) != 0) {
                        port.setCaptureStatus(capture);
                        break;
                    }
                }

                String response2 = ConnectionManager.getInstance().sendPortXStatsNamesRequest(port);
                String response3 = ConnectionManager.getInstance().sendPortXStatsValuesRequest(port);
                Map<String, Long> loadedXStatsList = Util.getXStatsFromJSONString(response2, response3);
                port.setXstats(loadedXStatsList);

                // Is it right ?
                port.setSpeed(port.getAttr().getSpeed());
            }
            return portList;
        } catch (Exception ex) {
            LOG.error("Error reading port status", ex);
            return new ArrayList<>();
        }
    }

    /**
     * Return updated port list
     *
     * @return
     */
    public List<Port> getUpdatedPortList() {
        return updatePortList();
    }

}

@@ -937,148 +937,8 @@
 us;%0A
-import com.exalttech.trex.ui.models.datastore.CaptureStatus;%0Aimport com.exalttech.trex.util.Constants;%0Aimport com.exalttech.trex.util.Util;%0A
 impo
@@ -1146,30 +1146,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0A/*
@@ -2714,24 +2714,26 @@
 x_info());%0A%0A
+//
             
@@ -2825,16 +2825,18 @@
 METHOD,%0A
+//
         
@@ -2878,16 +2878,18 @@
 us%5C%22%22);%0A
+//
         
@@ -2970,16 +2970,18 @@
 onse1);%0A
+//
         
@@ -3017,16 +3017,18 @@
 (null);%0A
+//
         
@@ -3074,24 +3074,26 @@
 gth; i++) %7B%0A
+//
             
@@ -3140,24 +3140,26 @@
 getIndex();%0A
+//
             
@@ -3221,32 +3221,34 @@
 x_mask) != 0) %7B%0A
+//
                 
@@ -3279,32 +3279,34 @@
 tatus(capture);%0A
+//
                 
@@ -3312,32 +3312,34 @@
          break;%0A
+//
                 
@@ -3336,32 +3336,34 @@
               %7D%0A
+//
                 
@@ -3425,24 +3425,26 @@
 sk) != 0) %7B%0A
+//
             
@@ -3487,16 +3487,18 @@
 pture);%0A
+//
         
@@ -3520,16 +3520,18 @@
  break;%0A
+//
         
@@ -3536,32 +3536,34 @@
               %7D%0A
+//
                 
@@ -3560,25 +3560,29 @@
           %7D%0A
-%0A
+//%0A//
             
@@ -3662,32 +3662,34 @@
 sRequest(port);%0A
+//
                 
@@ -3770,24 +3770,26 @@
 uest(port);%0A
+//
             
@@ -3881,16 +3881,18 @@
 onse3);%0A
+//
         

Disable fetching port hardware counters and capture stastus.