pi/netsender: create run function for use by clients

Issue #57 new
Saxon Milton created an issue

A common pattern seen in netsender clients is to implement a “run” function that Calls the Netsender.Run() method, Sends net logs, checks the varsum and updates things based on any var changes. It always closely resembles the following (this one from the rv client):

// run starts the main loop. This will run netsender on every pass of the loop
// (sleeping inbetween), check vars, and if changed, update revid as appropriate.
func run(rv *revid.Revid, ns *netsender.Sender, l logging.Logger, nl *netlogger.Logger, p *turbidityProbe) {
    var vs int
    for {
        l.Debug("running netsender")
        err := ns.Run()
        if err != nil {
            l.Warning(pkg+"Run Failed. Retrying...", "error", err.Error())
            time.Sleep(netSendRetryTime)
            continue
        }

        l.Debug("sending logs")
        err = nl.Send(ns)
        if err != nil {
            l.Warning(pkg+"Logs could not be sent", "error", err.Error())
        }

        l.Debug("checking varsum")
        newVs := ns.VarSum()
        if vs == newVs {
            sleep(ns, l)
            continue
        }
        vs = newVs
        l.Info("varsum changed", "vs", vs)

        l.Debug("getting new vars")
        vars, err := ns.Vars()
        if err != nil {
            l.Error(pkg+"netSender failed to get vars", "error", err.Error())
            time.Sleep(netSendRetryTime)
            continue
        }
        l.Debug("got new vars", "vars", vars)

        // Configure revid based on the vars.
        l.Debug("updating revid's configuration")
        err = rv.Update(vars)
        if err != nil {
            l.Warning(pkg+"couldn't update revid", "error", err.Error())
            sleep(ns, l)
            continue
        }
        l.Info("revid successfully reconfigured")

        // Update transform matrix based on new revid variables.
        err = p.Update(rv.Config().TransformMatrix)
        if err != nil {
            l.Error("could not update turbidity probe", "error", err.Error())
        }

        l.Debug("checking mode")
        switch ns.Mode() {
        case modePaused:
            l.Debug("mode is Paused, stopping revid")
            rv.Stop()
        case modeNormal, modeLoop:
            l.Debug("mode is Normal or Loop, starting revid")
            err = rv.Start()
            if err != nil {
                l.Error(pkg+"could not start revid", "error", err.Error())
                ns.SetMode(modePaused, &vs)
                sleep(ns, l)
                continue
            }
        case modeBurst:
            l.Debug("mode is Burst, bursting revid")
            err = rv.Burst()
            if err != nil {
                l.Warning(pkg+"could not start burst", "error", err.Error())
                ns.SetMode(modePaused, &vs)
                sleep(ns, l)
                continue
            }
            ns.SetMode(modePaused, &vs)
        }
        l.Info("revid updated with new mode")

        sleep(ns, l)
    }
}

I think what should actually happen is that Netsender.Run() should do all of this, and for anything more custom like actions based on any changes of variables we can provide a callback.

Comments (0)

  1. Log in to comment