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.CapturedPackets;
import com.exalttech.trex.application.TrexApp;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import org.apache.log4j.Logger;

import java.util.List;

public class PktCaptureService extends ScheduledService<CapturedPackets>
    private static Logger LOG = Logger.getLogger(PktCaptureService.class);
    
    private int currentActiveMonitorId = 0;
    
    private TRexClient tRexClient = TrexApp.injector.getInstance(TRexClient.class);
    
    @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 boolean startMonitor(List<Integer> rx, List<Integer> tx) throws PktCaptureServiceException {
        TRexClientResult<CaptureMonitor> result = tRexClient.captureMonitorStart(rx, tx);
        guardNotFailed(result);
        currentActiveMonitorId = result.get().getCaptureId();
        return true;
    }
    
    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 void getActiveCaptures() throws PktCaptureServiceException {
        TRexClientResult<CaptureInfo[]> result = tRexClient.getActiveCaptures();
        guardNotFailed(result);
    }
    
    private void guardNotFailed(TRexClientResult<?> result) throws PktCaptureServiceException {
        if (result.isFailed()) {
            throw new PktCaptureServiceException(result.getError());
        }
    }
}

@@ -357,27 +357,30 @@
 rex.
-application.TrexApp
+core.ConnectionManager
 ;%0Aim
@@ -580,16 +580,18 @@
 Packets%3E
+ %7B
 %0A    pri
@@ -752,23 +752,24 @@
 t = 
-TrexApp.injecto
+ConnectionManage
 r.ge
@@ -778,26 +778,31 @@
 nstance(
-TR
+).getTr
 exClient
 .class);
@@ -793,22 +793,17 @@
 exClient
-.class
+(
 );%0A    %0A
@@ -1713,24 +1713,41 @@
 aptureId();%0A
+        start();%0A
         retu
@@ -1757,32 +1757,221 @@
 true;%0A    %7D%0A
+%0A
     
-%0A   
+synchronized public void stopMonitor() %7B%0A        tRexClient.captureMonitorStop(currentActiveMonitorId);%0A        tRexClient.captureMonitorRemove(currentActiveMonitorId);%0A    %7D%0A%0A    synchronized
  public Capt

Add stopMonitor to pktCaptureService