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

Done

parent 9ab93a00
No related branches found
No related tags found
No related merge requests found
data/*.yaml
package main
import (
"os"
"fmt"
"net"
"encoding/json"
"bufio"
"lab2/node"
)
func main(){
send_message(create_message(2, os.Args[2]), os.Args[1])
}
func wait(){
reader := bufio.NewReader(os.Stdin)
fmt.Println("Appuyez sur ENTER pour continuer...")
reader.ReadString('\n')
}
func send_message(msg node.Message, address string){
conn, err := net.Dial("tcp", "127.0.0.1:" + address)
if err != nil {
fmt.Println("Error connecting:", err.Error())
os.Exit(1)
}
data, _ := json.Marshal(msg)
conn.Write(data)
rcv := make([]byte, 2048)
n, _ := conn.Read(rcv)
fmt.Println(string(rcv[:n]))
conn.Close()
}
func create_message(id int, file string) node.Message{
return node.Message{
Cmd: id,
Filename: file,
}
}
......@@ -28,5 +28,5 @@ type IdSource struct{
type Search struct{
Id string
Filename string
Found string
Found []string
}
......@@ -5,26 +5,34 @@ import (
"os"
"net"
"encoding/json"
"encoding/base64"
"errors"
"crypto/sha256"
"time"
"lab2/node"
"gopkg.in/yaml.v2"
)
const (
TIMEOUT = 200
TTL = 255
)
var (
sys node.System
sources []node.IdSource
requests []node.Search
files [2]string
files []string
client 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)
yaml.Unmarshal(data, &sys)
fmt.Println(sys)
l, err := net.Listen("tcp", "127.0.0.1:" + sys.Address)
if err != nil {
......@@ -33,6 +41,11 @@ func main(){
}
fmt.Printf("Node %d listening.\n", sys.Id)
defer l.Close()
files = make([]string, 0)
if(sys.Id == 3){
files = append(files, "ok.txt")
}
files = append(files, "guigui.mp4")
for {
c, err := l.Accept()
......@@ -57,9 +70,18 @@ func hasRequest(id string) bool{
func updateRequests(id string, loca string){
for i, v := range requests{
if(v.Id == id){
requests[i].Found = loca
requests[i].Found = append(requests[i].Found, loca)
}
}
}
func getRequest(id string) node.Search{
for _, v := range requests{
if(v.Id == id){
return v
}
}
return node.Search{}
}
func hasSource(id string) bool{
......@@ -95,7 +117,6 @@ func handleConnection(c net.Conn){
if err != nil {
panic(err)
}
defer c.Close()
var msg node.Message
err = json.Unmarshal(buffer[:n], &msg)
......@@ -106,9 +127,26 @@ func handleConnection(c net.Conn){
if(msg.Cmd == 0){
// Query
query(msg)
c.Close()
} else if(msg.Cmd == 1){
// QueryHit
queryHit(msg)
c.Close()
} else if(msg.Cmd == 2){
// Start search
client = msg.Source
hashb := sha256.Sum256([]byte(msg.Filename + time.Now().String()))
hash := base64.StdEncoding.EncodeToString(hashb[:])
requests = append(requests, node.Search{Id: hash, Filename: msg.Filename, Found: make([]string, 0)})
go responde_client(c, hash)
msg.Cmd = 0
msg.Ttl = TTL
msg.Source = sys.Address
msg.Id = hash
if(hasFile(msg.Filename)){
updateRequests(msg.Id, sys.Address)
}
SendAll(msg, "")
} else{
// Unknown
fmt.Println("Unknown command...")
......@@ -116,13 +154,24 @@ func handleConnection(c net.Conn){
}
}
func responde_client(c net.Conn, id string){
time.Sleep(TIMEOUT * time.Millisecond)
data, _ := json.Marshal(getRequest(id))
c.Write(data)
c.Close()
}
func query(msg node.Message){
// if never received
if(!hasSource(msg.Id)){
sources = append(sources, node.IdSource{Id: msg.Id, Source: msg.Source})
// Check if have ressource
if(hasFile(msg.Filename)){
// If send to source new queryHit
source, _ := getSource(msg.Id)
source, err := getSource(msg.Id)
if(err != nil){
panic(err)
}
hit := node.Message{
Cmd: 1,
Id: msg.Id,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment