diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-07-29 01:00:35 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-07-29 01:00:35 (GMT) |
commit | 34b75131b9cb6fd510fe47af865cf5ec7d52a31b (patch) | |
tree | 4d1a0d9f822ef4f626917af6d700c76841157afa /src | |
parent | 1b9e777c9cec218e040166dd5ae12c5bde843f20 (diff) |
the telnet client now uses sessions as well
Diffstat (limited to 'src')
-rw-r--r-- | src/rhimportd/ctrlTelnet.go | 122 |
1 files changed, 72 insertions, 50 deletions
diff --git a/src/rhimportd/ctrlTelnet.go b/src/rhimportd/ctrlTelnet.go index 19d3489..1339a81 100644 --- a/src/rhimportd/ctrlTelnet.go +++ b/src/rhimportd/ctrlTelnet.go @@ -26,6 +26,7 @@ package main import ( "fmt" + "math" "net/http" "strconv" "strings" @@ -35,11 +36,11 @@ import ( "github.com/spreadspace/telgo" ) -func telnetQuit(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { +func telnetQuit(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) bool { return true } -func telnetHelp(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { +func telnetHelp(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) bool { switch len(args) { case 2: switch args[1] { @@ -175,7 +176,7 @@ func telnetSetFilePolicy(c *telgo.Client, param *rhimport.FilePolicy, val string } } -func telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { +func telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) bool { if len(args) != 3 { c.Sayln("wrong number of arguments") return false @@ -183,8 +184,7 @@ func telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.D var ctx *rhimport.Context if c.UserData == nil { - logname := "telnet-" + c.Conn.RemoteAddr().String() - c.UserData = rhimport.NewContext(conf, db, getStdLog(logname+"-std"), getDbgLog(logname+"-dbg")) + c.UserData = rhimport.NewContext(conf, nil, getStdLog("sess-%s-std"), getDbgLog("sess-%s-dbg")) ctx = c.UserData.(*rhimport.Context) ctx.Trusted = false } else { @@ -227,7 +227,7 @@ func telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.D return false } -func telnetReset(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { +func telnetReset(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) bool { if len(args) > 1 { c.Sayln("too many arguments") return false @@ -237,7 +237,7 @@ func telnetReset(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb return false } -func telnetShow(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { +func telnetShow(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) bool { if len(args) > 1 { c.Sayln("too many arguments") return false @@ -267,73 +267,95 @@ func telnetShow(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb. } func telnetProgressCallback(step int, stepName string, current, total float64, title string, cart, cut uint, userdata interface{}) bool { - c := userdata.(*telgo.Client) - if cart > 0 { - c.Say("%s: %3.2f%% (Cart/Cut: %d/%d)\r", stepName, (current/total)*100, cart, cut) - } else { - c.Say("%s: %3.2f%%\r", stepName, (current/total)*100) + if math.IsNaN(current) || math.IsInf(current, 0) { + current = 0.0 + } + if math.IsNaN(total) || math.IsInf(total, 0) { + total = 0.0 + } + c := userdata.(chan<- rhimport.ProgressData) + select { + case c <- rhimport.ProgressData{Step: step, StepName: stepName, Current: current, Total: total, Title: title, Cart: cart, Cut: cut}: + default: } return true } -func telnetRun(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { +func telnetDoneCallback(res rhimport.Result, userdata interface{}) bool { + c := userdata.(chan<- rhimport.Result) + c <- res + return true +} + +func telnetRun(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) bool { if c.UserData == nil { c.Sayln("context is empty please set at least one option") return false } ctx := c.UserData.(*rhimport.Context) - if err := ctx.SanityCheck(); err != nil { - c.Sayln("sanity check for import context returned: %s", err) + + id, s, code, errstring := sessions.New(ctx, "") + if code != http.StatusOK { + c.Sayln("creating session failed: %s", errstring) return false } + c.Sayln("got session id: %s", id) - ctx.ProgressCallBack = telnetProgressCallback - ctx.ProgressCallBackData = c - ctx.Cancel = c.Cancel - - c.Sayln("fetching file from '%s'", ctx.SourceUri) - if res, err := rhimport.FetchFile(ctx); err != nil { - c.Sayln("fetch file error: %s", err) - return false - } else if res.ResponseCode != http.StatusOK { - c.Sayln("fetch file error: %s", res.ErrorString) + donechan := make(chan rhimport.Result, 1) + if err := s.AddDoneHandler((chan<- rhimport.Result)(donechan), telnetDoneCallback); err != nil { + c.Sayln("installing done callback failed: %s", errstring) return false } - c.Sayln("") - c.Sayln("normalizing file '%s'", ctx.SourceFile) - if res, err := rhimport.NormalizeFile(ctx); err != nil { - c.Sayln("normalize file error: %s", err) - return false - } else if res.ResponseCode != http.StatusOK { - c.Sayln("normalize file error: %s", res.ErrorString) + progresschan := make(chan rhimport.ProgressData, 10) + if err := s.AddProgressHandler((chan<- rhimport.ProgressData)(progresschan), telnetProgressCallback); err != nil { + c.Sayln("installing progress callback failed: %s", errstring) return false } - c.Sayln("") - c.Sayln("importing file '%s'", ctx.SourceFile) - if res, err := rhimport.ImportFile(ctx); err != nil { - c.Sayln("") - c.Sayln("import file error: %s", err) - } else { - c.Sayln("") - if res.ResponseCode == http.StatusOK { - c.Sayln("File got succesfully imported into Cart/Cut %d/%d, Source: '%s'", res.Cart, res.Cut, res.SourceFile) - } else { - c.Sayln("Fileimport has failed (Cart/Cut %d/%d, Source: '%s'): %s", res.Cart, res.Cut, res.SourceFile, res.ErrorString) + c.Sayln("starting import from '%s'", ctx.SourceUri) + s.Run(0) + + currentStep := 0 + for { + select { + case <-c.Cancel: + s.Cancel() + case p := <-progresschan: + if currentStep != p.Step { + if currentStep > 0 { + c.Sayln("") + } + currentStep = p.Step + } + + if p.Cart > 0 { + c.Say("\r * %s: '%s':\t%6.2f%% (Cart/Cut: %d/%d)", p.StepName, p.Title, (p.Current/p.Total)*100, p.Cart, p.Cut) + } else { + c.Say("\r * %s '%s':\t%6.2f%%", p.StepName, p.Title, (p.Current/p.Total)*100) + } + case res := <-donechan: + if currentStep > 0 { + c.Sayln("") + } + if res.ResponseCode == http.StatusOK { + c.Sayln("File got succesfully imported into Cart/Cut %d/%d, Source: '%s'", res.Cart, res.Cut, res.SourceFile) + } else { + c.Sayln("Fileimport has failed (Cart/Cut %d/%d, Source: '%s'): %s", res.Cart, res.Cut, res.SourceFile, res.ErrorString) + } + return false } } - return false } func StartControlTelnet(addr string, conf *rhimport.Config, db *rddb.DB, sessions *rhimport.SessionStore) { cmdlist := make(telgo.CmdList) - cmdlist["quit"] = func(c *telgo.Client, args []string) bool { return telnetQuit(c, args, conf, db) } - cmdlist["help"] = func(c *telgo.Client, args []string) bool { return telnetHelp(c, args, conf, db) } - cmdlist["set"] = func(c *telgo.Client, args []string) bool { return telnetSet(c, args, conf, db) } - cmdlist["reset"] = func(c *telgo.Client, args []string) bool { return telnetReset(c, args, conf, db) } - cmdlist["show"] = func(c *telgo.Client, args []string) bool { return telnetShow(c, args, conf, db) } - cmdlist["run"] = func(c *telgo.Client, args []string) bool { return telnetRun(c, args, conf, db) } + cmdlist["quit"] = func(c *telgo.Client, args []string) bool { return telnetQuit(c, args, conf, db, sessions) } + cmdlist["help"] = func(c *telgo.Client, args []string) bool { return telnetHelp(c, args, conf, db, sessions) } + cmdlist["set"] = func(c *telgo.Client, args []string) bool { return telnetSet(c, args, conf, db, sessions) } + cmdlist["reset"] = func(c *telgo.Client, args []string) bool { return telnetReset(c, args, conf, db, sessions) } + cmdlist["show"] = func(c *telgo.Client, args []string) bool { return telnetShow(c, args, conf, db, sessions) } + cmdlist["run"] = func(c *telgo.Client, args []string) bool { return telnetRun(c, args, conf, db, sessions) } rhl.Println("telnet-ctrl: listening on", addr) s := telgo.NewServer(addr, "rhimportd> ", cmdlist, nil) |