Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IA04 TP3
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
Package registry
Container 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
Mohamed Fall
IA04 TP3
Commits
e79d7a20
Commit
e79d7a20
authored
2 years ago
by
Antoine Kryus
Browse files
Options
Downloads
Patches
Plain Diff
add server functions, response and request types, utils function ranking to give ballot result
parent
fc8b29fc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ia04/agt/ballotagent/ballotAgent.go
+137
-25
137 additions, 25 deletions
ia04/agt/ballotagent/ballotAgent.go
ia04/comsoc/utils.go
+9
-0
9 additions, 0 deletions
ia04/comsoc/utils.go
ia04/go.mod
+1
-3
1 addition, 3 deletions
ia04/go.mod
with
147 additions
and
28 deletions
ia04/agt/ballotagent/ballotAgent.go
+
137
−
25
View file @
e79d7a20
...
...
@@ -11,34 +11,68 @@ import (
"log"
)
type
Request
struct
{
Vote
[]
comsoc
.
Alternative
`json:"vote"`
// je comprends pas le json:""
ID
int
`json:"id"`
Meth
string
type
RequestVote
struct
{
voteID
string
`json:"vote-id"`
prefs
[]
Alternative
`json:"prefs"`
agentID
int
`json:"agent-id"`
options
[]
Alternative
`json:"options"`
}
type
Response
struct
{
Result
int
`json:"res"`
type
RequestNewBallot
struct
{
rule
string
`json:"rule"`
deadline
string
`json:"deadline"`
voterIDs
[]
string
`json:"voter-ids"`
alts
int
`json:"#alts"`
}
type
RequestResult
struct
{
ballotID
string
`json:"ballot-id"`
}
type
ResponseNewBallot
struct
{
ballotID
string
`json:"ballot-id"`
// code int
}
// type ResponseVote struct {
// code int
// }
type
ResponseResult
struct
{
winner
int
`json:"winner"`
ranking
[]
Alternative
`json:"ranking"`
// code int
}
type
BallotID
int
type
RestBallotAgent
struct
{
sync
.
Mutex
ID
BallotID
addr
string
pfl
comsoc
.
Profile
addr
string
// the only non-varying value in one
// server instance, the rest is change
// every RequestNewBallot
pfl
Profile
rule
string
isOpen
bool
// open: 1 / closed: 0
voterIDs
[]
string
alts
[]
Alternative
}
type
Alternative
comsoc
.
Alternative
type
Profile
comsoc
.
Profile
type
RestBallotI
interface
{
checkMethod
(
method
string
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
decodeRequest
(
r
*
http
.
Request
)
(
req
Request
,
err
error
)
decodeRequestNewBallot
(
r
*
http
.
Request
)
(
req
RequestNewBallot
,
err
error
)
decodeRequestVote
(
r
*
http
.
Request
)
(
req
RequestVote
,
err
error
)
decodeRequestResult
(
r
*
http
.
Request
)
(
req
RequestResult
,
err
error
)
}
// je ne sais pas trop comment utiliser l'interface je te laisse si tu préfères
func
NewRestBallotAgent
(
addr
string
,
emptyProfile
comsoc
.
Profile
)
*
RestBallotAgent
{
return
&
RestBallotAgent
{
ID
:
BallotID
(
1
),
addr
:
addr
,
pfl
:
emptyProfil
e
}
func
NewRestBallotAgent
(
addr
string
,
emptyProfile
Profile
)
*
RestBallotAgent
{
return
&
RestBallotAgent
{
ID
:
BallotID
(
0
),
addr
:
addr
,
isOpen
:
tru
e
}
}
// Test de la méthode
...
...
@@ -51,14 +85,29 @@ func (rsa *RestBallotAgent) checkMethod(method string, w http.ResponseWriter, r
return
true
}
func
(
*
RestBallotAgent
)
decodeRequest
(
r
*
http
.
Request
)
(
req
Request
,
err
error
)
{
func
(
*
RestBallotAgent
)
decodeRequest
NewBallot
(
r
*
http
.
Request
)
(
req
Request
NewBallot
,
err
error
)
{
buf
:=
new
(
bytes
.
Buffer
)
buf
.
ReadFrom
(
r
.
Body
)
err
=
json
.
Unmarshal
(
buf
.
Bytes
(),
&
req
)
return
}
func
(
rba
*
RestBallotAgent
)
doVote
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
*
RestBallotAgent
)
decodeRequestVote
(
r
*
http
.
Request
)
(
req
RequestVote
,
err
error
)
{
buf
:=
new
(
bytes
.
Buffer
)
buf
.
ReadFrom
(
r
.
Body
)
err
=
json
.
Unmarshal
(
buf
.
Bytes
(),
&
req
)
return
}
func
(
*
RestBallotAgent
)
decodeRequestResult
(
r
*
http
.
Request
)
(
req
RequestResult
,
err
error
)
{
buf
:=
new
(
bytes
.
Buffer
)
buf
.
ReadFrom
(
r
.
Body
)
err
=
json
.
Unmarshal
(
buf
.
Bytes
(),
&
req
)
return
}
func
(
rba
*
RestBallotAgent
)
doNewBallot
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// mise à jour du nombre de requêtes
rba
.
Lock
()
defer
rba
.
Unlock
()
...
...
@@ -70,7 +119,7 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
}
// décodage de la requête
req
,
err
:=
rba
.
decodeRequest
(
r
)
req
,
err
:=
rba
.
decodeRequest
NewBallot
(
r
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprint
(
w
,
err
.
Error
())
...
...
@@ -78,23 +127,44 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
}
// traitement de la requête
var
resp
Response
var
resp
Response
NewBallot
rba
.
pfl
=
append
(
rba
.
pfl
,
req
.
Vote
)
// req should have vote (list of int) and ID (int)
if
rba
.
ID
!=
0
{
// zero Value
rba
.
ID
=
rba
.
ID
+
1
}
else
{
rba
.
ID
=
1
}
if
comsoc
.
CheckProfileAlternative
(
rba
.
pfl
,
req
.
Vote
)
!=
nil
{
rba
.
rule
=
req
.
rule
rba
.
voterIDs
=
req
.
voterIDs
for
i
:=
1
;
i
<=
req
.
alts
;
i
++
{
rba
.
alts
[
i
]
=
Alternative
(
i
)
}
deadline
,
err
:=
time
.
Parse
(
"Mon Jan _2 15:04:05 MST 2006"
,
req
.
deadline
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusNotImplemented
)
msg
:=
fmt
.
Sprintf
(
"
Vote of voter
'%s' is not correct"
,
req
.
ID
)
msg
:=
fmt
.
Sprintf
(
"
Deadline
\"
'%s'
\"
is not
in a
correct
format
"
,
req
.
deadline
)
r
.
Write
([]
byte
(
msg
))
return
}
time
.
AfterFunc
(
time
.
Until
(
deadline
),
closeVote
(
rba
))
// treat deadline
w
.
WriteHeader
(
http
.
StatusOK
)
serial
,
_
:=
json
.
Marshal
(
resp
)
w
.
Write
(
serial
)
}
func
(
rba
*
RestBallotAgent
)
do
Close_v
ote
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
rba
*
RestBallotAgent
)
do
V
ote
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// mise à jour du nombre de requêtes
rba
.
Lock
()
defer
rba
.
Unlock
()
...
...
@@ -106,7 +176,7 @@ func (rba *RestBallotAgent) doClose_vote(w http.ResponseWriter, r *http.Request)
}
// décodage de la requête
req
,
err
:=
rba
.
decodeRequest
(
r
)
req
,
err
:=
rba
.
decodeRequest
Vote
(
r
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprint
(
w
,
err
.
Error
())
...
...
@@ -114,25 +184,67 @@ func (rba *RestBallotAgent) doClose_vote(w http.ResponseWriter, r *http.Request)
}
// traitement de la requête
var
resp
Response
switch
rba
.
Meth
{
rba
.
pfl
=
append
(
rba
.
pfl
,
req
.
prefs
)
// wtf the error
if
comsoc
.
CheckProfileAlternative
(
rba
.
pfl
,
req
.
Vote
)
!=
nil
{
w
.
WriteHeader
(
http
.
StatusNotImplemented
)
msg
:=
fmt
.
Sprintf
(
"Vote of voter '%s' is not correct"
,
req
.
ID
)
r
.
Write
([]
byte
(
msg
))
return
}
switch
rba
.
rule
{
case
"borda"
:
//call borda SCF and SWF and store results
}
// another way to do this would be to call method directly from router (mux)
// code here
w
.
WriteHeader
(
http
.
StatusOK
)
// serial, _ := json.Marshal(resp)
// w.Write(serial)
}
func
(
rba
*
RestBallotAgent
)
doResult
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// mise à jour du nombre de requêtes
rba
.
Lock
()
defer
rba
.
Unlock
()
// rba.reqCount++
// vérification de la méthode de la requête
if
!
rba
.
checkMethod
(
"POST"
,
w
,
r
)
{
return
}
// décodage de la requête
req
,
err
:=
rba
.
decodeRequestResult
(
r
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprint
(
w
,
err
.
Error
())
return
}
// traitement de la requête
var
resp
ResponseResult
// code here
w
.
WriteHeader
(
http
.
StatusOK
)
serial
,
_
:=
json
.
Marshal
(
resp
)
w
.
Write
(
serial
)
}
func
closeVote
(
rba
*
RestBallotAgent
)
{
rba
.
status
=
0
}
func
(
rba
*
RestBallotAgent
)
Start
()
{
// création du routeur
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
"/new_ballot"
)
mux
.
HandleFunc
(
"/vote"
)
mux
.
HandleFunc
(
"/close_vote"
)
mux
.
HandleFunc
(
"/get_results"
)
mux
.
HandleFunc
(
"/result"
)
// création du serveur http
s
:=
&
http
.
Server
{
...
...
This diff is collapsed.
Click to expand it.
ia04/comsoc/utils.go
+
9
−
0
View file @
e79d7a20
...
...
@@ -38,6 +38,15 @@ func maxCount(count Count) (bestAlts []Alternative) {
return
}
func
ranking
(
count
Count
)
(
ranking
[]
Alternative
)
{
for
len
(
count
)
>
0
{
best
:=
maxCount
(
count
)[
0
]
// comment gérer le tie break
ranking
=
append
(
ranking
,
)
delete
(
count
,
best
)
}
return
}
// vérifie le profil donné, par ex. qu'ils sont tous complets et que chaque alternative n'apparaît qu'une seule fois par préférences
// func checkProfile(prefs Profile) error
...
...
This diff is collapsed.
Click to expand it.
ia04/go.mod
+
1
−
3
View file @
e79d7a20
...
...
@@ -2,6 +2,4 @@ module ia04
go 1.19
require (
gitlab.utc.fr/lagruesy/ia04 v0.2.0
)
require gitlab.utc.fr/lagruesy/ia04 v0.2.0
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