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

Support of TCP #26

Merged
merged 11 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
final rewording & nitpicks
  • Loading branch information
NoelM committed Feb 8, 2025
commit 96fcc67bf0d92b8ff67ced1e7bd56143a3eff063
34 changes: 17 additions & 17 deletions notel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@ func main() {
return
}

notelConfig, err := confs.LoadConfig(os.Args[1])
notelConf, err := confs.LoadConfig(os.Args[1])
if err != nil {
logs.ErrorLog("notel: unable to load conf: %s\n", err.Error())
return
}

CommuneDb = databases.NewCommuneDatabase()
CommuneDb.LoadCommuneDatabase(notelConfig.CommuneDbPath)
CommuneDb.LoadCommuneDatabase(notelConf.CommuneDbPath)

MessageDb = databases.NewMessageDatabase()
MessageDb.LoadMessages(notelConfig.MessagesDbPath)
MessageDb.LoadMessages(notelConf.MessagesDbPath)

UsersDb = databases.NewUsersDatabase()
UsersDb.LoadDatabase(notelConfig.UsersDbPath)
UsersDb.LoadDatabase(notelConf.UsersDbPath)

group.Add(1)
metrics := NewMetrics()
go serveMetrics(&group, metrics, notelConfig.Connectors)
go metricsServe(&group, metrics, notelConf.Connectors)

