// Copyright (C) 2017-2021  Nexedi SA and Contributors.
//                          Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.

package neo
// NEO/go event tracer

import (
	"lab.nexedi.com/kirr/go123/tracing"
	"lab.nexedi.com/kirr/go123/xnet"

	"lab.nexedi.com/kirr/neo/go/neo/neonet"
	"lab.nexedi.com/kirr/neo/go/neo/proto"
	"lab.nexedi.com/kirr/neo/go/neo/xneo"
)

// GoTracer collects events from NEO/go trace points and sends them to event dispatcher.
type GoTracer struct {
	pg *tracing.ProbeGroup
	rx interface { RxEvent(interface{}) }

	node2Name	   map[*xneo.Node]string
	nodeTab2Owner	   map[*xneo.NodeTable]string
	clusterState2Owner map[*proto.ClusterState]string
}

func NewTraceCollector(rx interface { RxEvent(interface{}) }) *GoTracer {
	return &GoTracer{
		pg: &tracing.ProbeGroup{},
		rx: rx,

		node2Name:          make(map[*xneo.Node]string),
		nodeTab2Owner:      make(map[*xneo.NodeTable]string),
		clusterState2Owner: make(map[*proto.ClusterState]string),
	}
}

//trace:import "lab.nexedi.com/kirr/neo/go/neo/neonet"
//trace:import "lab.nexedi.com/kirr/neo/go/neo/proto"
//trace:import "lab.nexedi.com/kirr/neo/go/neo/xneo"

// Attach attaches the tracer to appropriate trace points.
func (t *GoTracer) Attach() {
	tracing.Lock()
	//neo_traceMsgRecv_Attach(t.pg, t.traceNeoMsgRecv)
	neonet_traceMsgSendPre_Attach(t.pg, t.traceNeoMsgSendPre)
	proto_traceClusterStateChanged_Attach(t.pg, t.traceClusterState)
	xneo_traceNodeChanged_Attach(t.pg, t.traceNode)
	traceMasterStartReady_Attach(t.pg, t.traceMasterStartReady)
	tracing.Unlock()
}

func (t *GoTracer) Detach() {
	t.pg.Done()
}

// RegisterNode lets the tracer know ptr-to-node-state -> node name relation.
//
// This way it can translate e.g. *NodeTable -> owner node name when creating
// corresponding event.
func (t *GoTracer) RegisterNode(node *xneo.Node, name string) {
	tracing.Lock()
	defer tracing.Unlock()

	// XXX verify there is no duplicate names
	// XXX verify the same pointer is not registerd twice
	t.node2Name[node] = name
	t.nodeTab2Owner[node.State.NodeTab] = name
	t.clusterState2Owner[&node.State.Code] = name
}

func (t *GoTracer) TraceNetDial(ev *xnet.TraceDial) {
	t.rx.RxEvent(&eventNetDial{
		Dialer: ev.Dialer,
		Addr:   ev.Addr,
	})
}

func (t *GoTracer) TraceNetConnect(ev *xnet.TraceConnect) {
	t.rx.RxEvent(&eventNetConnect{
		Src:	ev.Src.String(),
		Dst:	ev.Dst.String(),
		Dialed: ev.Dialed,
	})
}

func (t *GoTracer) TraceNetListen(ev *xnet.TraceListen)	{
	t.rx.RxEvent(&eventNetListen{Laddr: ev.Laddr.String()})
}

func (t *GoTracer) TraceNetTx(ev *xnet.TraceTx) {} // we use traceNeoMsgSend instead

func (t *GoTracer) traceNeoMsgSendPre(l *neonet.NodeLink, connID uint32, msg proto.Msg) {
	t.rx.RxEvent(&eventNeoSend{l.LocalAddr().String(), l.RemoteAddr().String(), connID, msg})
}

func (t *GoTracer) traceClusterState(cs *proto.ClusterState) {
	where := t.clusterState2Owner[cs]
	t.rx.RxEvent(&eventClusterState{where, *cs})
}

func (t *GoTracer) traceNode(nt *xneo.NodeTable, n *xneo.PeerNode) {
	where := t.nodeTab2Owner[nt]
	t.rx.RxEvent(&eventNodeTab{where, n.NodeInfo})
}

func (t *GoTracer) traceMasterStartReady(m *Master, ready bool) {
	where := t.node2Name[m.node]
	t.rx.RxEvent(&eventMStartReady{where, ready})
}
