diff --git a/web-service-gin/main.go b/web-service-gin/main.go index 0edc1001a63aad3f99cd140cc74aa3e41cdd2b74..c5b6f34470fd326c71e223e960144c748ffcd4ed 100644 --- a/web-service-gin/main.go +++ b/web-service-gin/main.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "log" + "math/rand" "net/http" "os" "strconv" @@ -14,6 +15,9 @@ import ( var arrFile []string var idServ string var port string +var parent int +var msgId = 0 +var err error // album represents data about a record album. type album struct { @@ -32,22 +36,52 @@ var albums = []album{ {ID: "7", Neighb: []int{3, 4}}, } -func reqNeighb(c *gin.Context) { +func initReq(c *gin.Context) { + msgId = rand.Intn(100) + parent, err = strconv.Atoi(idServ) + printErr(err) 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:808" + strconv.Itoa(albums[i].Neighb[j]) + "/req/" + idServ + "/" + strconv.Itoa(j)) - printErr(err) - fmt.Println(resp.Status) + 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) + } } } } - //c.IndentedJSON(http.StatusOK, albums) } func receiveReq(c *gin.Context) { - fmt.Println(c.Param("parent")) - fmt.Println(c.Param("tlv")) + 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")) + 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") + } + tlv = tlv - 1 + if tlv != 0 { + for i := 0; i < len(albums); i++ { + 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)) + printErr(err) + fmt.Println(resp.Status) + } + } + } + } + } else { + fmt.Println("Witnessed end of broadcast") + } c.Status(http.StatusOK) } @@ -71,8 +105,8 @@ func main() { port = strconv.Itoa(8080 + nbIdServ) router := gin.Default() - router.GET("/req", reqNeighb) - router.GET("/req/:parent/:tlv", receiveReq) + router.GET("/initReq/:tlv", initReq) + router.GET("/req/:parent/:tlv/:msgId", receiveReq) fmt.Println(port) /*