summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-18 18:22:25 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-18 18:22:25 (GMT)
commit6b947ff616b31d44b601ad027f92f34faafaad9a (patch)
tree4744cfccb37cc1850092bcbc0b3a70e2c774f044
parent61c1a7078ba21c4e12bfbacb368ba7e87263248a (diff)
telnet client quit command is implemented
-rw-r--r--src/helsinki.at/rhimportd/ctrlTelnet.go65
1 files changed, 61 insertions, 4 deletions
diff --git a/src/helsinki.at/rhimportd/ctrlTelnet.go b/src/helsinki.at/rhimportd/ctrlTelnet.go
index bcad22c..1e0a00e 100644
--- a/src/helsinki.at/rhimportd/ctrlTelnet.go
+++ b/src/helsinki.at/rhimportd/ctrlTelnet.go
@@ -25,14 +25,70 @@
package main
import (
+ "bufio"
+ "fmt"
"helsinki.at/rhimport"
"net"
+ "strings"
)
-func client_handler(conn net.Conn) {
- defer conn.Close()
- rhdl.Println("telnet-ctrl: new client from:", conn.RemoteAddr())
+const (
+ prompt = "rhimportd> "
+)
+
+type TelnetClient struct {
+ conn net.Conn
+ scanner *bufio.Scanner
+ writer *bufio.Writer
+}
+
+func (c *TelnetClient) write_string(text string) (err error) {
+ if _, err = c.writer.WriteString(text); err != nil {
+ return
+ }
+ return c.writer.Flush()
+}
+
+func (c *TelnetClient) handle_command(cmd string) bool {
+ if cmd == "quit" {
+ return true
+ } else if strings.HasPrefix(cmd, "help") {
+ c.write_string(fmt.Sprintf("usage: tba...\n"))
+ } else {
+ c.write_string("unknown command\n")
+ }
+ return false
+}
+
+func (c *TelnetClient) handle() {
+ defer c.conn.Close()
+
+ if err := c.write_string(prompt); err != nil {
+ return
+ }
+ for c.scanner.Scan() {
+ if exit := c.handle_command(c.scanner.Text()); exit {
+ return
+ }
+
+ if err := c.write_string(prompt); err != nil {
+ return
+ }
+ }
+ if err := c.scanner.Err(); err != nil {
+ rhdl.Printf("telnet-ctrl(%s): read command error: %s", c.conn.RemoteAddr(), err)
+ } else {
+ rhdl.Printf("telnet-ctrl(%s): connection closed", c.conn.RemoteAddr())
+ }
+}
+func newTelnetClient(conn net.Conn) (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)
+ return c
}
func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDb) {
@@ -51,6 +107,7 @@ func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdD
return
}
- go client_handler(conn)
+ c := newTelnetClient(conn)
+ go c.handle()
}
}