From 289c319ea930df1fef06b68c7c5fc5240b318edb Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 2 Jun 2019 06:21:12 -0700 Subject: [PATCH] more code restructuring Signed-off-by: Jeff Carr --- gorilla.go | 17 +++--- accountPage.go => gui-accountPage.go | 0 mouseClick.go => gui-buttonClicks.go | 4 +- splash.go => gui-splashPage.go | 13 +++++ vm.go => gui-vmPage.go | 0 login.go | 7 ++- main.go | 78 +--------------------------- 7 files changed, 31 insertions(+), 88 deletions(-) rename accountPage.go => gui-accountPage.go (100%) rename mouseClick.go => gui-buttonClicks.go (98%) rename splash.go => gui-splashPage.go (89%) rename vm.go => gui-vmPage.go (100%) diff --git a/gorilla.go b/gorilla.go index f16f9d8..f23e391 100644 --- a/gorilla.go +++ b/gorilla.go @@ -41,30 +41,29 @@ func gorillaReadProtobuf(conn *websocket.Conn) { } } -func gorillaSendProtobuf(msg *pb.Event) { - log.Println("gorillaSendProtobuf() Event_Type =", msg.Type, "Event.Account.Nick =", msg.Account) +func websocketSendProtobuf(msg *pb.Event) { + log.Println("websocketSendProtobuf() Event_Type =", msg.Type, "Event.Account.Nick =", msg.Account) if (gorillaConn == nil) { - log.Println("gorillaSendProtobuf() CAN NOT SEND (socket connection isn't open)") + log.Println("websocketSendProtobuf() CAN NOT SEND (socket connection isn't open)") return } test, err := proto.Marshal(msg) if err != nil { - log.Println("gorillaConn.WriteMessage() Marshal failed SERIOUS ERROR", err) - log.Println("gorillaConn.WriteMessage() Marshal failed SERIOUS ERROR", err) - log.Println("gorillaConn.WriteMessage() Marshal failed SERIOUS ERROR", err) - // TODO: return here? + log.Println("websocketSendProtobuf() Marshal failed SERIOUS ERROR", err) + log.Println("websocketSendProtobuf() Marshal failed SERIOUS ERROR", err) + log.Println("websocketSendProtobuf() Marshal failed SERIOUS ERROR", err) return } err2 := gorillaConn.WriteMessage(websocket.BinaryMessage, test) if err2 != nil { - log.Println("gorillaConn.WriteMessage() failed write:", err2) + log.Println("websocketSendProtobuf() WriteMessage() failed write:", err2) gorillaConn = nil return } - log.Println("gorillaSendProtobuf() END OK", time.Now()) + log.Println("websocketSendProtobuf() END OK", time.Now()) } func closeGorillaConn() { diff --git a/accountPage.go b/gui-accountPage.go similarity index 100% rename from accountPage.go rename to gui-accountPage.go diff --git a/mouseClick.go b/gui-buttonClicks.go similarity index 98% rename from mouseClick.go rename to gui-buttonClicks.go index d2a6c12..31f5d03 100644 --- a/mouseClick.go +++ b/gui-buttonClicks.go @@ -61,7 +61,7 @@ func mainMouseClick(b *gui.GuiButton) { event.Account = b.Account log.Println("\tTRIGGERING CREATE event=", event) log.Println("\tTRIGGERING CREATE event.Account=", event.Account) - prepareAndSend(event) + websocketSendProtobuf(event) } else if (b.Action == "CONFIG") { newConfig := loadDefaultConfig() config.Accounts = newConfig.Accounts @@ -89,7 +89,7 @@ func mainMouseClick(b *gui.GuiButton) { event := pb.MakeGetEvent() event.Account = b.Account - prepareAndSend(event) + websocketSendProtobuf(event) count := 0 for { diff --git a/splash.go b/gui-splashPage.go similarity index 89% rename from splash.go rename to gui-splashPage.go index eb27ebb..f640ed7 100644 --- a/splash.go +++ b/gui-splashPage.go @@ -61,6 +61,19 @@ func getNEWTEXT() *ui.AttributedString { return aText } +func splashClick(b *gui.GuiButton) { + gw := b.Box.Window + // if there is already an account, skip straight to the main screen + for key, _ := range config.Accounts { + log.Println("gui.State = splash BUT THERE IS AN ACCOUNT Account = ", config.Accounts[key]) + makeCloudInfoBox(gw) + return + } + // if there is not an account, then go to 'make account' + gw.MakeWindow = addSubdomainQuestionBox + gw = gui.ShowTab(gw, "Box2", "New Account?") +} + func addSubdomainQuestionBox(gw *gui.GuiWindow) *gui.GuiBox { log.Println("addSubdomainQuestionBox() START") box := gui.AddGenericBox(gw) diff --git a/vm.go b/gui-vmPage.go similarity index 100% rename from vm.go rename to gui-vmPage.go diff --git a/login.go b/login.go index 0e82776..66dc187 100644 --- a/login.go +++ b/login.go @@ -9,14 +9,19 @@ import pb "git.wit.com/wit/witProtobuf" // import "github.com/davecgh/go-spew/spew" +// This sends a login protocol buffer and waits for a response + func login(b *gui.GuiButton) { log.Println("login() START") gui.Data.State = "SEND LOGIN" // TODO: move this into a seperate goroutine + // note: this may never be possible because of cross platform + // andlabs/ui requirements. windows and macos seem to freak out + // when you try to trigger the UI in seperate threads event := pb.MakeLoginEvent() event.Account = b.Account - prepareAndSend(event) + websocketSendProtobuf(event) count := 0 for { diff --git a/main.go b/main.go index a3faf05..7b8af65 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,13 @@ import "log" import "os" import "time" import "os/user" -import "reflect" +// import "reflect" // this is the king of dns libraries import "github.com/miekg/dns" import "git.wit.com/wit/gui" -import pb "git.wit.com/wit/witProtobuf" +// import pb "git.wit.com/wit/witProtobuf" import "git.wit.com/jcarr/dnssecsocket" import "github.com/gobuffalo/packr" @@ -154,77 +154,3 @@ func r() { } } } - -func prepareAndSend(event *pb.Event) { - if (event.Account == nil) { - log.Println("\tmain.prepareAndSend() ERROR event.Token = nil") - log.Println("\tmain.prepareAndSend() ERROR event.Token = nil") - log.Println("\tmain.prepareAndSend() ERROR event.Token = nil") - } else { - s := event.Account.Token - log.Println("\tmain.prepareAndSend() event.Token =", s) // s[len(s)-24:]) // this can panic because strings.() is stupid - } - gorillaSendProtobuf(event) - gui.Data.State = "READ PROTOBUF" -} - -func splashClick(b *gui.GuiButton) { - gw := b.Box.Window - // if there is already an account, skip straight to the main screen - for key, _ := range config.Accounts { - log.Println("gui.State = splash BUT THERE IS AN ACCOUNT Account = ", config.Accounts[key]) - makeCloudInfoBox(gw) - return - } - // if there is not an account, then go to 'make account' - gw.MakeWindow = addSubdomainQuestionBox - gw = gui.ShowTab(gw, "Box2", "New Account?") -} - -// this watches the GUI primarily to process protobuf's -// maybe this is pointless or wrong -func watchGUI() { - count := 0 - - for { - if (count > 10) { - log.Println("Sleep() in watchGUI() gui.Data.State =", gui.Data.State) - for i, window := range gui.Data.Windows { - log.Println("watchGUI() gui.Data.Windows i =", i, "Action =", window.Action) - for name, abox := range window.BoxMap { - log.Println("\twatchGUI() BOX name =", name) - if (name == "SplashArea3") { - log.Println("\twatchGUI() BOX abox =", reflect.TypeOf(abox)) - win := abox.Window - log.Println("\twatchGUI() BOX win =", reflect.TypeOf(win)) - area := win.Area - log.Println("\twatchGUI() BOX area =", reflect.TypeOf(area)) - spew.Dump(area.UiArea) - area.UiArea.Show() - time.Sleep(20000 * time.Millisecond) - os.Exit(0) - } - } - } - count = 0 - } - count += 1 - time.Sleep(200 * time.Millisecond) - - if (gui.Data.State == "kill") { - log.Println("gui.State = kill") - log.Println("gui.State = kill") - log.Println("gui.State = kill") - onExit(nil) - } else if (gui.Data.State == "HIDE") { - time.Sleep(20 * time.Millisecond) // maybe required for macos & windows refresh to work? - // gui.Data.Windows[0].Box1.Show() - // gui.Data.Windows[0].Box2.Show() - for key, element := range gui.Data.Windows { - log.Println("watchGUI() gui.Data.Windows =", key, element.Action) - element.UiTab.Show() - } - gui.Data.State = "done" - } - } -}