summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/rhctl/noptcp.go57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/rhctl/noptcp.go b/src/rhctl/noptcp.go
index 66495ec..6dc8165 100644
--- a/src/rhctl/noptcp.go
+++ b/src/rhctl/noptcp.go
@@ -25,14 +25,66 @@ import (
"net"
)
+const (
+ CLIENT_MAX_DATA_BYTES = 64 * 1024 // close connection if the client sends more then 64k of data
+)
+
type NopTCPInterface struct {
ln net.Listener
ctrl *SwitchControl
}
+type Close bool
+
+func readData(con net.Conn, ch chan<- interface{}) {
+ defer func() {
+ ch <- Close(false)
+ }()
+ bytesRead := 0
+ for {
+ var null [1024]byte
+ n, err := con.Read(null[:])
+ if err != nil {
+ return
+ }
+ bytesRead = bytesRead + n
+ if bytesRead > CLIENT_MAX_DATA_BYTES {
+ rhdl.Printf("NopTCP: client(%s) sent too many bytes", con.RemoteAddr())
+ return
+ }
+ }
+}
+
func handleClient(con net.Conn, ctrl *SwitchControl) {
- con.Write([]byte("hello world"))
- con.Close()
+ defer func() {
+ con.Close()
+ rhdl.Printf("NopTCP: client(%s) disconnected", con.RemoteAddr())
+ }()
+
+ var ch = ctrl.Updates.Sub()
+ defer ctrl.Updates.Unsub(ch)
+ go readData(con, ch)
+
+ ctrl.Updates.AddSub(ch, "state")
+ ctrl.Commands <- &Command{Type: CmdState, Response: ch}
+ for {
+ data, ok := <-ch
+ if !ok {
+ return
+ }
+ switch data.(type) {
+ case State:
+ state := data.(State)
+ if state.ActiveServer == "" {
+ state.ActiveServer = "none"
+ }
+ if _, err := con.Write([]byte(state.ActiveServer + "\n")); err != nil {
+ break
+ }
+ case Close:
+ return
+ }
+ }
}
func (nop *NopTCPInterface) Run() {
@@ -52,6 +104,7 @@ func (nop *NopTCPInterface) Run() {
return
}
}
+ rhdl.Printf("NopTCP: client(%s) connected", con.RemoteAddr())
go handleClient(con, nop.ctrl)
}
}