Skip to content
Snippets Groups Projects
Commit 9ab93a00 authored by IliasN's avatar IliasN
Browse files

Done maybe

parent a4a46942
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,20 @@ type System struct{
type Message struct{
Cmd int `json:cmd` // 0 = Query, 1 = QueryHit
Id string `json:id`
Filename string `json:filename`
Ttl int `json:ttl`
Ttl int `json:ttl,omitempty`
Found string `json:found,omitempty`
Source string `json:source`
}
type IdSource struct{
Id string
Source string
}
type Search struct{
Id string
Filename string
Found string
}
......@@ -5,6 +5,7 @@ import (
"os"
"net"
"encoding/json"
"errors"
"lab2/node"
......@@ -13,9 +14,15 @@ import (
var (
sys node.System
sources []node.IdSource
requests []node.Search
files [2]string
)
func main(){
sources = make([]node.IdSource, 0)
requests = make([]node.Search, 0)
files = [2]string{"ok.txt", "guigui.mp4"}
data, _ := os.ReadFile(os.Args[1])
_ = yaml.Unmarshal(data, sys)
fmt.Println(sys)
......@@ -38,6 +45,50 @@ func main(){
}
}
func hasRequest(id string) bool{
for _, v := range requests{
if(v.Id == id){
return true
}
}
return false
}
func updateRequests(id string, loca string){
for i, v := range requests{
if(v.Id == id){
requests[i].Found = loca
}
}
}
func hasSource(id string) bool{
for _, v := range sources{
if(v.Id == id){
return true
}
}
return false
}
func getSource(id string) (*string, error){
for _, v := range sources{
if(v.Id == id){
return &v.Source, nil
}
}
return nil, errors.New("No source for this key")
}
func hasFile(filename string) bool{
for _, v := range files{
if(v == filename){
return true
}
}
return false
}
func handleConnection(c net.Conn){
buffer := make([]byte, 2048)
n, err := c.Read(buffer)
......@@ -54,24 +105,59 @@ func handleConnection(c net.Conn){
if(msg.Cmd == 0){
// Query
query(msg)
} else if(msg.Cmd == 1){
// QueryHit
queryHit(msg)
} else{
// Unknown
fmt.Println("Unknown command...")
panic(errors.New("Unknown command"))
}
}
func query(){
func query(msg node.Message){
// if never received
if(!hasSource(msg.Id)){
// Check if have ressource
if(hasFile(msg.Filename)){
// If send to source new queryHit
source, _ := getSource(msg.Id)
hit := node.Message{
Cmd: 1,
Id: msg.Id,
Found: sys.Address,
}
SendMessage(hit, node.Node{Address: *source})
}
msg.Ttl--
if(msg.Ttl > 0){
s := msg.Source
msg.Source = sys.Address
// send to all but source
SendAll(msg, s)
}
}
}
func queryHit(){
func queryHit(msg node.Message){
if(hasRequest(msg.Id)){
// Stock dans liste requests la loca
updateRequests(msg.Id, msg.Found)
}else if(hasSource(msg.Id)){
// Envoyer à la prochaine source
source, _ := getSource(msg.Id)
SendMessage(msg, node.Node{Address: *source})
}
}
func SendAll(msg node.Message){
func SendAll(msg node.Message, not string){
for _, nei := range sys.Neighbours{
if(nei.Address != not){
go SendMessage(msg, nei)
}
}
}
func SendMessage(msg node.Message, nd node.Node){
conn, err := net.Dial("tcp", "127.0.0.1:" + nd.Address)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment