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

added YAML, need to fuse

parent cafeafa5
Branches
No related tags found
No related merge requests found
......@@ -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))
}
id: 1
address: "localhost:8081"
neighbours:
- id: 2
address: "localhost:8082"
- id: 3
address: "localhost:8083"
- id: 4
address: "localhost:8084"
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
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
id: 4
address: "localhost:8084"
neighbours:
- id: 1
address: "localhost:8081"
- id: 7
address: "localhost:8087"
id: 5
address: "localhost:8085"
neighbours:
- id: 2
address: "localhost:8082"
- id: 6
address: "localhost:8086"
id: 6
address: "localhost:8086"
neighbours:
- id: 2
address: "localhost:8082"
- id: 3
address: "localhost:8083"
- id: 5
address: "localhost:8085"
id: 7
address: "localhost:8087"
neighbours:
- id: 3
address: "localhost:8083"
- id: 4
address: "localhost:8084"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment