From 21be664c7308d095db972768faf94fe8794bb03e Mon Sep 17 00:00:00 2001 From: programmer10110 Date: Thu, 6 Aug 2020 19:23:05 +0300 Subject: [PATCH] WIP: http server for client --- client/client.go | 26 ++++++++------------------ client/state.go | 14 +++++++++----- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/client/client.go b/client/client.go index 1a80004..951f515 100644 --- a/client/client.go +++ b/client/client.go @@ -74,17 +74,18 @@ func (c *Client) Poll() { panic(err) } - resp, _, err := c.fsm.Do(fsmReq.Event, fsmReq.Args...) + resp, fsmDump, err := c.fsm.Do(fsmReq.Event, fsmReq.Args...) if err != nil { panic(err) } - if err = c.processFSMResponse(resp); err != nil { - panic(err) - } + var operation *Operation - operation := &Operation{ - //Payload: fsmResponse.Data + if resp.IsOpRequired { + operation = &Operation{ + Type: OperationType(resp.State), + Payload: resp.Data, // TODO:marshall + } } // I.e., if FSM returned an Operation for us. @@ -98,7 +99,7 @@ func (c *Client) Poll() { panic(err) } - if err := c.state.SaveFSM(c.fsm); err != nil { + if err := c.state.SaveFSM(fsmDump); err != nil { panic(err) } } @@ -108,17 +109,6 @@ func (c *Client) Poll() { } } -func (c *Client) processFSMResponse(resp *fsm.Response) error { - switch resp.State { - case "smtth": - // Do smth - case "another_state": - //Do another thing - //..... - } - return nil -} - func (c *Client) GetOperations() (map[string]*Operation, error) { return c.state.GetOperations() } diff --git a/client/state.go b/client/state.go index cee4580..83ae8d8 100644 --- a/client/state.go +++ b/client/state.go @@ -13,14 +13,15 @@ import ( const ( offsetKey = "offset" operationsKey = "operations" + fsmStateKey = "fsm_state" ) type State interface { SaveOffset(uint64) error LoadOffset() (uint64, error) - SaveFSM(interface{}) error - LoadFSM() (interface{}, error) + SaveFSM([]byte) error + LoadFSM() ([]byte, error) PutOperation(operation *Operation) error DeleteOperation(operationID string) error @@ -87,13 +88,16 @@ func (s *LevelDBState) LoadOffset() (uint64, error) { } // TODO: implement. -func (s *LevelDBState) SaveFSM(interface{}) error { +func (s *LevelDBState) SaveFSM(fsmState []byte) error { + if err := s.stateDb.Put([]byte(fsmStateKey), fsmState, nil); err != nil { + return fmt.Errorf("failed to save fsm state: %w", err) + } return nil } // TODO: implement. -func (s *LevelDBState) LoadFSM() (interface{}, error) { - return nil, nil +func (s *LevelDBState) LoadFSM() ([]byte, error) { + return s.stateDb.Get([]byte(fsmStateKey), nil) } func (s *LevelDBState) PutOperation(operation *Operation) error {