StoreBackEnd/pkg/network/ServerUnicast.go

58 lines
1.1 KiB
Go
Raw Permalink Normal View History

package network
import (
"StoreBackEnd/pkg/protocol/managers"
"bufio"
"fmt"
"net"
)
type ServerUnicast struct {
Network string
2022-02-26 18:08:52 +01:00
Port int
ReqManager *managers.RequestManager
}
func (server ServerUnicast) Run() {
2022-02-26 18:08:52 +01:00
listen, err := net.Listen(server.Network, fmt.Sprintf("0.0.0.0:%d", server.Port)) // "tcp", "0.0.0.0:58000"
if err != nil {
fmt.Printf("Can't start server : %s\n", err)
return
}
// Attente connexion du FileFrontEnd
con, err := listen.Accept()
2022-03-20 15:32:45 +01:00
print("Connecté au FFE")
if err != nil {
fmt.Printf("Error while accepting client : %s\n", err)
return
} else {
2022-02-22 08:29:11 +01:00
for { // TODO : Extraire cette partie de code
reader := bufio.NewReader(con)
line, err := reader.ReadString('\n')
println("[REQUEST] " + line)
2022-02-22 08:29:11 +01:00
if err != nil {
return
}
response := server.ReqManager.Execute(line, reader)
if response == nil {
break
} else {
println("[RESPONSE] : ", response.Cmd)
_, _ = con.Write([]byte(response.Cmd)) // TODO : ATTENTION laisser les \n
if response.Write != nil {
response.Write(bufio.NewWriter(con))
}
}
}
con.Close()
}
listen.Close()
}