diff --git a/web-service-gin/Storage1/Important.txt b/web-service-gin/Storage2/Can.txt similarity index 100% rename from web-service-gin/Storage1/Important.txt rename to web-service-gin/Storage2/Can.txt diff --git a/web-service-gin/Storage2/Garbage.py b/web-service-gin/Storage2/Garbage.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage3/Ham.txt b/web-service-gin/Storage3/Ham.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage3/Important.txt b/web-service-gin/Storage3/Important.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage4/Can.txt b/web-service-gin/Storage4/Can.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage4/Ham.txt b/web-service-gin/Storage4/Ham.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage4/Test.html b/web-service-gin/Storage4/Test.html new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage5/Garbage.py b/web-service-gin/Storage5/Garbage.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage5/Test.html b/web-service-gin/Storage5/Test.html new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage6/Can.txt b/web-service-gin/Storage6/Can.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage6/Important.txt b/web-service-gin/Storage6/Important.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage7/Ham.txt b/web-service-gin/Storage7/Ham.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/Storage7/Important.txt b/web-service-gin/Storage7/Important.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/web-service-gin/main.go b/web-service-gin/main.go index c5b6f34470fd326c71e223e960144c748ffcd4ed..2c16187d0f66b37a8f9fa1e431c63aea748e2123 100644 --- a/web-service-gin/main.go +++ b/web-service-gin/main.go @@ -18,6 +18,8 @@ var port string var parent int var msgId = 0 var err error +var foundRsc = false +var basePort = 8080 // album represents data about a record album. type album struct { @@ -38,16 +40,14 @@ var albums = []album{ func initReq(c *gin.Context) { msgId = rand.Intn(100) - parent, err = strconv.Atoi(idServ) + parent = -1 printErr(err) for i := 0; i < len(albums); i++ { if albums[i].ID == idServ { for j := 0; j < len(albums[i].Neighb); j++ { - if parent != albums[i].Neighb[j] { - resp, err := http.Get("http://localhost:808" + strconv.Itoa(albums[i].Neighb[j]) + "/req/" + idServ + "/" + c.Param("tlv") + "/" + strconv.Itoa(msgId)) - printErr(err) - fmt.Println(resp.Status) - } + resp, err := http.Get("http://localhost:" + strconv.Itoa(basePort+albums[i].Neighb[j]) + "/req/" + idServ + "/" + c.Param("tlv") + "/" + strconv.Itoa(msgId) + "/" + c.Param("ressource")) + printErr(err) + fmt.Println(resp.Status) } } } @@ -59,32 +59,55 @@ func receiveReq(c *gin.Context) { transmitterId, err := strconv.Atoi(c.Param("parent")) printErr(err) tlv, err := strconv.Atoi(c.Param("tlv")) + printErr(err) + if msgId != idMsgInt { msgId = idMsgInt parent = transmitterId - fmt.Println("My parent is " + strconv.Itoa(transmitterId) + " and this is a message from the broadcast called " + strconv.Itoa(idMsgInt)) - } else { - fmt.Println("I already have a parent,dont care") + foundRsc = false + } + + for _, x := range arrFile { + if x == c.Param("ressource") { + if !foundRsc { + go sendFound(c.Param("ressource")) + } + foundRsc = true + break + } } + tlv = tlv - 1 if tlv != 0 { for i := 0; i < len(albums); i++ { + //Open Neighb reg of this node if albums[i].ID == idServ { for j := 0; j < len(albums[i].Neighb); j++ { - if transmitterId != albums[i].Neighb[j] { - resp, err := http.Get("http://localhost:808" + strconv.Itoa(albums[i].Neighb[j]) + "/req/" + idServ + "/" + strconv.Itoa(tlv) + "/" + strconv.Itoa(msgId)) + if transmitterId != albums[i].Neighb[j] && parent != albums[i].Neighb[j] { + resp, err := http.Get("http://localhost:" + strconv.Itoa(basePort+albums[i].Neighb[j]) + "/req/" + idServ + "/" + strconv.Itoa(tlv) + "/" + strconv.Itoa(msgId) + "/" + c.Param("ressource")) printErr(err) fmt.Println(resp.Status) } } } } - } else { - fmt.Println("Witnessed end of broadcast") } c.Status(http.StatusOK) } +//Called by child when ressource is find +func foundReq(c *gin.Context) { + if parent == -1 { + fmt.Println("Ressource found ") + } +} + +func sendFound(ressource string) { + resp, err := http.Get("http://localhost:" + strconv.Itoa(basePort+parent) + "/foundReq/" + idServ + "/" + ressource) + printErr(err) + fmt.Println(resp.Status) +} + func printErr(err error) { if err != nil { log.Fatal(err) @@ -96,18 +119,18 @@ func main() { idServ = os.Args[1] nbIdServ, err := strconv.Atoi(idServ) - files, err := ioutil.ReadDir("Storage1/") + files, err := ioutil.ReadDir("Storage" + idServ + "/") printErr(err) for _, file := range files { arrFile = append(arrFile, file.Name()) } - port = strconv.Itoa(8080 + nbIdServ) + port = strconv.Itoa(basePort + nbIdServ) router := gin.Default() - router.GET("/initReq/:tlv", initReq) - router.GET("/req/:parent/:tlv/:msgId", receiveReq) - fmt.Println(port) + router.GET("/initReq/:tlv/:ressource", initReq) + router.GET("/req/:parent/:tlv/:msgId/:ressource", receiveReq) + router.GET("/foundReq/:finder/:ressource", foundReq) /* resp, err := http.Get("http://localhost:8080/req")