Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sys_dist_lab2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ilias.nhairi
sys_dist_lab2
Commits
9ab93a00
Commit
9ab93a00
authored
3 years ago
by
IliasN
Browse files
Options
Downloads
Patches
Plain Diff
Done maybe
parent
a4a46942
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
node/node.go
+14
-1
14 additions, 1 deletion
node/node.go
server.go
+90
-4
90 additions, 4 deletions
server.go
with
104 additions
and
5 deletions
node/node.go
+
14
−
1
View file @
9ab93a00
...
...
@@ -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
}
This diff is collapsed.
Click to expand it.
server.go
+
90
−
4
View file @
9ab93a00
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment