diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-31 11:50:38 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-31 11:50:38 +0200 |
| commit | 0d860b20a8b6b61d937124ee1955074b12c3f8e6 (patch) | |
| tree | 506c74a1709fcf648d6850eb9486257e70ce1e5a /src/state.go | |
| parent | 6258542936e54074784cbc1bf910bd0503312d39 (diff) | |
Initial approach to creating a fsm with states and substates
Diffstat (limited to 'src/state.go')
| -rw-r--r-- | src/state.go | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/state.go b/src/state.go index a48d13d..aa31513 100644 --- a/src/state.go +++ b/src/state.go @@ -1,5 +1,9 @@ package eduvpn +import ( + "errors" +) + type VPNState struct { // Info passed by the client ConfigDirectory string `json:"-"` @@ -14,34 +18,46 @@ type VPNState struct { // The file we keep open for logging LogFile *FileLogger `json:"-"` + + FSM *FSM `json:"-"` } func (state *VPNState) Register(name string, directory string, stateCallback func(string, string, string)) error { + if state.FSM == nil { + state.InitializeFSM() + } + if !state.HasTransition(APP_REGISTERED) { + return errors.New("app already registered") + } state.Name = name state.ConfigDirectory = directory state.StateCallback = stateCallback // Initialize the logger - state.InitLog(LOG_WARNING) - - state.Log(LOG_INFO, "App registered") + // state.InitLog(LOG_WARNING) - state.StateCallback("Start", "Registered", "app registered") + // state.Log(LOG_INFO, "App registered") // Try to load the previous configuration if state.LoadConfig() != nil { // This error can be safely ignored, as when the config does not load, the struct will not be filled - state.Log(LOG_INFO, "Previous configuration not found") + // state.Log(LOG_INFO, "Previous configuration not found") } + state.GoTransition(APP_REGISTERED, "HALLO") return nil } -func (state *VPNState) Deregister() { +func (state *VPNState) Deregister() error { + if !state.HasTransition(APP_DEREGISTERED) { + return errors.New("app cannot deregister") + } // Close the log file state.CloseLog() // Re-initialize everything state = &VPNState{} + state.GoTransition(APP_DEREGISTERED, "") + return nil } func (state *VPNState) Connect(url string) (string, error) { @@ -62,7 +78,17 @@ func (state *VPNState) Connect(url string) (string, error) { } } - return state.Server.GetConfig() + config, configErr := state.Server.GetConfig() + + if configErr != nil { + return "", configErr + } + + if !state.HasTransition(SERVER_CONNECTED) { + return "", errors.New("cannot connect to server, invalid state") + } + + return config, nil } var VPNStateInstance *VPNState |
