package com.cisco.trex.stl.gui.services.capture;

import com.cisco.trex.stateless.TRexClient;
import com.cisco.trex.stateless.model.TRexClientResult;
import com.cisco.trex.stateless.model.capture.CaptureInfo;
import com.cisco.trex.stateless.model.capture.CaptureMonitor;
import com.cisco.trex.stateless.model.capture.CaptureMonitorStop;
import com.cisco.trex.stateless.model.capture.CapturedPackets;
import com.exalttech.trex.core.ConnectionManager;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import org.apache.log4j.Logger;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PktCaptureService extends ScheduledService<CapturedPackets> {
    private static Logger LOG = Logger.getLogger(PktCaptureService.class);
    
    private int currentActiveMonitorId = 0;
    
    private TRexClient tRexClient = ConnectionManager.getInstance().getTrexClient();
    
    @Override
    protected Task<CapturedPackets> createTask() {
        return new Task<CapturedPackets>() {
            @Override
            protected CapturedPackets call() throws Exception {
                if (currentActiveMonitorId == 0) {
                    return null;
                }
                try {
                    return fetchCapturedPkts(currentActiveMonitorId, 10);
                } catch (PktCaptureServiceException e) {
                    LOG.error("Unable to fetch pkts from monitor.", e);
                    return null;
                }
                
            }
        };
    }
    
    public int startMonitor(List<Integer> rx, List<Integer> tx) throws PktCaptureServiceException {
        TRexClientResult<CaptureMonitor> result = tRexClient.captureMonitorStart(rx, tx);
        guardNotFailed(result);
        currentActiveMonitorId = result.get().getCaptureId();
        start();
        return currentActiveMonitorId;
    }

    synchronized public void stopMonitor() {
        tRexClient.captureMonitorStop(currentActiveMonitorId);
        tRexClient.captureMonitorRemove(currentActiveMonitorId);
    }

    public CaptureMonitorStop stopRecorder(int caputureId) throws PktCaptureServiceException {
        TRexClientResult<CaptureMonitorStop> result = tRexClient.captureMonitorStop(caputureId);
        if (result.isFailed()) {
            LOG.error("Unable to stop monitor. "+result.getError());
            throw new PktCaptureServiceException(result.getError());
        }
        return result.get();
    }

    synchronized public CapturedPackets fetchCapturedPkts(int captureId, int chunkSize) throws PktCaptureServiceException {
        TRexClientResult<CapturedPackets> result = tRexClient.captureFetchPkts(captureId, chunkSize);
        guardNotFailed(result);
        return result.get();
    }
    
    public CaptureMonitor addRecorder(List<Integer> rx, List<Integer> tx, int bufferSize) throws PktCaptureServiceException {
        TRexClientResult<CaptureMonitor> result = tRexClient.captureRecorderStart(rx, tx, bufferSize);
        guardNotFailed(result);
        return result.get();
    }
    
    public boolean removeRecorder(int recorderId) {
        return tRexClient.captureMonitorRemove(recorderId);
    }
    
    public List<CaptureInfo> getActiveCaptures() throws PktCaptureServiceException {
        TRexClientResult<CaptureInfo[]> result = tRexClient.getActiveCaptures();
        guardNotFailed(result);
        return Arrays.stream(result.get()).collect(Collectors.toList());
    }
    
    private void guardNotFailed(TRexClientResult<?> result) throws PktCaptureServiceException {
        if (result.isFailed()) {
            throw new PktCaptureServiceException(result.getError());
        }
    }
}

@@ -1618,16 +1618,39 @@
 eger%3E tx
+, boolean serviceEnable
 ) throws
@@ -1809,36 +1809,27 @@
         
-currentActiveMonitor
+int capture
 Id = res
@@ -1866,16 +1866,28 @@
     
-start();
+if (serviceEnable) %7B
 %0A   
@@ -1891,22 +1891,19 @@
         
-return
+   
  current
@@ -1921,100 +1921,207 @@
 orId
-;%0A    %7D%0A%0A    synchronized public void stopMonitor() %7B%0A        tRexClient.captureMonitorStop(
+ = captureId;%0A            start();%0A        %7D%0A        return captureId;%0A    %7D%0A%0A    synchronized public void stopMonitor(int captureId) %7B%0A        if (currentActiveMonitorId == captureId) %7B%0A            
 curr
@@ -2134,27 +2134,40 @@
 iveMonitorId
-)
+ = 0
 ;%0A
+        %7D%0A
         tRex
@@ -2191,35 +2191,72 @@
 itor
-Remove(currentActiveMonitor
+Stop(captureId);%0A        tRexClient.captureMonitorRemove(capture
 Id);

Add possibility to start multiple monitors.