diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlTelnet.go | 71 |
1 files changed, 64 insertions, 7 deletions
diff --git a/src/helsinki.at/rhimportd/ctrlTelnet.go b/src/helsinki.at/rhimportd/ctrlTelnet.go index 1e0a00e..4d60fe2 100644 --- a/src/helsinki.at/rhimportd/ctrlTelnet.go +++ b/src/helsinki.at/rhimportd/ctrlTelnet.go @@ -40,6 +40,9 @@ type TelnetClient struct { conn net.Conn scanner *bufio.Scanner writer *bufio.Writer + conf *rhimport.Config + rddb *rhimport.RdDb + ctx *rhimport.ImportContext } func (c *TelnetClient) write_string(text string) (err error) { @@ -49,13 +52,64 @@ func (c *TelnetClient) write_string(text string) (err error) { return c.writer.Flush() } -func (c *TelnetClient) handle_command(cmd string) bool { +func (c *TelnetClient) say(text string) (err error) { + return c.write_string(text + "\n") +} + +func (c *TelnetClient) handle_cmd_help(args []string) { + c.say(fmt.Sprintf("usage: tba...(args: %v)", args)) +} + +func (c *TelnetClient) handle_cmd_set(args []string) { + c.say(fmt.Sprintf("setting args: %v", args)) + if c.ctx == nil { + c.ctx = &rhimport.ImportContext{Config: c.conf, RdDb: c.rddb} + c.ctx.UserName = "hugo" + } +} + +func (c *TelnetClient) handle_cmd_clear(args []string) { + if len(args) >= 0 { + c.say("too many arguments") + } + c.ctx = nil +} + +func (c *TelnetClient) handle_cmd_show(args []string) { + c.say(fmt.Sprintf("%+v", c.ctx)) +} + +func (c *TelnetClient) handle_cmd_run(args []string) { + if c.ctx != nil { + c.say(fmt.Sprintf("running args: %v", args)) + c.ctx = nil + } else { + c.say(fmt.Sprintf("context is empty please set at least one option")) + } +} + +func (c *TelnetClient) handle_cmd(cmdstr string) bool { + cmdslice := strings.Fields(cmdstr) + if len(cmdslice) == 0 || cmdslice[0] == "" { + return false + } + cmd := cmdslice[0] + args := cmdslice[1:] + if cmd == "quit" { return true - } else if strings.HasPrefix(cmd, "help") { - c.write_string(fmt.Sprintf("usage: tba...\n")) + } else if cmd == "help" { + c.handle_cmd_help(args) + } else if cmd == "set" { + c.handle_cmd_set(args) + } else if cmd == "clear" { + c.handle_cmd_clear(args) + } else if cmd == "show" { + c.handle_cmd_show(args) + } else if cmd == "run" { + c.handle_cmd_run(args) } else { - c.write_string("unknown command\n") + c.say("unknown command") } return false } @@ -67,7 +121,7 @@ func (c *TelnetClient) handle() { return } for c.scanner.Scan() { - if exit := c.handle_command(c.scanner.Text()); exit { + if exit := c.handle_cmd(c.scanner.Text()); exit { return } @@ -82,12 +136,15 @@ func (c *TelnetClient) handle() { } } -func newTelnetClient(conn net.Conn) (c *TelnetClient) { +func newTelnetClient(conn net.Conn, conf *rhimport.Config, rddb *rhimport.RdDb) (c *TelnetClient) { rhl.Println("telnet-ctrl: new client from:", conn.RemoteAddr()) c = &TelnetClient{} c.conn = conn c.scanner = bufio.NewScanner(conn) c.writer = bufio.NewWriter(conn) + c.conf = conf + c.rddb = rddb + c.ctx = nil return c } @@ -107,7 +164,7 @@ func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdD return } - c := newTelnetClient(conn) + c := newTelnetClient(conn, conf, rddb) go c.handle() } } |