45 lines
973 B
Go
45 lines
973 B
Go
package network
|
|
|
|
import (
|
|
"StoreBackEnd/pkg/protocol/managers"
|
|
"bufio"
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
type ServerUnicast struct {
|
|
Network string
|
|
Port int
|
|
ReqManager *managers.RequestManager
|
|
}
|
|
|
|
func (server ServerUnicast) Run() {
|
|
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()
|
|
print("connection ok")
|
|
|
|
if err != nil {
|
|
fmt.Printf("Error while accepting client : %s\n", err)
|
|
return
|
|
} else {
|
|
for { // TODO : Extraire cette partie de code
|
|
reader := bufio.NewReader(con)
|
|
line, err := reader.ReadString('\n')
|
|
println("[REQUEST] " + line)
|
|
if err != nil {
|
|
return
|
|
}
|
|
result := server.ReqManager.Execute(line, reader)
|
|
println("[RESPONSE] : ", result)
|
|
_, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n
|
|
}
|
|
}
|
|
}
|