From c8de92d99ba4e8f307d92bf1087076934a8bf233 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Feb 2022 08:29:11 +0100 Subject: [PATCH] Start commit 22/02/2022 --- cmd/main.go | 9 +++++++-- pkg/network/ClientMulticast.go | 11 ++++++++--- pkg/network/ServerUnicast.go | 15 +++++++++------ pkg/protocol/RequestManager.go | 4 ++-- pkg/protocol/repository/ProtocolRepository.go | 7 +++++++ 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8ec3b02..e48db51 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,6 +2,8 @@ package main import ( "_StorBackEnd/pkg/network" + "_StorBackEnd/pkg/protocol" + "_StorBackEnd/pkg/protocol/repository" ) const ( @@ -12,11 +14,14 @@ const ( func main() { println("StorBackEnd started !") + protocolRepository := repository.CreateProtocolRepository() - multicast := network.CreateClientMulticast(MULTICAST_ADDRESS, MULTICAST_SECOND) + multicast := network.CreateClientMulticast(MULTICAST_ADDRESS, MULTICAST_SECOND, protocolRepository) go multicast.Run() - server := network.ServerUnicast{Network: "tcp", Address: UNICAST_ADDRESS} + requestManager := protocol.RequestManager{Repository: protocolRepository} + + server := network.ServerUnicast{Network: "tcp", Address: UNICAST_ADDRESS, ReqManager: &requestManager} server.Run() } diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 855d41f..23832b9 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -1,15 +1,17 @@ package network import ( + "_StorBackEnd/pkg/protocol/repository" "net" "time" ) // CreateClientMulticast Méthode de construction d'un instance de la stuct ClientMulticast -func CreateClientMulticast(address string, second time.Duration) ClientMulticast { +func CreateClientMulticast(address string, second time.Duration, repository *repository.ProtocolRepository) ClientMulticast { return ClientMulticast{ - address: address, - second: second, + address: address, + second: second, + repository: repository, } } @@ -21,6 +23,9 @@ type ClientMulticast struct { // second Temps en seconde entre chaque ping second time.Duration + + // Repository de protocol permettant de + repository *repository.ProtocolRepository } // Run Cette méthode démarre une commmunication multicast diff --git a/pkg/network/ServerUnicast.go b/pkg/network/ServerUnicast.go index f39f53c..1addabb 100644 --- a/pkg/network/ServerUnicast.go +++ b/pkg/network/ServerUnicast.go @@ -10,7 +10,7 @@ import ( type ServerUnicast struct { Network string Address string - ReqManager protocol.RequestManager + ReqManager *protocol.RequestManager } func (server ServerUnicast) Run() { @@ -29,12 +29,15 @@ func (server ServerUnicast) Run() { fmt.Printf("Error while accepting client : %s\n", err) return } else { - line, err := bufio.NewReader(con).ReadString('\n') - if err != nil { - return - } + for { // TODO : Extraire cette partie de code + line, err := bufio.NewReader(con).ReadString('\n') + if err != nil { + return + } - server.ReqManager.Execute(line) + result := server.ReqManager.Execute(line) + con.Write(append([]byte(result), '\n')) + } } } diff --git a/pkg/protocol/RequestManager.go b/pkg/protocol/RequestManager.go index 3cf7ecd..9e47fec 100644 --- a/pkg/protocol/RequestManager.go +++ b/pkg/protocol/RequestManager.go @@ -3,12 +3,12 @@ package protocol import "_StorBackEnd/pkg/protocol/repository" type RequestManager struct { - repository repository.ProtocolRepository + Repository *repository.ProtocolRepository } func (receiver RequestManager) Execute(request string) string { // On lis ce que l'on reçoit - result, executed := receiver.repository.ExecuteReader(request) + result, executed := receiver.Repository.ExecuteReader(request) // On renvoie la réponse (Comment pour fichier ?) if executed { diff --git a/pkg/protocol/repository/ProtocolRepository.go b/pkg/protocol/repository/ProtocolRepository.go index 2bc7421..0e12c22 100644 --- a/pkg/protocol/repository/ProtocolRepository.go +++ b/pkg/protocol/repository/ProtocolRepository.go @@ -4,6 +4,13 @@ import ( "_StorBackEnd/pkg/protocol" ) +func CreateProtocolRepository() *ProtocolRepository { + return &ProtocolRepository{ + protocolReaders: make(map[string]*protocol.IProtocolReader), + protocolWriters: make(map[string]*protocol.IProtocolWriter), + } +} + type ProtocolRepository struct { protocolReaders map[string]*protocol.IProtocolReader protocolWriters map[string]*protocol.IProtocolWriter