diff options
author | Christian Pointner <equinox@helsinki.at> | 2017-01-11 20:55:56 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2017-01-11 20:55:56 (GMT) |
commit | 6c552b607e8c1a175ae26e90b9637836dc571ee9 (patch) | |
tree | 525f29ca4da68c359a3395b80742f83eabf96037 /src/rhctl/noptcp.go | |
parent | 8d49e38e991f8be59ed9290f433d86944c617f16 (diff) |
nop-tcp client seems to be working now
Diffstat (limited to 'src/rhctl/noptcp.go')
-rw-r--r-- | src/rhctl/noptcp.go | 57 |
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) } } |