This commit is contained in:
crusader 2018-09-18 11:51:40 +09:00
parent 8bc927cf91
commit 9f798ea115
3 changed files with 140 additions and 1 deletions

8
Gopkg.lock generated
View File

@ -13,6 +13,12 @@
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
name = "github.com/satori/go.uuid"
packages = ["."]
revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3"
version = "v1.2.0"
[[projects]]
name = "github.com/stretchr/testify"
packages = ["assert"]
@ -28,6 +34,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "ec738887f78fd05112bc2a2f3b8a899e39749ad1dd11668f687f3ccebe1f3cf6"
inputs-digest = "42705304701176173d344446cd1f46bfef849ef7bc78c789b6a70526b132567a"
solver-name = "gps-cdcl"
solver-version = 1

96
echo/echo.go Normal file
View File

@ -0,0 +1,96 @@
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() string {
return "ECHO"
}
func (m *EchoMatcher) Type() 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
}

37
echo/echo_test.go Normal file
View File

@ -0,0 +1,37 @@
package echo
import (
"net"
"testing"
osm "git.loafle.net/overflow/service_matcher-go"
)
func TestEchoMatcher(t *testing.T) {
m := NewMatcher()
conn, err := net.Dial("tcp", "192.168.1.99:7")
if err != nil {
t.Error(err)
return
}
defer conn.Close()
matchCtx := osm.NewMatchCtx("192.168.1.99", 7)
for i := 0; i < m.PacketCount(matchCtx); i++ {
_, err := conn.Write(m.Packet(matchCtx, i).Buffer)
if err != nil {
t.Error(err)
}
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := osm.NewPacket(bytes, n)
if err := m.Match(matchCtx, i, p); err != nil {
t.Error(err)
}
}
t.Log(m.Name(matchCtx))
t.Log(matchCtx)
}