From daa4401c3c492f81a38361048a2844a70d47df9f Mon Sep 17 00:00:00 2001 From: IliasN <iliascfpt@gmail.com> Date: Wed, 24 Nov 2021 15:32:35 +0100 Subject: [PATCH] Done --- .gitignore | 1 + client.go | 44 ++++++++++++++++++++++++++++++ node/node.go | 2 +- server.go | 75 +++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 .gitignore create mode 100644 client.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f3257d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data/*.yaml diff --git a/client.go b/client.go new file mode 100644 index 0000000..73432e8 --- /dev/null +++ b/client.go @@ -0,0 +1,44 @@ +package main + +import ( + "os" + "fmt" + "net" + "encoding/json" + "bufio" + + "lab2/node" +) + +func main(){ + send_message(create_message(2, os.Args[2]), os.Args[1]) +} + +func wait(){ + reader := bufio.NewReader(os.Stdin) + fmt.Println("Appuyez sur ENTER pour continuer...") + reader.ReadString('\n') +} + +func send_message(msg node.Message, address string){ + conn, err := net.Dial("tcp", "127.0.0.1:" + address) + if err != nil { + fmt.Println("Error connecting:", err.Error()) + os.Exit(1) + } + data, _ := json.Marshal(msg) + conn.Write(data) + rcv := make([]byte, 2048) + n, _ := conn.Read(rcv) + fmt.Println(string(rcv[:n])) + conn.Close() +} + + +func create_message(id int, file string) node.Message{ + return node.Message{ + Cmd: id, + Filename: file, + } +} + diff --git a/node/node.go b/node/node.go index 30336b9..9a0cf35 100644 --- a/node/node.go +++ b/node/node.go @@ -28,5 +28,5 @@ type IdSource struct{ type Search struct{ Id string Filename string - Found string + Found []string } diff --git a/server.go b/server.go index c345bd9..559450b 100644 --- a/server.go +++ b/server.go @@ -5,26 +5,34 @@ import ( "os" "net" "encoding/json" + "encoding/base64" "errors" + "crypto/sha256" + "time" "lab2/node" "gopkg.in/yaml.v2" ) +const ( + TIMEOUT = 200 + TTL = 255 +) + var ( sys node.System sources []node.IdSource requests []node.Search - files [2]string + files []string + client string ) func main(){ sources = make([]node.IdSource, 0) requests = make([]node.Search, 0) - files = [2]string{"ok.txt", "guigui.mp4"} data, _ := os.ReadFile(os.Args[1]) - _ = yaml.Unmarshal(data, sys) + yaml.Unmarshal(data, &sys) fmt.Println(sys) l, err := net.Listen("tcp", "127.0.0.1:" + sys.Address) if err != nil { @@ -33,16 +41,21 @@ func main(){ } fmt.Printf("Node %d listening.\n", sys.Id) defer l.Close() + files = make([]string, 0) + if(sys.Id == 3){ + files = append(files, "ok.txt") + } + files = append(files, "guigui.mp4") - for { - c, err := l.Accept() - if err != nil { - fmt.Println("Error connecting:", err.Error()) - return - } + for { + c, err := l.Accept() + if err != nil { + fmt.Println("Error connecting:", err.Error()) + return + } handleConnection(c) - } + } } func hasRequest(id string) bool{ @@ -57,11 +70,20 @@ func hasRequest(id string) bool{ func updateRequests(id string, loca string){ for i, v := range requests{ if(v.Id == id){ - requests[i].Found = loca + requests[i].Found = append(requests[i].Found, loca) } } } +func getRequest(id string) node.Search{ + for _, v := range requests{ + if(v.Id == id){ + return v + } + } + return node.Search{} +} + func hasSource(id string) bool{ for _, v := range sources{ if(v.Id == id){ @@ -95,7 +117,6 @@ func handleConnection(c net.Conn){ if err != nil { panic(err) } - defer c.Close() var msg node.Message err = json.Unmarshal(buffer[:n], &msg) @@ -106,9 +127,26 @@ func handleConnection(c net.Conn){ if(msg.Cmd == 0){ // Query query(msg) + c.Close() } else if(msg.Cmd == 1){ // QueryHit queryHit(msg) + c.Close() + } else if(msg.Cmd == 2){ + // Start search + client = msg.Source + hashb := sha256.Sum256([]byte(msg.Filename + time.Now().String())) + hash := base64.StdEncoding.EncodeToString(hashb[:]) + requests = append(requests, node.Search{Id: hash, Filename: msg.Filename, Found: make([]string, 0)}) + go responde_client(c, hash) + msg.Cmd = 0 + msg.Ttl = TTL + msg.Source = sys.Address + msg.Id = hash + if(hasFile(msg.Filename)){ + updateRequests(msg.Id, sys.Address) + } + SendAll(msg, "") } else{ // Unknown fmt.Println("Unknown command...") @@ -116,13 +154,24 @@ func handleConnection(c net.Conn){ } } +func responde_client(c net.Conn, id string){ + time.Sleep(TIMEOUT * time.Millisecond) + data, _ := json.Marshal(getRequest(id)) + c.Write(data) + c.Close() +} + func query(msg node.Message){ // if never received if(!hasSource(msg.Id)){ + sources = append(sources, node.IdSource{Id: msg.Id, Source: msg.Source}) // Check if have ressource if(hasFile(msg.Filename)){ // If send to source new queryHit - source, _ := getSource(msg.Id) + source, err := getSource(msg.Id) + if(err != nil){ + panic(err) + } hit := node.Message{ Cmd: 1, Id: msg.Id, -- GitLab