more refactoring
parent
3566309c3d
commit
71ee479873
@ -1,62 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
|
||||
"github.com/ThreeDotsLabs/watermill/message/router/plugin"
|
||||
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
|
||||
)
|
||||
|
||||
type TELNETServer struct {
|
||||
l net.Listener
|
||||
}
|
||||
|
||||
func NewTELNETServer(l net.Listener) *TELNETServer {
|
||||
ts := TELNETServer{
|
||||
l: l,
|
||||
}
|
||||
|
||||
return &ts
|
||||
}
|
||||
|
||||
func (ts *TELNETServer) Run(ctx context.Context) {
|
||||
for {
|
||||
conn, err := ts.l.Accept()
|
||||
func main() {
|
||||
l, err := net.Listen("tcp", ":5555")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
wmLogger := watermill.NewStdLogger(false, false)
|
||||
router, err := message.NewRouter(message.RouterConfig{}, wmLogger)
|
||||
|
||||
go func(c net.Conn) {
|
||||
s := bufio.NewScanner(c)
|
||||
defer c.Close()
|
||||
router.AddPlugin(plugin.SignalsHandler)
|
||||
|
||||
for s.Scan() {
|
||||
t := s.Text()
|
||||
err := ts.Publish("raw_telnet", message.NewMessage(watermill.NewUUID(), []byte(t)))
|
||||
if err != nil {
|
||||
log.Fatalln("couldn't write to raw_telnet queue")
|
||||
}
|
||||
log.Printf("Received user input: %q\n", t)
|
||||
}
|
||||
}(conn)
|
||||
}
|
||||
}
|
||||
router.AddMiddleware(
|
||||
middleware.CorrelationID,
|
||||
middleware.Recoverer,
|
||||
)
|
||||
|
||||
func (ts *TELNETServer) Publish(topic string, messages ...*message.Message) error {
|
||||
pubSub := gochannel.NewGoChannel(gochannel.Config{}, wmLogger)
|
||||
|
||||
return nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
ts := NewTELNETServer(ctx, l, pubSub)
|
||||
|
||||
func main() {
|
||||
l, err := net.Listen("tcp", ":5555")
|
||||
for {
|
||||
conn, err := ts.l.Accept()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
ts := NewTELNETServer(l)
|
||||
ts.Run(context.Background())
|
||||
go ts.Handle(conn)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
type TELNETParser struct {
|
||||
}
|
||||
|
||||
type TELNETServer struct {
|
||||
l net.Listener
|
||||
p message.Publisher
|
||||
c context.Context
|
||||
}
|
||||
|
||||
func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher) *TELNETServer {
|
||||
ts := TELNETServer{
|
||||
c: c,
|
||||
l: l,
|
||||
p: p,
|
||||
}
|
||||
|
||||
return &ts
|
||||
}
|
||||
|
||||
func (ts *TELNETServer) Handle(conn net.Conn) {
|
||||
s := bufio.NewScanner(conn)
|
||||
defer conn.Close()
|
||||
|
||||
for s.Scan() {
|
||||
t := s.Text()
|
||||
err := ts.p.Publish("telnet.raw", message.NewMessage(watermill.NewUUID(), []byte(t)))
|
||||
if err != nil {
|
||||
log.Fatalln("couldn't write to raw_telnet queue")
|
||||
}
|
||||
log.Printf("Received user input: %q\n", t)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue