WIP: http server for client

This commit is contained in:
programmer10110 2020-08-06 19:23:05 +03:00
parent 0d155437b5
commit 21be664c73
2 changed files with 17 additions and 23 deletions

View File

@ -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()
}

View File

@ -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 {