service_matcher-go/echo/echo.go
crusader bce329e44e ing
2018-10-23 13:31:25 +09:00

97 lines
1.8 KiB
Go

package echo
import (
"fmt"
osm "git.loafle.net/overflow/service_matcher-go"
uuid "github.com/satori/go.uuid"
)
const (
ECHO_COUNT = 2
)
type EchoMatcher struct {
osm.Matchers
}
func (m *EchoMatcher) Key(matchCtx *osm.MatchCtx) string {
return "ECHO"
}
func (m *EchoMatcher) Type(matchCtx *osm.MatchCtx) string {
return "NETWORK"
}
func (m *EchoMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *EchoMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *EchoMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *EchoMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *EchoMatcher) Name(matchCtx *osm.MatchCtx) string {
return "ECHO"
}
func (m *EchoMatcher) IsPrePacket() bool {
return false
}
func (m *EchoMatcher) PacketCount(matchCtx *osm.MatchCtx) int {
return ECHO_COUNT
}
func (m *EchoMatcher) Packet(matchCtx *osm.MatchCtx, index int) *osm.Packet {
reqStr := uuid.NewV4().String()
rbyte := make([]byte, len(reqStr))
copy(rbyte[:], reqStr)
matchCtx.SetAttribute(fmt.Sprintf("%d", index), reqStr)
return osm.NewPacket(rbyte, len(reqStr))
}
func (m *EchoMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return ECHO_COUNT-1 > index
}
func (m *EchoMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *EchoMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
sendStr, ok := matchCtx.GetAttribute(fmt.Sprintf("%d", index))
if !ok {
return osm.NotMatchedError()
}
recvStr := string(packet.Bytes())
if sendStr != recvStr {
return osm.NotMatchedError()
}
return nil
}
func NewMatcher() osm.Matcher {
m := &EchoMatcher{}
return m
}