diff --git a/node/node.go b/node/node.go index 2b709438c9dd1135bbf125879057f7e932dcc77f..30336b9ccaa54ef251875bc6833ae8a912fc08f1 100644 --- a/node/node.go +++ b/node/node.go @@ -13,7 +13,20 @@ type System struct{ type Message struct{ Cmd int `json:cmd` // 0 = Query, 1 = QueryHit + Id string `json:id` Filename string `json:filename` - Ttl int `json:ttl` + Ttl int `json:ttl,omitempty` Found string `json:found,omitempty` + Source string `json:source` +} + +type IdSource struct{ + Id string + Source string +} + +type Search struct{ + Id string + Filename string + Found string } diff --git a/server.go b/server.go index 2305b182b39c3582ccf8491fbbc43fcc207c2200..c345bd9ab2ee28af8f11055ef6d0d546698dda75 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "os" "net" "encoding/json" + "errors" "lab2/node" @@ -13,9 +14,15 @@ import ( var ( sys node.System + sources []node.IdSource + requests []node.Search + files [2]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) fmt.Println(sys) @@ -38,6 +45,50 @@ func main(){ } } +func hasRequest(id string) bool{ + for _, v := range requests{ + if(v.Id == id){ + return true + } + } + return false +} + +func updateRequests(id string, loca string){ + for i, v := range requests{ + if(v.Id == id){ + requests[i].Found = loca + } + } +} + +func hasSource(id string) bool{ + for _, v := range sources{ + if(v.Id == id){ + return true + } + } + return false +} + +func getSource(id string) (*string, error){ + for _, v := range sources{ + if(v.Id == id){ + return &v.Source, nil + } + } + return nil, errors.New("No source for this key") +} + +func hasFile(filename string) bool{ + for _, v := range files{ + if(v == filename){ + return true + } + } + return false +} + func handleConnection(c net.Conn){ buffer := make([]byte, 2048) n, err := c.Read(buffer) @@ -54,22 +105,57 @@ func handleConnection(c net.Conn){ if(msg.Cmd == 0){ // Query + query(msg) } else if(msg.Cmd == 1){ // QueryHit + queryHit(msg) } else{ // Unknown + fmt.Println("Unknown command...") + panic(errors.New("Unknown command")) } } -func query(){ +func query(msg node.Message){ + // if never received + if(!hasSource(msg.Id)){ + // Check if have ressource + if(hasFile(msg.Filename)){ + // If send to source new queryHit + source, _ := getSource(msg.Id) + hit := node.Message{ + Cmd: 1, + Id: msg.Id, + Found: sys.Address, + } + SendMessage(hit, node.Node{Address: *source}) + } + msg.Ttl-- + if(msg.Ttl > 0){ + s := msg.Source + msg.Source = sys.Address + // send to all but source + SendAll(msg, s) + } + } } -func queryHit(){ +func queryHit(msg node.Message){ + if(hasRequest(msg.Id)){ + // Stock dans liste requests la loca + updateRequests(msg.Id, msg.Found) + }else if(hasSource(msg.Id)){ + // Envoyer à la prochaine source + source, _ := getSource(msg.Id) + SendMessage(msg, node.Node{Address: *source}) + } } -func SendAll(msg node.Message){ +func SendAll(msg node.Message, not string){ for _, nei := range sys.Neighbours{ - go SendMessage(msg, nei) + if(nei.Address != not){ + go SendMessage(msg, nei) + } } }