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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ilias.nhairi
sys_dist_lab2
Commits
daa4401c
Commit
daa4401c
authored
3 years ago
by
IliasN
Browse files
Options
Downloads
Patches
Plain Diff
Done
parent
9ab93a00
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
client.go
+44
-0
44 additions, 0 deletions
client.go
node/node.go
+1
-1
1 addition, 1 deletion
node/node.go
server.go
+62
-13
62 additions, 13 deletions
server.go
with
108 additions
and
14 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
daa4401c
data/*.yaml
This diff is collapsed.
Click to expand it.
client.go
0 → 100644
+
44
−
0
View file @
daa4401c
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
,
}
}
This diff is collapsed.
Click to expand it.
node/node.go
+
1
−
1
View file @
daa4401c
...
...
@@ -28,5 +28,5 @@ type IdSource struct{
type
Search
struct
{
Id
string
Filename
string
Found
string
Found
[]
string
}
This diff is collapsed.
Click to expand it.
server.go
+
62
−
13
View file @
daa4401c
...
...
@@ -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
,
...
...
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