Skip to content
Snippets Groups Projects
Commit 1eb29b8c authored by jeremy.martinot's avatar jeremy.martinot :speech_balloon:
Browse files

Message have a Contenu in place of simple key, add right adress for a content ;)

parent b5ff987b
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
type Message struct { type Message struct {
Id uint64 Id uint64
Key int Content Contenu
Type_ string Type_ string
} }
...@@ -32,7 +32,7 @@ type Contenu struct { ...@@ -32,7 +32,7 @@ type Contenu struct {
var ( var (
thisnode Node thisnode Node
port string port string
//Liste des requetes deja vues/effectués Pourquoi int ? //Liste des requetes deja vues/effectués
requests []uint64 requests []uint64
msg_count_uuid uint64 //valeur dont on commencera a compter les id msg_count_uuid uint64 //valeur dont on commencera a compter les id
) )
...@@ -40,20 +40,22 @@ var ( ...@@ -40,20 +40,22 @@ var (
func fillContents() { func fillContents() {
println(thisnode.MyKey) println(thisnode.MyKey)
for _, key := range thisnode.MyKey { for _, key := range thisnode.MyKey {
addContent(key, thisnode.Address) myContent := createContenu(key,thisnode.Address)
addContent(myContent)
fmt.Println(key) fmt.Println(key)
} }
for _, neighbour := range thisnode.Neighbours { for _, neighbour := range thisnode.Neighbours {
for _, content := range neighbour.MyKey { for _, key := range neighbour.MyKey {
addContent(content, neighbour.Address) hisContent := createContenu(key,neighbour.Address)
addContent(hisContent)
} }
} }
} }
func createMessage(id uint64, key int, _type string) Message { func createMessage(id uint64, content Contenu, _type string) Message {
return Message{ return Message{
Id: id, Id: id,
Key: key, Content: content,
Type_: _type, Type_: _type,
} }
} }
...@@ -136,17 +138,18 @@ func handleRequest(msg Message, conn net.Conn) { ...@@ -136,17 +138,18 @@ func handleRequest(msg Message, conn net.Conn) {
sender := conn.RemoteAddr().String() sender := conn.RemoteAddr().String()
requests = append(requests, msg.Id) requests = append(requests, msg.Id)
if !hasAlreadySeen(msg) { if !hasAlreadySeen(msg) {
if hasKey(msg.Key) { if hasKey(msg.Content.Key) {
sendMessage(createMessage(msg.Id, msg.Key, "POSITIVE"),sender) msg.Content.Origin = thisnode.Address
sendMessage(createMessage(msg.Id, msg.Content, "POSITIVE"),sender)
} else { } else {
if sendToClosestWhilePositive(msg,sender) == true { if sendToClosestWhilePositive(msg,sender) == true {
sendMessage(createMessage(msg.Id, msg.Key, "POSITIVE"),sender) sendMessage(createMessage(msg.Id, msg.Content, "POSITIVE"),sender)
} else { } else {
sendMessage(createMessage(msg.Id, msg.Key, "NEGATIVE"),sender) sendMessage(createMessage(msg.Id, msg.Content, "NEGATIVE"),sender)
} }
} }
} else { } else {
sendMessage(createMessage(msg.Id, msg.Key, "NEGATIVE"),sender) sendMessage(createMessage(msg.Id, msg.Content, "NEGATIVE"),sender)
} }
} }
...@@ -155,12 +158,10 @@ func sortContents() { ...@@ -155,12 +158,10 @@ func sortContents() {
return thisnode.Contents[p].Key < thisnode.Contents[q].Key }) return thisnode.Contents[p].Key < thisnode.Contents[q].Key })
} }
func addContent(key int, ownerIp string) { func addContent(content Contenu) {
newcontent := createContenu(key,ownerIp) thisnode.Contents = append(thisnode.Contents,content)
thisnode.Contents = append(thisnode.Contents,newcontent)
} }
func hasKey(key int) bool { func hasKey(key int) bool {
for _, element := range thisnode.Contents { for _, element := range thisnode.Contents {
if key == element.Key { if key == element.Key {
...@@ -170,10 +171,9 @@ func hasKey(key int) bool { ...@@ -170,10 +171,9 @@ func hasKey(key int) bool {
return false return false
} }
func handleReply(msg Message, src string) bool{ func handleReply(msg Message, src string) bool{
if msg.Type_ == "POSITIVE" { if msg.Type_ == "POSITIVE" {
addContent(msg.Key,src) addContent(msg.Content)
return true return true
} }
if msg.Type_ == "NEGATIVE" { if msg.Type_ == "NEGATIVE" {
...@@ -227,7 +227,8 @@ func main() { ...@@ -227,7 +227,8 @@ func main() {
fmt.Println(err) fmt.Println(err)
os.Exit(2) os.Exit(2)
} }
msg := createMessage(msg_count_uuid, key, "REQUEST") contentToFind := createContenu(key,"")
msg := createMessage(msg_count_uuid, contentToFind, "REQUEST")
has := sendToClosestWhilePositive(msg,thisnode.Address) has := sendToClosestWhilePositive(msg,thisnode.Address)
println(has) println(has)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment