package com.github.bingoohuang.blackcat.instrument.discruptor;

import com.github.bingoohuang.blackcat.instrument.callback.BlackcatMethodRt;
import com.github.bingoohuang.blackcat.instrument.callback.BlackcatMetricMsg;
import com.github.bingoohuang.blackcat.instrument.callback.BlackcatTraceMsg;
import com.github.bingoohuang.blackcat.sdk.netty.BlackcatNettyClient;
import com.github.bingoohuang.blackcat.sdk.protobuf.BlackcatMsg.BlackcatReq;
import com.lmax.disruptor.dsl.Disruptor;
import lombok.val;

import java.util.concurrent.Executors;

public class BlackcatClient {
    static BlackcatMethodRuntimeProducer blackcatMethodRtProducer;
    static BlackcatTraceMessageProducer traceMessageProducer;
    static BlackcatMetricProducer metricProducer;

    static {
        // Executor that will be used to construct new threads for consumers
        val executor = Executors.newSingleThreadExecutor();
        // The factory for the event
        val factory = new BlackcatReqFactory();
        // Specify the size of the ring buffer, must be power of 2.
        val bufferSize = 1024;
        // Construct the Disruptor
        val disruptor = new Disruptor<BlackcatReq.Builder>(factory, bufferSize, executor);

        val nettyClient = new BlackcatNettyClient();
        nettyClient.connect();

        // Connect the handler
        val handler = new BlackcatReqEventHandler(nettyClient);
        disruptor.handleEventsWith(handler);

        // Start the Disruptor, starts all threads running
        disruptor.start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        val ringBuffer = disruptor.getRingBuffer();

        blackcatMethodRtProducer = new BlackcatMethodRuntimeProducer(ringBuffer);
        traceMessageProducer = new BlackcatTraceMessageProducer(ringBuffer);
        metricProducer = new BlackcatMetricProducer(ringBuffer);
    }

    public static void send(BlackcatMethodRt blackcatMethodRt) {
        blackcatMethodRtProducer.send(blackcatMethodRt);
    }

    public static void send(BlackcatTraceMsg traceMsg) {
        traceMessageProducer.send(traceMsg);
    }

    public static void send(BlackcatMetricMsg blackcatMetricMsg) {
        metricProducer.send(blackcatMetricMsg);
    }
}

@@ -495,16 +495,53 @@
 bok.val;
+%0Aimport org.n3r.diamond.client.Miner;
 %0A%0Aimport
@@ -801,145 +801,8 @@
 c %7B%0A
-        // Executor that will be used to construct new threads for consumers%0A        val executor = Executors.newSingleThreadExecutor();%0A
     
@@ -993,34 +993,61 @@
     
-// Construct the Disruptor
+val threadFactory = Executors.defaultThreadFactory();
 %0A   
@@ -1127,16 +1127,21 @@
 ze, 
-execu
+threadFac
 tor
+y
 );%0A%0A
@@ -1365,24 +1365,42 @@
 ndler);%0A
-%0A
+ 
        
+disruptor.start();
  // Star
@@ -1446,35 +1446,8 @@
 ning
-%0A        disruptor.start();
 %0A%0A  
@@ -1569,25 +1569,24 @@
 ngBuffer();%0A
-%0A
         blac
@@ -1865,24 +1865,68 @@
 MethodRt) %7B%0A
+        if (!isBlackcatSwitchOn()) return;%0A%0A
         blac
@@ -2030,24 +2030,68 @@
 traceMsg) %7B%0A
+        if (!isBlackcatSwitchOn()) return;%0A%0A
         trac
@@ -2193,24 +2193,68 @@
 etricMsg) %7B%0A
+        if (!isBlackcatSwitchOn()) return;%0A%0A
         metr
@@ -2294,11 +2294,182 @@
 );%0A    %7D
+%0A%0A    public static boolean isBlackcatSwitchOn() %7B%0A        val switchConf = new Miner().getStone(%22blackcatserver%22, %22switch%22);%0A        return %22on%22.equals(switchConf);%0A    %7D
 %0A%7D%0A

switch for blackcat client send