|
| 1 | +package spp.cli.commands.instrument |
| 2 | + |
| 3 | +import com.github.ajalt.clikt.core.CliktCommand |
| 4 | +import com.github.ajalt.clikt.parameters.arguments.argument |
| 5 | +import com.github.ajalt.clikt.parameters.arguments.multiple |
| 6 | +import com.github.ajalt.clikt.parameters.options.flag |
| 7 | +import com.github.ajalt.clikt.parameters.options.option |
| 8 | +import eu.geekplace.javapinning.JavaPinning |
| 9 | +import eu.geekplace.javapinning.pin.Pin |
| 10 | +import io.vertx.core.Vertx |
| 11 | +import io.vertx.core.json.Json |
| 12 | +import io.vertx.core.json.JsonObject |
| 13 | +import io.vertx.core.net.NetClientOptions |
| 14 | +import io.vertx.core.net.TrustOptions |
| 15 | +import io.vertx.ext.bridge.BridgeEventType |
| 16 | +import io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper |
| 17 | +import io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser |
| 18 | +import io.vertx.kotlin.coroutines.await |
| 19 | +import kotlinx.coroutines.runBlocking |
| 20 | +import spp.cli.PlatformCLI |
| 21 | +import spp.protocol.SourceMarkerServices |
| 22 | +import spp.protocol.extend.TCPServiceFrameParser |
| 23 | +import spp.protocol.instrument.LiveInstrumentEvent |
| 24 | +import spp.protocol.instrument.LiveInstrumentEventType |
| 25 | +import spp.protocol.instrument.breakpoint.event.LiveBreakpointHit |
| 26 | +import spp.protocol.instrument.breakpoint.event.LiveBreakpointRemoved |
| 27 | +import spp.protocol.instrument.log.event.LiveLogHit |
| 28 | +import spp.protocol.instrument.log.event.LiveLogRemoved |
| 29 | + |
| 30 | +class SubscribeEvents : CliktCommand( |
| 31 | + help = "Listens for and outputs live events. Subscribes to all events by default" |
| 32 | +) { |
| 33 | + |
| 34 | + val instrumentIds by argument( |
| 35 | + name = "Instrument IDs", |
| 36 | + help = "Capture events from specific live instruments" |
| 37 | + ).multiple() |
| 38 | + |
| 39 | + val includeBreakpoints by option("--breakpoints", "-b", help = "Include live breakpoint events") |
| 40 | + .flag(default = false) |
| 41 | + val includeLogs by option("--logs", "-l", help = "Include live log events") |
| 42 | + .flag(default = false) |
| 43 | + val includeMeters by option("--meters", "-m", help = "Include live meter events") |
| 44 | + .flag(default = false) |
| 45 | + val includeTraces by option("--traces", "-t", help = "Include live trace events") |
| 46 | + .flag(default = false) |
| 47 | + |
| 48 | + override fun run() { |
| 49 | + var eventCount = 1 |
| 50 | + runBlocking { |
| 51 | + val vertx = Vertx.vertx() |
| 52 | + val client = if (PlatformCLI.certFingerprint != null) { |
| 53 | + val options = NetClientOptions() |
| 54 | + .setReconnectAttempts(Int.MAX_VALUE).setReconnectInterval(5000) |
| 55 | + .setSsl(PlatformCLI.platformHost.startsWith("https")) |
| 56 | + .setTrustOptions( |
| 57 | + TrustOptions.wrap( |
| 58 | + JavaPinning.trustManagerForPins(listOf(Pin.fromString("CERTSHA256:${PlatformCLI.certFingerprint}"))) |
| 59 | + ) |
| 60 | + ) |
| 61 | + vertx.createNetClient(options) |
| 62 | + } else { |
| 63 | + val options = NetClientOptions() |
| 64 | + .setReconnectAttempts(Int.MAX_VALUE).setReconnectInterval(5000) |
| 65 | + .setSsl(PlatformCLI.platformHost.startsWith("https")) |
| 66 | + vertx.createNetClient(options) |
| 67 | + } |
| 68 | + val socket = client.connect( |
| 69 | + 5455, |
| 70 | + PlatformCLI.platformHost.substringAfter("https://").substringAfter("http://") |
| 71 | + .substringBefore(":") |
| 72 | + ).await() |
| 73 | + socket!!.handler(FrameParser(TCPServiceFrameParser(vertx, socket))) |
| 74 | + |
| 75 | + vertx.eventBus().consumer<JsonObject>("local." + SourceMarkerServices.Provide.LIVE_INSTRUMENT_SUBSCRIBER) { |
| 76 | + val liveEvent = Json.decodeValue(it.body().toString(), LiveInstrumentEvent::class.java) |
| 77 | + |
| 78 | + //todo: impl filter on platform |
| 79 | + if (instrumentIds.isNotEmpty()) { |
| 80 | + when (liveEvent.eventType) { |
| 81 | + LiveInstrumentEventType.LOG_HIT -> { |
| 82 | + val logHit = Json.decodeValue(liveEvent.data, LiveLogHit::class.java) |
| 83 | + if (logHit.logId !in instrumentIds) { |
| 84 | + return@consumer |
| 85 | + } |
| 86 | + } |
| 87 | + LiveInstrumentEventType.BREAKPOINT_HIT -> { |
| 88 | + val breakpointHit = Json.decodeValue(liveEvent.data, LiveBreakpointHit::class.java) |
| 89 | + if (breakpointHit.breakpointId !in instrumentIds) { |
| 90 | + return@consumer |
| 91 | + } |
| 92 | + } |
| 93 | + LiveInstrumentEventType.BREAKPOINT_REMOVED -> { |
| 94 | + val breakpointRemoved = Json.decodeValue(liveEvent.data, LiveBreakpointRemoved::class.java) |
| 95 | + if (breakpointRemoved.breakpointId !in instrumentIds) { |
| 96 | + return@consumer |
| 97 | + } |
| 98 | + } |
| 99 | + LiveInstrumentEventType.LOG_REMOVED -> { |
| 100 | + val logRemoved = Json.decodeValue(liveEvent.data, LiveLogRemoved::class.java) |
| 101 | + if (logRemoved.logId !in instrumentIds) { |
| 102 | + return@consumer |
| 103 | + } |
| 104 | + } |
| 105 | + else -> TODO("Unhandled event type: ${liveEvent.eventType}") |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (!includeBreakpoints && !includeLogs && !includeMeters && !includeTraces) { |
| 110 | + //listen for all events |
| 111 | + println( |
| 112 | + "\nEvent (${eventCount++}):\n" + |
| 113 | + "\tType: ${liveEvent.eventType}\n" + |
| 114 | + "\tData: ${liveEvent.data}" |
| 115 | + ) |
| 116 | + } else { |
| 117 | + //todo: impl filtering on platform |
| 118 | + //listen for specific events |
| 119 | + if (includeBreakpoints && liveEvent.eventType.name.startsWith("breakpoint", true)) { |
| 120 | + println( |
| 121 | + "\nEvent (${eventCount++}):\n" + |
| 122 | + "\tType: ${liveEvent.eventType}\n" + |
| 123 | + "\tData: ${liveEvent.data}" |
| 124 | + ) |
| 125 | + } else if (includeLogs && liveEvent.eventType.name.startsWith("log", true)) { |
| 126 | + println( |
| 127 | + "\nEvent (${eventCount++}):\n" + |
| 128 | + "\tType: ${liveEvent.eventType}\n" + |
| 129 | + "\tData: ${liveEvent.data}" |
| 130 | + ) |
| 131 | + } else if (includeMeters && liveEvent.eventType.name.startsWith("meter", true)) { |
| 132 | + println( |
| 133 | + "\nEvent (${eventCount++}):\n" + |
| 134 | + "\tType: ${liveEvent.eventType}\n" + |
| 135 | + "\tData: ${liveEvent.data}" |
| 136 | + ) |
| 137 | + } else if (includeTraces && liveEvent.eventType.name.startsWith("trace", true)) { |
| 138 | + println( |
| 139 | + "\nEvent (${eventCount++}):\n" + |
| 140 | + "\tType: ${liveEvent.eventType}\n" + |
| 141 | + "\tData: ${liveEvent.data}" |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + //register listener |
| 148 | + FrameHelper.sendFrame( |
| 149 | + BridgeEventType.REGISTER.name.toLowerCase(), |
| 150 | + SourceMarkerServices.Provide.LIVE_INSTRUMENT_SUBSCRIBER, |
| 151 | + JsonObject(), |
| 152 | + socket |
| 153 | + ) |
| 154 | + println("Listening for events...") |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments