2022-02-20 11:34:10 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-03-05 17:21:46 +01:00
|
|
|
"StoreBackEnd/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
|
2022-02-26 18:08:52 +01:00
|
|
|
Port int
|
2022-02-22 09:35:44 +01:00
|
|
|
ReqManager *managers.RequestManager
|
2022-02-20 11:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2022-02-20 11:34:10 +01:00
|
|
|
|
|
|
|
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-03-08 12:01:51 +01:00
|
|
|
print("connection ok")
|
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
|
2022-03-08 20:37:49 +01:00
|
|
|
reader := bufio.NewReader(con)
|
|
|
|
line, err := reader.ReadString('\n')
|
2022-03-08 17:44:10 +01:00
|
|
|
println("[REQUEST] " + line)
|
2022-02-22 08:29:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-08 20:37:49 +01:00
|
|
|
result := server.ReqManager.Execute(line, reader)
|
2022-03-08 17:44:10 +01:00
|
|
|
println("[RESPONSE] : ", result)
|
2022-03-05 17:21:46 +01:00
|
|
|
_, _ = con.Write(append([]byte(result), '\n')) // TODO : ATTENTION laisser les \n
|
2022-02-20 11:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|