refactoring telnet code out of main()

main
Nick Dumas 1 year ago
parent e25f3a95f2
commit 3566309c3d

@ -0,0 +1,3 @@
package main
import ()

@ -2,31 +2,61 @@ package main
import (
"bufio"
"fmt"
"context"
"log"
"net"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
)
type MUDHandler struct {
type TELNETServer struct {
l net.Listener
}
func main() {
l, err := net.Listen("tcp", ":5555")
if err != nil {
log.Fatal(err)
func NewTELNETServer(l net.Listener) *TELNETServer {
ts := TELNETServer{
l: l,
}
defer l.Close()
return &ts
}
func (ts *TELNETServer) Run(ctx context.Context) {
for {
conn, err := l.Accept()
conn, err := ts.l.Accept()
if err != nil {
log.Fatal(err)
}
go func(c net.Conn) {
s := bufio.NewScanner(c)
defer c.Close()
for s.Scan() {
fmt.Printf("Received user input: %q\n", s.Text())
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)
}
}
func (ts *TELNETServer) Publish(topic string, messages ...*message.Message) error {
return nil
}
func main() {
l, err := net.Listen("tcp", ":5555")
if err != nil {
log.Fatal(err)
}
defer l.Close()
ts := NewTELNETServer(l)
ts.Run(context.Background())
}

@ -2,6 +2,11 @@ module code.ndumas.com/ndumas/gomud
go 1.19
require github.com/therealfakemoot/go-telnet v0.0.0-20230818185913-4f336ab6b975
require github.com/ThreeDotsLabs/watermill v1.3.3
require github.com/reiver/go-oi v1.0.0 // indirect
require (
github.com/google/uuid v1.3.0 // indirect
github.com/lithammer/shortuuid/v3 v3.0.7 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
)

@ -1,4 +1,17 @@
github.com/reiver/go-oi v1.0.0 h1:nvECWD7LF+vOs8leNGV/ww+F2iZKf3EYjYZ527turzM=
github.com/reiver/go-oi v1.0.0/go.mod h1:RrDBct90BAhoDTxB1fenZwfykqeGvhI6LsNfStJoEkI=
github.com/therealfakemoot/go-telnet v0.0.0-20230818185913-4f336ab6b975 h1:0ya9MAIQtQnj2zj4XA/lAkd/PHXCD1sIFh/vBMQxADM=
github.com/therealfakemoot/go-telnet v0.0.0-20230818185913-4f336ab6b975/go.mod h1:W73X7Jmde9dHeL8DTG7y3+F4fcPhyNt9HSLe/oyOX5U=
github.com/ThreeDotsLabs/watermill v1.3.3 h1:ulVgkk7n/hhWmqKKJrquWJWwIDGr9LXJ8W5XmwgSDJ4=
github.com/ThreeDotsLabs/watermill v1.3.3/go.mod h1:FUH1a4BEmr5UCmCtg7CIYvEL11mdeVBDp1404+eLP+c=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/lithammer/shortuuid/v3 v3.0.7 h1:trX0KTHy4Pbwo/6ia8fscyHoGA+mf1jWbPJVuvyJQQ8=
github.com/lithammer/shortuuid/v3 v3.0.7/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

Loading…
Cancel
Save