Skip to content
Snippets Groups Projects
Commit 0f6607bf authored by ivan.rigo's avatar ivan.rigo
Browse files

Start findReq Func

parent e376b791
No related branches found
No related tags found
No related merge requests found
Showing with 41 additions and 18 deletions
...@@ -18,6 +18,8 @@ var port string ...@@ -18,6 +18,8 @@ var port string
var parent int var parent int
var msgId = 0 var msgId = 0
var err error var err error
var foundRsc = false
var basePort = 8080
// album represents data about a record album. // album represents data about a record album.
type album struct { type album struct {
...@@ -38,16 +40,14 @@ var albums = []album{ ...@@ -38,16 +40,14 @@ var albums = []album{
func initReq(c *gin.Context) { func initReq(c *gin.Context) {
msgId = rand.Intn(100) msgId = rand.Intn(100)
parent, err = strconv.Atoi(idServ) parent = -1
printErr(err) printErr(err)
for i := 0; i < len(albums); i++ { for i := 0; i < len(albums); i++ {
if albums[i].ID == idServ { if albums[i].ID == idServ {
for j := 0; j < len(albums[i].Neighb); j++ { 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 + "/" + c.Param("tlv") + "/" + strconv.Itoa(msgId) + "/" + c.Param("ressource"))
resp, err := http.Get("http://localhost:808" + strconv.Itoa(albums[i].Neighb[j]) + "/req/" + idServ + "/" + c.Param("tlv") + "/" + strconv.Itoa(msgId)) printErr(err)
printErr(err) fmt.Println(resp.Status)
fmt.Println(resp.Status)
}
} }
} }
} }
...@@ -59,32 +59,55 @@ func receiveReq(c *gin.Context) { ...@@ -59,32 +59,55 @@ func receiveReq(c *gin.Context) {
transmitterId, err := strconv.Atoi(c.Param("parent")) transmitterId, err := strconv.Atoi(c.Param("parent"))
printErr(err) printErr(err)
tlv, err := strconv.Atoi(c.Param("tlv")) tlv, err := strconv.Atoi(c.Param("tlv"))
printErr(err)
if msgId != idMsgInt { if msgId != idMsgInt {
msgId = idMsgInt msgId = idMsgInt
parent = transmitterId parent = transmitterId
fmt.Println("My parent is " + strconv.Itoa(transmitterId) + " and this is a message from the broadcast called " + strconv.Itoa(idMsgInt)) foundRsc = false
} else { }
fmt.Println("I already have a parent,dont care")
for _, x := range arrFile {
if x == c.Param("ressource") {
if !foundRsc {
go sendFound(c.Param("ressource"))
}
foundRsc = true
break
}
} }
tlv = tlv - 1 tlv = tlv - 1
if tlv != 0 { if tlv != 0 {
for i := 0; i < len(albums); i++ { for i := 0; i < len(albums); i++ {
//Open Neighb reg of this node
if albums[i].ID == idServ { if albums[i].ID == idServ {
for j := 0; j < len(albums[i].Neighb); j++ { for j := 0; j < len(albums[i].Neighb); j++ {
if transmitterId != albums[i].Neighb[j] { if transmitterId != albums[i].Neighb[j] && parent != 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)) 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) printErr(err)
fmt.Println(resp.Status) fmt.Println(resp.Status)
} }
} }
} }
} }
} else {
fmt.Println("Witnessed end of broadcast")
} }
c.Status(http.StatusOK) 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) { func printErr(err error) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
...@@ -96,18 +119,18 @@ func main() { ...@@ -96,18 +119,18 @@ func main() {
idServ = os.Args[1] idServ = os.Args[1]
nbIdServ, err := strconv.Atoi(idServ) nbIdServ, err := strconv.Atoi(idServ)
files, err := ioutil.ReadDir("Storage1/") files, err := ioutil.ReadDir("Storage" + idServ + "/")
printErr(err) printErr(err)
for _, file := range files { for _, file := range files {
arrFile = append(arrFile, file.Name()) arrFile = append(arrFile, file.Name())
} }
port = strconv.Itoa(8080 + nbIdServ) port = strconv.Itoa(basePort + nbIdServ)
router := gin.Default() router := gin.Default()
router.GET("/initReq/:tlv", initReq) router.GET("/initReq/:tlv/:ressource", initReq)
router.GET("/req/:parent/:tlv/:msgId", receiveReq) router.GET("/req/:parent/:tlv/:msgId/:ressource", receiveReq)
fmt.Println(port) router.GET("/foundReq/:finder/:ressource", foundReq)
/* /*
resp, err := http.Get("http://localhost:8080/req") resp, err := http.Get("http://localhost:8080/req")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment