2022-02-20 11:34:10 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-02-22 09:35:44 +01:00
|
|
|
"_StorBackEnd/pkg/protocol/managers"
|
2022-02-20 11:34:10 +01:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerUnicast struct {
|
2022-02-20 11:55:11 +01:00
|
|
|
Network string
|
|
|
|
Address string
|
2022-02-22 09:35:44 +01:00
|
|
|
ReqManager *managers.RequestManager
|
2022-02-20 11:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server ServerUnicast) Run() {
|
|
|
|
|
|
|
|
listen, err := net.Listen(server.Network, server.Address) // "tcp", "0.0.0.0:58000"
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Can't start server : %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-20 11:41:41 +01:00
|
|
|
// Attente connexion du FileFrontEnd
|
|
|
|
con, err := listen.Accept()
|
2022-02-20 11:34:10 +01:00
|
|
|
|
2022-02-20 11:41:41 +01:00
|
|
|
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
|
|
|
|
line, err := bufio.NewReader(con).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result := server.ReqManager.Execute(line)
|
2022-02-22 12:23:01 +01:00
|
|
|
con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n
|
2022-02-20 11:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|