for _, connConfig := range notelConfig.Connectors {
if !connConfig.Active {
for _, connConf := range notelConf.Connectors {
if !connConf.Active {
continue
}

switch connConfig.Kind {
switch connConf.Kind {
case "modem":
group.Add(1)
go serveModem(&group, connConfig, metrics)
go modemServe(&group, connConf, metrics)

case "websocket":
group.Add(1)
go serveWebSocket(&group, connConfig, metrics)
go webSocketServe(&group, connConf, metrics)

case "tcp":
group.Add(1)
go serveTCP(&group, connConfig, metrics)
go tcpServe(&group, connConf, metrics)
}
}
group.Wait()
Expand All @@ -69,8 +69,8 @@ func main() {
UsersDb.Quit()
}

func NotelApplication(mntl *minigo.Minitel, wg *sync.WaitGroup, connConf *confs.ConnectorConf, metrics *Metrics) {
wg.Add(1)
func NotelApplication(minitel *minigo.Minitel, group *sync.WaitGroup, connConf *confs.ConnectorConf, metrics *Metrics) {
group.Add(1)

metrics.ConnCount.With(prometheus.Labels{"source": connConf.Tag}).Inc()

Expand All @@ -81,18 +81,18 @@ func NotelApplication(mntl *minigo.Minitel, wg *sync.WaitGroup, connConf *confs.
startConn := time.Now()

SIGNIN:
creds, op := NewPageSignIn(mntl).Run()
creds, op := NewPageSignIn(minitel).Run()

if op == minigo.GuideOp {
creds, op = NewSignUpPage(mntl).Run()
creds, op = NewSignUpPage(minitel).Run()

if op == minigo.SommaireOp {
goto SIGNIN
}
}

if op == minigo.EnvoiOp {
SommaireHandler(mntl, creds["login"], metrics)
SommaireHandler(minitel, creds["login"], metrics)
}

metrics.ConnDurationCount.With(prometheus.Labels{"source": connConf.Tag}).Add(time.Since(startConn).Seconds())
Expand All @@ -102,5 +102,5 @@ SIGNIN:

logs.InfoLog("[%s] notel-handler: quit handler, connected=%d\n", connConf.Tag, activeUsers)

wg.Done()
group.Done()
}
2 changes: 1 addition & 1 deletion notel/modems.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/NoelM/minigo/notel/logs"
)

func serveModem(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
func modemServe(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
defer wg.Done()

modem, err := minigo.NewModem(connConf.Path, 115200, connConf.Config, connConf.Tag, metrics.ConnAttemptCount)
Expand Down
2 changes: 1 addition & 1 deletion notel/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewMetrics() *Metrics {
}
}

func serveMetrics(wg *sync.WaitGroup, metrics *Metrics, connectors []confs.ConnectorConf) {
func metricsServe(wg *sync.WaitGroup, metrics *Metrics, connectors []confs.ConnectorConf) {
defer wg.Done()

for _, cv := range []*prometheus.CounterVec{metrics.ConnCount, metrics.ConnLostCount, metrics.ConnDurationCount, metrics.ConnAttemptCount} {
Expand Down
37 changes: 19 additions & 18 deletions notel/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,46 @@ import (
"github.com/NoelM/minigo/notel/logs"
)

func serveTCP(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
func tcpServe(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
defer wg.Done()

handler := func(conn net.Conn) {
tcp, _ := minigo.NewTCP(conn)
_ = tcp.Init()

number, err := waitForConnect(tcp)
number, err := getCallMetadata(tcp)
if err != nil {
logs.ErrorLog("[%s] serve-TCP: unable to connect %s\n", connConf.Tag, err)
logs.ErrorLog("[%s:<tel-number>] tcp-serve: unable to connect %s\n", connConf.Tag, err)
return
}
logs.InfoLog("[%s] serve-TCP: connect from number %s\n", connConf.Tag, number)

time.Sleep(7 * time.Second)

fullTag := fmt.Sprintf("%s:%s", connConf.Tag, number)
logs.InfoLog("[%s] tcp-serve: new call", fullTag)

// Sleep while the connection is fully established, 7s is a approx.
time.Sleep(7 * time.Second)

var innerWg sync.WaitGroup
var group sync.WaitGroup

network := minigo.NewNetwork(tcp, false, &innerWg, "TCP")
m := minigo.NewMinitel(network, false, &innerWg, fullTag, metrics.ConnLostCount)
m.NoCSI()
go m.Serve()
network := minigo.NewNetwork(tcp, false, &group, fullTag)
minitel := minigo.NewMinitel(network, false, &group, fullTag, metrics.ConnLostCount)
minitel.NoCSI()
go minitel.Serve()

NotelApplication(m, &innerWg, &connConf, metrics)
innerWg.Wait()
NotelApplication(minitel, &group, &connConf, metrics)
group.Wait()

logs.InfoLog("[%s] serve-TCP: disconnect\n", fullTag)
logs.InfoLog("[%s] tcp-serve: disconnect\n", fullTag)
tcp.Disconnect()

logs.InfoLog("[%s] serve-TCP: session closed\n", fullTag)
logs.InfoLog("[%s] tcp-serve: terminated\n", fullTag)
}

err := listenAndServeTCP(connConf.Path, handler)
err := tcpListenAndServe(connConf.Path, handler)
log.Fatal(err)
}

func listenAndServeTCP(path string, handler func(net.Conn)) error {
func tcpListenAndServe(path string, handler func(net.Conn)) error {

listener, err := net.Listen("tcp", path)
if err != nil {
Expand All @@ -69,7 +70,7 @@ func listenAndServeTCP(path string, handler func(net.Conn)) error {
}
}

func waitForConnect(tcp *minigo.TCP) (string, error) {
func getCallMetadata(tcp *minigo.TCP) (string, error) {
/*
CALLFROM 0173742367
STARTURL
Expand Down
2 changes: 1 addition & 1 deletion notel/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"nhooyr.io/websocket"
)

func serveWebSocket(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
func webSocketServe(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
defer wg.Done()

fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 2 additions & 3 deletions tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
)

type TCP struct {
conn net.Conn

conn net.Conn
connected bool
}

Expand All @@ -35,7 +34,7 @@ func (t *TCP) Write(b []byte) error {
}

func (t *TCP) Read() ([]byte, error) {
msg := make([]byte, 256)
msg := make([]byte, 64)
t.conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
n, err := t.conn.Read(msg)

Expand Down