From 6c552b607e8c1a175ae26e90b9637836dc571ee9 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 11 Jan 2017 21:55:56 +0100 Subject: nop-tcp client seems to be working now 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) } } -- cgit v0.10.2