Browse Source

use type switching for different commands instead of type codes

Alan Shreve 12 years ago
parent
commit
1e85846418

+ 7 - 8
src/ngrok/client/main.go

@@ -171,7 +171,7 @@ func control(s *State, ctl *ui.Controller) {
 
 	if regAck.Error != "" {
 		emsg := fmt.Sprintf("Server failed to allocate tunnel: %s", regAck.Error)
-		ctl.Cmds <- ui.Command{ui.QUIT, emsg}
+		ctl.Cmds <- ui.CmdQuit{emsg}
 		return
 	}
 
@@ -244,15 +244,14 @@ func Main() {
 		defer ctl.Wait.Done()
 		for {
 			select {
-			case cmd := <-ctl.Cmds:
-				switch cmd.Code {
-				case ui.QUIT:
-					quitMessage = cmd.Payload.(string)
+			case obj := <-ctl.Cmds:
+				switch cmd := obj.(type) {
+				case ui.CmdQuit:
+					quitMessage = cmd.Message
 					ctl.DoShutdown()
 					return
-				case ui.REPLAY:
+				case ui.CmdRequest:
 					go func() {
-						payload := cmd.Payload.([]byte)
 						var localConn conn.Conn
 						localConn, err := conn.Dial(s.opts.localaddr, "prv")
 						if err != nil {
@@ -261,7 +260,7 @@ func Main() {
 						}
 						//defer localConn.Close()
 						localConn = s.protocol.WrapConn(localConn)
-						localConn.Write(payload)
+						localConn.Write(cmd.Payload)
 						ioutil.ReadAll(localConn)
 					}()
 				}

+ 9 - 7
src/ngrok/client/ui/command.go

@@ -1,11 +1,13 @@
 package ui
 
-type Command struct {
-	Code    int
-	Payload interface{}
+type Command interface{}
+
+type CmdQuit struct {
+    // display this message after quit
+    Message string
 }
 
-const (
-	QUIT = iota
-	REPLAY
-)
+type CmdRequest struct {
+    // the bytes of the request to issue
+    Payload []byte
+}

+ 1 - 1
src/ngrok/client/views/term/view.go

@@ -120,7 +120,7 @@ func (v *TermView) input() {
 			switch ev.Key {
 			case termbox.KeyCtrlC:
 				v.Info("Got quit command")
-				v.ctl.Cmds <- ui.Command{ui.QUIT, ""}
+				v.ctl.Cmds <- ui.CmdQuit{}
 			}
 
 		case termbox.EventResize:

+ 1 - 1
src/ngrok/client/views/web/http.go

@@ -81,7 +81,7 @@ func (h *WebHttpView) register() {
 			if err != nil {
 				panic(err)
 			}
-			h.ctl.Cmds <- ui.Command{ui.REPLAY, bodyBytes}
+			h.ctl.Cmds <- ui.CmdRequest{bodyBytes}
 			w.Write([]byte(http.StatusText(200)))
 		} else {
 			// XXX: 400