2022-08-03 12:38:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// client simulator
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-20 17:59:37 +00:00
|
|
|
"bytes"
|
2022-08-04 08:16:41 +00:00
|
|
|
"context"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
2022-11-21 07:26:50 +00:00
|
|
|
"io"
|
2022-08-03 12:38:36 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/justincpresley/go-cobs"
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-11-20 08:13:11 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
|
2022-11-20 08:13:11 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
var cobsConfig = cobs.Config{SpecialByte: protocol.CobsDelimiter, Delimiter: true, EndingSave: true}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
type protocolState int8
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
const (
|
|
|
|
cmdAnnounce protocolState = iota
|
|
|
|
cmdData
|
|
|
|
respAnnounce
|
|
|
|
respData
|
|
|
|
)
|
2022-08-04 08:16:41 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
var validTransitions = map[protocolState]protocolState{
|
|
|
|
cmdAnnounce: cmdData,
|
|
|
|
cmdData: respAnnounce,
|
|
|
|
respAnnounce: respData,
|
|
|
|
respData: cmdAnnounce,
|
|
|
|
}
|
|
|
|
|
|
|
|
var protocolStateNames = map[protocolState]string{
|
|
|
|
cmdAnnounce: "CMD ANNOUNCE",
|
|
|
|
cmdData: "CMD DATA",
|
|
|
|
respAnnounce: "RESP ANNOUNCE",
|
|
|
|
respData: "RESP DATA",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p protocolState) String() string {
|
|
|
|
if name, ok := protocolStateNames[p]; ok {
|
|
|
|
return name
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return fmt.Sprintf("unknown %d", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestCommandGenerator struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
currentCommand *protocol.Command
|
|
|
|
currentResponse *protocol.Response
|
|
|
|
commands chan *protocol.Command
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *TestCommandGenerator) CmdAnnouncement() ([]byte, error) {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
g.currentCommand = <-g.commands
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
announceData, err := msgpack.Marshal(g.currentCommand.Announce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not marshal command annoucement: %w", err)
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-11-28 16:10:46 +00:00
|
|
|
|
|
|
|
g.logger.WithField("announcement", &g.currentCommand.Announce).Info("write command announcement")
|
|
|
|
|
|
|
|
return announceData, nil
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *TestCommandGenerator) CmdData() ([]byte, error) {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
|
|
|
|
cmdData, err := msgpack.Marshal(g.currentCommand.Command)
|
2022-08-04 08:16:41 +00:00
|
|
|
if err != nil {
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil, fmt.Errorf("could not marshal command data: %w", err)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
g.logger.WithField("command", &g.currentCommand.Command).Info("write command data")
|
|
|
|
|
|
|
|
return cmdData, nil
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (g *TestCommandGenerator) HandleResponseAnnounce(frame []byte) error {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
|
|
|
|
var ann messages.ResponseAnnounce
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(frame, &ann); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal response announcement")
|
|
|
|
}
|
|
|
|
|
|
|
|
g.logger.WithField("announcement", &ann).Info("received response announcement")
|
|
|
|
|
|
|
|
g.currentResponse = &protocol.Response{Announce: &ann}
|
|
|
|
|
|
|
|
return nil
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (g *TestCommandGenerator) HandleResponse(frame []byte) error {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
|
|
|
|
switch g.currentResponse.Announce.Code {
|
|
|
|
case messages.RespHealth:
|
|
|
|
var response messages.HealthResponse
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(frame, &response); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
g.currentResponse.Response = &response
|
2022-11-21 07:26:50 +00:00
|
|
|
case messages.RespFetchCRL:
|
|
|
|
var response messages.FetchCRLResponse
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(frame, &response); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal failed: %w", err)
|
|
|
|
}
|
2022-11-28 16:10:46 +00:00
|
|
|
|
|
|
|
g.currentResponse.Response = &response
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-08-04 08:16:41 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.logger.WithField(
|
|
|
|
"command",
|
|
|
|
g.currentCommand,
|
|
|
|
).WithField(
|
|
|
|
"response",
|
|
|
|
g.currentResponse,
|
|
|
|
).Info("handled health response")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *TestCommandGenerator) GenerateCommands(ctx context.Context) error {
|
2022-11-28 16:10:46 +00:00
|
|
|
const (
|
|
|
|
healthInterval = 5 * time.Second
|
|
|
|
startPause = 3 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
announce *messages.CommandAnnounce
|
|
|
|
err error
|
|
|
|
)
|
2022-08-04 08:16:41 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.logger.Info("start generating commands")
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
time.Sleep(startPause)
|
|
|
|
|
|
|
|
announce, err = messages.BuildCommandAnnounce(messages.CmdFetchCRL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("build command announce failed: %w", err)
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.commands <- &protocol.Command{
|
2022-11-28 16:10:46 +00:00
|
|
|
Announce: announce,
|
2022-11-20 17:59:37 +00:00
|
|
|
Command: &messages.FetchCRLCommand{IssuerID: "sub-ecc_person_2022"},
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
timer := time.NewTimer(healthInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
_ = timer.Stop()
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.logger.Info("stopping health check loop")
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
2022-11-28 16:10:46 +00:00
|
|
|
announce, err = messages.BuildCommandAnnounce(messages.CmdHealth)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("build command announce failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.commands <- &protocol.Command{
|
2022-11-28 16:10:46 +00:00
|
|
|
Announce: announce,
|
2022-11-20 17:59:37 +00:00
|
|
|
Command: &messages.HealthCommand{},
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
timer.Reset(healthInterval)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
type clientSimulator struct {
|
|
|
|
protocolState protocolState
|
|
|
|
logger *logrus.Logger
|
|
|
|
lock sync.Mutex
|
|
|
|
framesIn chan []byte
|
|
|
|
commandGenerator *TestCommandGenerator
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) readFrames() error {
|
|
|
|
const readInterval = 50 * time.Millisecond
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
var frame []byte
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
buffer := &bytes.Buffer{}
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
delimiter := []byte{cobsConfig.SpecialByte}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
for {
|
|
|
|
readBytes, err := c.readFromStdin()
|
|
|
|
if err != nil {
|
|
|
|
c.logger.WithError(err).Error("stdin read error")
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
close(c.framesIn)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if len(readBytes) == 0 {
|
|
|
|
time.Sleep(readInterval)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.logger.Tracef("read %d bytes", len(readBytes))
|
|
|
|
|
|
|
|
buffer.Write(readBytes)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
rest := buffer.Bytes()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if !bytes.Contains(rest, delimiter) {
|
2022-11-28 16:10:46 +00:00
|
|
|
c.logger.Tracef("read data does not contain the delimiter %x", delimiter)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
for bytes.Contains(rest, delimiter) {
|
|
|
|
parts := bytes.SplitAfterN(rest, delimiter, 2)
|
|
|
|
frame, rest = parts[0], parts[1]
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.logger.Tracef("frame of length %d", len(frame))
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if len(frame) == 0 {
|
|
|
|
continue
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
err = cobs.Verify(frame, cobsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("frame verification failed: %w", err)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
decoded := cobs.Decode(frame, cobsConfig)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.logger.Tracef("frame decoded to length %d", len(decoded))
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.framesIn <- decoded
|
2022-11-28 16:10:46 +00:00
|
|
|
|
|
|
|
c.logger.Tracef("%d bytes remaining", len(rest))
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
buffer.Truncate(0)
|
|
|
|
buffer.Write(rest)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) writeFrame(frame []byte) error {
|
|
|
|
encoded := cobs.Encode(frame, cobsConfig)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if _, err := io.Copy(os.Stdout, bytes.NewBuffer(encoded)); err != nil {
|
|
|
|
return fmt.Errorf("could not write data: %w", err)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
return nil
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) readFromStdin() ([]byte, error) {
|
|
|
|
const bufferSize = 1024
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
buf := make([]byte, bufferSize)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
c.logger.Trace("waiting for input")
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
count, err := os.Stdin.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading input failed: %w", err)
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
c.logger.Tracef("read %d bytes from stdin", count)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return buf[:count], nil
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) writeCmdAnnouncement() error {
|
|
|
|
frame, err := c.commandGenerator.CmdAnnouncement()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get command annoucement: %w", err)
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.logger.Trace("writing command announcement")
|
|
|
|
|
|
|
|
if err := c.writeFrame(frame); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if err := c.nextState(); err != nil {
|
|
|
|
return err
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) writeCommand() error {
|
|
|
|
frame, err := c.commandGenerator.CmdData()
|
2022-11-20 17:59:37 +00:00
|
|
|
if err != nil {
|
2022-11-21 07:26:50 +00:00
|
|
|
return fmt.Errorf("could not get command data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Trace("writing command data")
|
|
|
|
|
|
|
|
if err := c.writeFrame(frame); err != nil {
|
|
|
|
return err
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if err := c.nextState(); err != nil {
|
|
|
|
return err
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) handleResponseAnnounce() error {
|
|
|
|
c.logger.Trace("waiting for response announce")
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
frame := <-c.framesIn
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if frame == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if err := c.commandGenerator.HandleResponseAnnounce(frame); err != nil {
|
|
|
|
return fmt.Errorf("response announce handling failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.nextState(); err != nil {
|
|
|
|
return err
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientSimulator) handleResponseData() error {
|
|
|
|
c.logger.Trace("waiting for response data")
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
frame := <-c.framesIn
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if frame == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if err := c.commandGenerator.HandleResponse(frame); err != nil {
|
|
|
|
return fmt.Errorf("response handler failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.nextState(); err != nil {
|
|
|
|
return err
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientSimulator) Run(ctx context.Context) error {
|
|
|
|
c.protocolState = cmdAnnounce
|
|
|
|
errors := make(chan error)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
go func() {
|
|
|
|
err := c.readFrames()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
errors <- err
|
2022-11-20 17:59:37 +00:00
|
|
|
}()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
go func() {
|
2022-11-21 07:26:50 +00:00
|
|
|
err := c.commandGenerator.GenerateCommands(ctx)
|
|
|
|
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case err := <-errors:
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from handler loop: %w", err)
|
|
|
|
}
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if err := c.handleProtocolState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) handleProtocolState() error {
|
|
|
|
c.logger.Tracef("handling protocol state %s", c.protocolState)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
switch c.protocolState {
|
|
|
|
case cmdAnnounce:
|
|
|
|
if err := c.writeCmdAnnouncement(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case cmdData:
|
|
|
|
if err := c.writeCommand(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case respAnnounce:
|
|
|
|
if err := c.handleResponseAnnounce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case respData:
|
|
|
|
if err := c.handleResponseData(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown protocol state %s", c.protocolState)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (c *clientSimulator) nextState() error {
|
|
|
|
next, ok := validTransitions[c.protocolState]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("illegal protocol state %s", c.protocolState)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
c.protocolState = next
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.SetOutput(os.Stderr)
|
2022-11-28 16:10:46 +00:00
|
|
|
logger.SetLevel(logrus.DebugLevel)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
|
|
|
|
sim := &clientSimulator{
|
|
|
|
commandGenerator: &TestCommandGenerator{
|
|
|
|
logger: logger,
|
2022-11-28 16:10:46 +00:00
|
|
|
commands: make(chan *protocol.Command),
|
2022-11-21 07:26:50 +00:00
|
|
|
},
|
|
|
|
logger: logger,
|
2022-11-28 16:10:46 +00:00
|
|
|
framesIn: make(chan []byte),
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := sim.Run(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("simulator returned an error")
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|