Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: split conversation entry types #4

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,32 @@ func (c *Connection) asyncLoop() {
return
default:
}
switch entry.Type {
case EntryTypeInput:
switch entry := entry.(type) {
case ConversationEntryInput:
if err := c.processInputEntry(entry); err != nil {
panic(err.Error())
}
case EntryTypeOutput:
case ConversationEntryOutput:
if err := c.processOutputEntry(entry); err != nil {
panic(fmt.Sprintf("output error: %s", err))
}
case EntryTypeClose:
case ConversationEntryClose:
c.Close()
case EntryTypeSleep:
case ConversationEntrySleep:
time.Sleep(entry.Duration)
default:
panic(
fmt.Sprintf(
"unknown conversation entry type: %d: %#v",
entry.Type,
"unknown conversation entry type: %T: %#v",
entry,
entry,
),
)
}
}
}

func (c *Connection) processInputEntry(entry ConversationEntry) error {
func (c *Connection) processInputEntry(entry ConversationEntryInput) error {
// Wait for segment to be received from muxer
segment, ok := <-c.muxerRecvChan
if !ok {
Expand All @@ -193,7 +193,7 @@ func (c *Connection) processInputEntry(entry ConversationEntry) error {
if err != nil {
return fmt.Errorf("decode error: %s", err)
}
if entry.InputMessage != nil {
if entry.Message != nil {
// Create Message object from CBOR
msg, err := entry.MsgFromCborFunc(uint(msgType), segment.Payload)
if err != nil {
Expand All @@ -208,25 +208,25 @@ func (c *Connection) processInputEntry(entry ConversationEntry) error {
// As changing the CBOR of the expected message is not thread-safe, we instead clear the
// CBOR of the received message
msg.SetCbor(nil)
if !reflect.DeepEqual(msg, entry.InputMessage) {
if !reflect.DeepEqual(msg, entry.Message) {
return fmt.Errorf(
"parsed message does not match expected value: got %#v, expected %#v",
msg,
entry.InputMessage,
entry.Message,
)
}
} else {
if entry.InputMessageType == uint(msgType) {
if entry.MessageType == uint(msgType) {
return nil
}
return fmt.Errorf("input message is not of expected type: expected %d, got %d", entry.InputMessageType, msgType)
return fmt.Errorf("input message is not of expected type: expected %d, got %d", entry.MessageType, msgType)
}
return nil
}

func (c *Connection) processOutputEntry(entry ConversationEntry) error {
func (c *Connection) processOutputEntry(entry ConversationEntryOutput) error {
payloadBuf := bytes.NewBuffer(nil)
for _, msg := range entry.OutputMessages {
for _, msg := range entry.Messages {
// Get raw CBOR from message
data := msg.Cbor()
// If message has no raw CBOR, encode the message
Expand Down
77 changes: 41 additions & 36 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,51 @@ const (
MockKeepAliveCookie uint16 = 999
)

type EntryType int
type ConversationEntry interface {
isConversationEntry()
}

const (
EntryTypeNone EntryType = 0
EntryTypeInput EntryType = 1
EntryTypeOutput EntryType = 2
EntryTypeClose EntryType = 3
EntryTypeSleep EntryType = 4
)
type conversationEntryBase struct{}

func (c conversationEntryBase) isConversationEntry() {}

type ConversationEntryInput struct {
conversationEntryBase
ProtocolId uint16
IsResponse bool
Message protocol.Message
MessageType uint
MsgFromCborFunc protocol.MessageFromCborFunc
}

type ConversationEntry struct {
Type EntryType
ProtocolId uint16
IsResponse bool
OutputMessages []protocol.Message
InputMessage protocol.Message
InputMessageType uint
MsgFromCborFunc protocol.MessageFromCborFunc
Duration time.Duration
type ConversationEntryOutput struct {
conversationEntryBase
ProtocolId uint16
IsResponse bool
Messages []protocol.Message
}

type ConversationEntryClose struct {
conversationEntryBase
}

type ConversationEntrySleep struct {
conversationEntryBase
Duration time.Duration
}

// ConversationEntryHandshakeRequestGeneric is a pre-defined conversation event that matches a generic
// handshake request from a client
var ConversationEntryHandshakeRequestGeneric = ConversationEntry{
Type: EntryTypeInput,
ProtocolId: handshake.ProtocolId,
InputMessageType: handshake.MessageTypeProposeVersions,
var ConversationEntryHandshakeRequestGeneric = ConversationEntryInput{
ProtocolId: handshake.ProtocolId,
MessageType: handshake.MessageTypeProposeVersions,
}

// ConversationEntryHandshakeNtCResponse is a pre-defined conversation entry for a server NtC handshake response
var ConversationEntryHandshakeNtCResponse = ConversationEntry{
Type: EntryTypeOutput,
var ConversationEntryHandshakeNtCResponse = ConversationEntryOutput{
ProtocolId: handshake.ProtocolId,
IsResponse: true,
OutputMessages: []protocol.Message{
Messages: []protocol.Message{
handshake.NewMsgAcceptVersion(
MockProtocolVersionNtC,
protocol.VersionDataNtC9to14(MockNetworkMagic),
Expand All @@ -72,11 +82,10 @@ var ConversationEntryHandshakeNtCResponse = ConversationEntry{
}

// ConversationEntryHandshakeNtNResponse is a pre-defined conversation entry for a server NtN handshake response
var ConversationEntryHandshakeNtNResponse = ConversationEntry{
Type: EntryTypeOutput,
var ConversationEntryHandshakeNtNResponse = ConversationEntryOutput{
ProtocolId: handshake.ProtocolId,
IsResponse: true,
OutputMessages: []protocol.Message{
Messages: []protocol.Message{
handshake.NewMsgAcceptVersion(
MockProtocolVersionNtN,
protocol.VersionDataNtN13andUp{
Expand All @@ -92,19 +101,17 @@ var ConversationEntryHandshakeNtNResponse = ConversationEntry{
}

// ConversationEntryKeepAliveRequest is a pre-defined conversation entry for a keep-alive request
var ConversationEntryKeepAliveRequest = ConversationEntry{
Type: EntryTypeInput,
var ConversationEntryKeepAliveRequest = ConversationEntryInput{
ProtocolId: keepalive.ProtocolId,
InputMessage: keepalive.NewMsgKeepAlive(MockKeepAliveCookie),
Message: keepalive.NewMsgKeepAlive(MockKeepAliveCookie),
MsgFromCborFunc: keepalive.NewMsgFromCbor,
}

// ConversationEntryKeepAliveResponse is a pre-defined conversation entry for a keep-alive response
var ConversationEntryKeepAliveResponse = ConversationEntry{
Type: EntryTypeOutput,
var ConversationEntryKeepAliveResponse = ConversationEntryOutput{
ProtocolId: keepalive.ProtocolId,
IsResponse: true,
OutputMessages: []protocol.Message{
Messages: []protocol.Message{
keepalive.NewMsgKeepAliveResponse(MockKeepAliveCookie),
},
}
Expand All @@ -130,7 +137,5 @@ var ConversationKeepAliveClose = []ConversationEntry{
ConversationEntryHandshakeRequestGeneric,
ConversationEntryHandshakeNtNResponse,
ConversationEntryKeepAliveRequest,
ConversationEntry{
Type: EntryTypeClose,
},
ConversationEntryClose{},
}