package communicator;

import jvmmonitor.model.CPULoadLog;
import jvmmonitor.model.GarbageCollectionLog;
import jvmmonitor.model.MemoryUsageLog;
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 java.io.FileNotFoundException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

/*
*  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 DASPublisher {

    public void publishMemoryData(long date, MemoryUsageLog memoryUsageLog) throws SocketException,
            UnknownHostException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            DataEndpointException,
            DataEndpointConfigurationException,
            TransportException {

        String HTTPD_LOG_STREAM="memoryStream";
        String VERSION="1.0.0";
        int defaultThriftPort=7611;
        int defaultBinaryPort=9611;
        HttpdAgent agent = new HttpdAgent(HTTPD_LOG_STREAM, VERSION, defaultThriftPort, defaultBinaryPort);

        agent.initialize();
        agent.publishLogEvents(date, memoryUsageLog);


    }

    public void publishGCData(List<GarbageCollectionLog> garbageCollectionLog) throws SocketException,
            UnknownHostException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            DataEndpointException,
            DataEndpointConfigurationException,
            TransportException {

        String HTTPD_LOG_STREAM="gcStream";
        String VERSION="1.0.0";
        int defaultThriftPort=7611;
        int defaultBinaryPort=9611;
        HttpdAgent agent = new HttpdAgent(HTTPD_LOG_STREAM, VERSION, defaultThriftPort, defaultBinaryPort);

        agent.initialize();
        for (GarbageCollectionLog gc:garbageCollectionLog) {
            agent.publishLogEvents(gc);
        }

    }

    public void publishCPUData(long date, CPULoadLog cpuLoadLog) throws SocketException,
            UnknownHostException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            DataEndpointException,
            DataEndpointConfigurationException,
            TransportException {

        String HTTPD_LOG_STREAM="cpuStream";
        String VERSION="1.0.0";
        int defaultThriftPort=7611;
        int defaultBinaryPort=9611;
        HttpdAgent agent = new HttpdAgent(HTTPD_LOG_STREAM, VERSION, defaultThriftPort, defaultBinaryPort);

        agent.initialize();
        agent.publishLogEvents(date, cpuLoadLog);

    }

    public void publishXXgcLogData(String fileName) throws SocketException,
            UnknownHostException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            DataEndpointException,
            DataEndpointConfigurationException,
            TransportException,
            FileNotFoundException {

        String HTTPD_LOG_STREAM="gcLogStream";
        String VERSION="1.0.0";
        int defaultThriftPort=7611;
        int defaultBinaryPort=9611;
        HttpdAgent agent = new HttpdAgent(HTTPD_LOG_STREAM, VERSION, defaultThriftPort, defaultBinaryPort);

        agent.initialize();
        agent.publishLogEvents(fileName);

    }


}

@@ -649,32 +649,61 @@
 nHostException;%0A
+import java.util.LinkedList;%0A
 import java.util
@@ -1790,17 +1790,19 @@
 G_STREAM
-=
+ = 
 %22memoryS
@@ -1823,33 +1823,35 @@
   String VERSION
-=
+ = 
 %221.0.0%22;%0A       
@@ -1864,33 +1864,35 @@
 efaultThriftPort
-=
+ = 
 7611;%0A        in
@@ -1902,33 +1902,35 @@
 efaultBinaryPort
-=
+ = 
 9611;%0A        Ht
@@ -2149,16 +2149,22 @@
 hGCData(
+Linked
 List%3CGar
@@ -2512,17 +2512,19 @@
 G_STREAM
-=
+ = 
 %22gcStrea
@@ -2541,33 +2541,35 @@
   String VERSION
-=
+ = 
 %221.0.0%22;%0A       
@@ -2582,33 +2582,35 @@
 efaultThriftPort
-=
+ = 
 7611;%0A        in
@@ -2620,33 +2620,35 @@
 efaultBinaryPort
-=
+ = 
 9611;%0A        Ht
@@ -2742,32 +2742,33 @@
 naryPort);%0A%0A
+%0A
         
 agent.initia
@@ -2759,87 +2759,46 @@
     
-agent.initialize();%0A        for (GarbageCollectionLog gc:garbageCollectionLog) 
+while(!garbageCollectionLog.isEmpty())
 %7B%0A  
@@ -2830,18 +2830,45 @@
 gEvents(
-gc
+ garbageCollectionLog.poll() 
 );%0A     
@@ -3253,17 +3253,19 @@
 G_STREAM
-=
+ = 
 %22cpuStre
@@ -3283,33 +3283,35 @@
   String VERSION
-=
+ = 
 %221.0.0%22;%0A       
@@ -3324,33 +3324,35 @@
 efaultThriftPort
-=
+ = 
 7611;%0A        in
@@ -3362,33 +3362,35 @@
 efaultBinaryPort
-=
+ = 
 9611;%0A        Ht
@@ -3969,17 +3969,19 @@
 G_STREAM
-=
+ = 
 %22gcLogSt
@@ -4009,17 +4009,19 @@
  VERSION
-=
+ = 
 %221.0.0%22;
@@ -4050,17 +4050,19 @@
 riftPort
-=
+ = 
 7611;%0A  
@@ -4088,17 +4088,19 @@
 naryPort
-=
+ = 
 9611;%0A  

Update publishGCData for LinkedList