diff --git a/web-service-gin/main.go b/web-service-gin/Gnutella.go similarity index 59% rename from web-service-gin/main.go rename to web-service-gin/Gnutella.go index a5229e90fbaa3a5b6856f47cbf4fe17a54082ca3..aeac7b5beefab6de128f64aed6aa9890ad6792b7 100644 --- a/web-service-gin/main.go +++ b/web-service-gin/Gnutella.go @@ -8,17 +8,18 @@ import ( "net/http" "os" "strconv" + "sync" "github.com/gin-gonic/gin" ) -var arrFile []string +var storage []string var idServ string -var parent int +var transmitter int var msgId = 0 var err error -var foundRsc = false var basePort = 8080 +var assignTransM sync.Mutex // album represents data about a record album. type album struct { @@ -37,26 +38,29 @@ var albums = []album{ {ID: "7", Neighb: []int{3, 4}}, } +func getRequest(urlTxt string) { + resp, err := http.Get(urlTxt) + printErr(err) + fmt.Println(resp.Status) +} + func initReq(c *gin.Context) { - msgId = rand.Intn(100) - parent = -1 - foundRsc = false + nbIdServ, err := strconv.Atoi(idServ) printErr(err) + msgId = rand.Intn(100) + nbIdServ + transmitter = -1 fmt.Println("\n Start of new research ") - for _, x := range arrFile { + for _, x := range storage { if x == c.Param("ressource") { fmt.Println("\n Ressource exist in Client ") - foundRsc = true break } } for i := 0; i < len(albums); i++ { if albums[i].ID == idServ { for j := 0; j < len(albums[i].Neighb); j++ { - 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) + go getRequest("http://localhost:" + strconv.Itoa(basePort+albums[i].Neighb[j]) + "/req/" + idServ + "/" + c.Param("tlv") + "/" + strconv.Itoa(msgId) + "/" + c.Param("ressource")) } } } @@ -65,22 +69,22 @@ func initReq(c *gin.Context) { func receiveReq(c *gin.Context) { idMsgInt, err := strconv.Atoi(c.Param("msgId")) printErr(err) - transmitterId, err := strconv.Atoi(c.Param("parent")) - printErr(err) - tlv, err := strconv.Atoi(c.Param("tlv")) - printErr(err) + assignTransM.Lock() if msgId != idMsgInt { msgId = idMsgInt - parent = transmitterId - foundRsc = false + assignTransM.Unlock() + + transmitterId, err := strconv.Atoi(c.Param("transmitter")) + printErr(err) + transmitter = transmitterId - for _, x := range arrFile { + tlv, err := strconv.Atoi(c.Param("tlv")) + printErr(err) + + for _, x := range storage { if x == c.Param("ressource") { - if !foundRsc { - go sendFound(c.Param("ressource"), idServ) - } - foundRsc = true + go getRequest("http://localhost:" + strconv.Itoa(basePort+transmitter) + "/foundReq/" + idServ + "/" + c.Param("ressource")) break } } @@ -91,15 +95,15 @@ func receiveReq(c *gin.Context) { //Open Neighb reg of this node 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:" + strconv.Itoa(basePort+albums[i].Neighb[j]) + "/req/" + idServ + "/" + strconv.Itoa(tlv) + "/" + strconv.Itoa(msgId) + "/" + c.Param("ressource")) - printErr(err) - fmt.Println(resp.Status) + if transmitter != albums[i].Neighb[j] { + getRequest("http://localhost:" + strconv.Itoa(basePort+albums[i].Neighb[j]) + "/req/" + idServ + "/" + strconv.Itoa(tlv) + "/" + strconv.Itoa(msgId) + "/" + c.Param("ressource")) } } } } } + } else { + assignTransM.Unlock() } c.Status(http.StatusOK) } @@ -109,19 +113,13 @@ func foundReq(c *gin.Context) { finderNb, err := strconv.Atoi(c.Param("finder")) printErr(err) portFinder := strconv.Itoa(basePort + finderNb) - if parent == -1 { + if transmitter == -1 { fmt.Println("\n Instance of " + c.Param("ressource") + " found in " + portFinder) } else { - sendFound(c.Param("ressource"), c.Param("finder")) + getRequest("http://localhost:" + strconv.Itoa(basePort+transmitter) + "/foundReq/" + c.Param("finder") + "/" + c.Param("ressource")) } } -func sendFound(ressource string, finder string) { - resp, err := http.Get("http://localhost:" + strconv.Itoa(basePort+parent) + "/foundReq/" + finder + "/" + ressource) - printErr(err) - fmt.Println(resp.Status) -} - func printErr(err error) { if err != nil { log.Fatal(err) @@ -137,20 +135,13 @@ func main() { printErr(err) for _, file := range files { - arrFile = append(arrFile, file.Name()) + storage = append(storage, file.Name()) } router := gin.Default() router.GET("/initReq/:tlv/:ressource", initReq) - router.GET("/req/:parent/:tlv/:msgId/:ressource", receiveReq) + router.GET("/req/:transmitter/:tlv/:msgId/:ressource", receiveReq) router.GET("/foundReq/:finder/:ressource", foundReq) - /* - resp, err := http.Get("http://localhost:8080/req") - printErr(err) - body, err := ioutil.ReadAll(resp.Body) - printErr(err) - fmt.Println(body) - */ router.Run("localhost:" + strconv.Itoa(basePort+nbIdServ)) } diff --git a/web-service-gin/neighbour-1.yaml b/web-service-gin/neighbour-1.yaml new file mode 100644 index 0000000000000000000000000000000000000000..56b14b28249759895f468fd558df15efdc638b85 --- /dev/null +++ b/web-service-gin/neighbour-1.yaml @@ -0,0 +1,11 @@ +id: 1 +address: "localhost:8081" +neighbours: + - id: 2 + address: "localhost:8082" + + - id: 3 + address: "localhost:8083" + + - id: 4 + address: "localhost:8084" diff --git a/web-service-gin/neighbour-2.yaml b/web-service-gin/neighbour-2.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ebe04f121d6a4756d586bc97632d9da3f3e3d933 --- /dev/null +++ b/web-service-gin/neighbour-2.yaml @@ -0,0 +1,11 @@ +id: 2 +address: "localhost:8082" +neighbours: + - id: 1 + address: "localhost:8081" + + - id: 5 + address: "localhost:8085" + + - id: 6 + address: "localhost:8086" \ No newline at end of file diff --git a/web-service-gin/neighbour-3.yaml b/web-service-gin/neighbour-3.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3dc6f7c7f8734c4174dd610b6ab93f34c1dd0ab8 --- /dev/null +++ b/web-service-gin/neighbour-3.yaml @@ -0,0 +1,11 @@ +id: 3 +address: "localhost:8083" +neighbours: + - id: 1 + address: "localhost:8081" + + - id: 6 + address: "localhost:8086" + + - id: 7 + address: "localhost:8087" \ No newline at end of file diff --git a/web-service-gin/neighbour-4.yaml b/web-service-gin/neighbour-4.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0c7475575d6c0c72f7082c8f1442d5db20575fdc --- /dev/null +++ b/web-service-gin/neighbour-4.yaml @@ -0,0 +1,8 @@ +id: 4 +address: "localhost:8084" +neighbours: + - id: 1 + address: "localhost:8081" + + - id: 7 + address: "localhost:8087" diff --git a/web-service-gin/neighbour-5.yaml b/web-service-gin/neighbour-5.yaml new file mode 100644 index 0000000000000000000000000000000000000000..cb323ac1d0ada3dbe2ea2987aa93d78577bbced6 --- /dev/null +++ b/web-service-gin/neighbour-5.yaml @@ -0,0 +1,8 @@ +id: 5 +address: "localhost:8085" +neighbours: + - id: 2 + address: "localhost:8082" + + - id: 6 + address: "localhost:8086" diff --git a/web-service-gin/neighbour-6.yaml b/web-service-gin/neighbour-6.yaml new file mode 100644 index 0000000000000000000000000000000000000000..93548464e5ea096b3bc296e6603c89bff61e9516 --- /dev/null +++ b/web-service-gin/neighbour-6.yaml @@ -0,0 +1,11 @@ +id: 6 +address: "localhost:8086" +neighbours: + - id: 2 + address: "localhost:8082" + + - id: 3 + address: "localhost:8083" + + - id: 5 + address: "localhost:8085" diff --git a/web-service-gin/neighbour-7.yaml b/web-service-gin/neighbour-7.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6ed89d6d2941e204ce32995c5d249d497d4c7633 --- /dev/null +++ b/web-service-gin/neighbour-7.yaml @@ -0,0 +1,8 @@ +id: 7 +address: "localhost:8087" +neighbours: + - id: 3 + address: "localhost:8083" + + - id: 4 + address: "localhost:8084" \ No newline at end of file