package controller;

import com.sun.tools.attach.AttachNotSupportedException;
import communicator.DASPublisher;
import jvmmonitor.UsageMonitor;
import jvmmonitor.exceptions.MonitoringNotStartedException;
import jvmmonitor.model.GarbageCollectionLog;
import jvmmonitor.model.UsageMonitorLog;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
import org.wso2.carbon.databridge.commons.exception.TransportException;

import javax.management.MalformedObjectNameException;
import java.io.IOException;
import java.util.LinkedList;

/*
*  Copyright (c) ${date}, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. 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.
*/

public class Controller {


    public void sendUsageData(String pid) throws IOException,
            AttachNotSupportedException,
            MalformedObjectNameException,
            InterruptedException,
            MonitoringNotStartedException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointException,
            DataEndpointConfigurationException {

        while (true) {

            UsageMonitor usageObj = new UsageMonitor(pid);
            usageObj.stratMonitoring();
            UsageMonitorLog usageLogObj = usageObj.getUsageLog();

            DASPublisher dasPublisherObj = new DASPublisher();

            dasPublisherObj.publishMemoryData(usageLogObj.getDate(), usageLogObj.getMemoryUsageLog());
            dasPublisherObj.publishCPUData(usageLogObj.getDate(), usageLogObj.getCpuLoadLog());
            dasPublisherObj.publishGCData(( LinkedList<GarbageCollectionLog>)usageLogObj.getGarbageCollectionLog() );

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }



    }

}

@@ -804,16 +804,42 @@
 kedList;
+%0Aimport java.util.Scanner;
 %0A%0A/*%0A*  
@@ -1972,36 +1972,8 @@
  %7B%0A%0A
-        while (true) %7B%0A%0A    
     
@@ -2031,20 +2031,16 @@
         
-    
 usageObj
@@ -2063,28 +2063,24 @@
 ();%0A        
- 
-   
 UsageMonitor
@@ -2134,79 +2134,552 @@
     
-    DASPublisher dasPublisherObj = new DASPublisher();%0A%0A            das
+DASPublisher dasMemoryPublisher = new DASPublisher(7611, 9611, %22admin%22, %22admin%22);%0A        DASPublisher dasCPUPublisher = new DASPublisher(7611, 9611, %22admin%22, %22admin%22);%0A        DASPublisher dasGCPublisher = new DASPublisher(7611, 9611, %22admin%22, %22admin%22);%0A%0A        Scanner reader = new Scanner(System.in);  // Reading from System.in%0A        System.out.println(%22Press any key to finish data collecting...%22);%0A        String userInput = %22Start%22;%0A%0A        while (userInput==%22Start%22) %7B%0A%0A            userInput = reader.next();%0A%0A            //dasMemory
 Publ
@@ -2675,35 +2675,32 @@
 sMemoryPublisher
-Obj
 .publishMemoryDa
@@ -2766,32 +2766,35 @@
 %0A            das
+CPU
 PublisherObj.pub
@@ -2778,35 +2778,32 @@
  dasCPUPublisher
-Obj
 .publishCPUData(
@@ -2867,19 +2867,23 @@
         
+//
 das
+GC
 Publishe
@@ -2883,19 +2883,16 @@
 ublisher
-Obj
 .publish
@@ -3139,16 +3139,166 @@
      %7D%0A%0A
+        dasMemoryPublisher.shutdownDataPublisher();%0A        dasCPUPublisher.shutdownDataPublisher();%0A        dasGCPublisher.shutdownDataPublisher();%0A%0A
 %0A%0A    %7D%0A

Add stop condition for a data collection function