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
dd6a9053
Commit
dd6a9053
authored
2 years ago
by
Antoine Kryus
Browse files
Options
Downloads
Patches
Plain Diff
update vote route function
parent
225f8360
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ia04/agt/ballotagent/ballotAgent.go
+50
-14
50 additions, 14 deletions
ia04/agt/ballotagent/ballotAgent.go
with
50 additions
and
14 deletions
ia04/agt/ballotagent/ballotAgent.go
+
50
−
14
View file @
dd6a9053
...
...
@@ -59,6 +59,8 @@ type RestBallotAgent struct {
isOpen
bool
// open: 1 / closed: 0
voterIDs
[]
string
alts
[]
comsoc
.
Alternative
count
comsoc
.
Count
bestAlts
[]
comsoc
.
Alternative
}
...
...
@@ -158,11 +160,25 @@ func (rba *RestBallotAgent) doNewBallot(w http.ResponseWriter, r *http.Request)
// close vote after deadline
_
=
time
.
AfterFunc
(
time
.
Until
(
deadline
),
func
(){
rba
.
isOpen
=
false
})
var
err
error
switch
rba
.
rule
{
case
"borda"
:
rba
.
count
,
err
=
comsoc
.
BordaSWF
(
rba
.
pfl
)
rba
.
bestAlts
,
err
=
comsoc
.
BordaSCF
(
rba
.
pfl
)
case
"copeland"
:
rba
.
count
,
err
=
comsoc
.
CopelandSWF
(
rba
.
pfl
)
rba
.
bestAlts
,
err
=
comsoc
.
CopelandSCF
(
rba
.
pfl
)
case
"majority"
:
rba
.
count
,
err
=
comsoc
.
MajoritySWF
(
rba
.
pfl
)
rba
.
bestAlts
,
err
=
comsoc
.
MajoritySCF
(
rba
.
pfl
)
}
})
// missing case approval
resp
.
ballotID
=
fmt
.
Sprintf
(
"vote%03d"
,
rba
.
ID
)
w
.
WriteHeader
(
http
.
StatusCreated
)
// 201
serial
,
_
:=
json
.
Marshal
(
resp
)
w
.
Write
(
serial
)
}
func
(
rba
*
RestBallotAgent
)
doVote
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -204,6 +220,14 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
return
}
// ballot ID checking
if
fmt
.
Sprintf
(
"vote%03d"
,
rba
.
ID
)
!=
req
.
voteID
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
msg
:=
fmt
.
Sprintf
(
"404: ballot ID '%s' not found."
,
req
.
voteID
)
w
.
Write
([]
byte
(
msg
))
return
}
// Process options case
if
len
(
req
.
options
)
>
0
{
// modify conditions according to options implemented
...
...
@@ -224,16 +248,10 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
return
}
switch
rba
.
rule
{
case
"borda"
:
//call borda SCF and SWF and store results
}
// code here
w
.
WriteHeader
(
http
.
StatusOK
)
// serial, _ := json.Marshal(resp)
// w.Write(serial)
msg
:=
fmt
.
Sprintf
(
"200: OK. Vote taken in account."
,
req
.
agentID
)
w
.
Write
([]
byte
(
msg
))
return
}
func
(
rba
*
RestBallotAgent
)
doResult
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -254,10 +272,28 @@ func (rba *RestBallotAgent) doResult(w http.ResponseWriter, r *http.Request) {
fmt
.
Fprint
(
w
,
err
.
Error
())
return
}
// Deadline checking
if
rba
.
isOpen
{
w
.
WriteHeader
(
http
.
StatusTooEarly
)
msg
:=
fmt
.
Sprintf
(
"425: Deadline of vote '%s' has not passed yet. Too early."
,
req
.
ballotID
)
w
.
Write
([]
byte
(
msg
))
return
}
// ballot ID checking
if
fmt
.
Sprintf
(
"vote%03d"
,
rba
.
ID
)
!=
req
.
ballotID
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
msg
:=
fmt
.
Sprintf
(
"404: ballot ID '%s' not found."
,
req
.
ballotID
)
w
.
Write
([]
byte
(
msg
))
return
}
// traitement de la requête
var
resp
ResponseResult
resp
.
winner
=
int
(
rba
.
bestAlts
[
0
])
// gérer les tie-break
resp
.
ranking
=
comsoc
.
ranking
(
rba
.
count
)
// code here
w
.
WriteHeader
(
http
.
StatusOK
)
...
...
@@ -269,9 +305,9 @@ func (rba *RestBallotAgent) doResult(w http.ResponseWriter, r *http.Request) {
func
(
rba
*
RestBallotAgent
)
Start
()
{
// création du routeur
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
"/new_ballot"
)
mux
.
HandleFunc
(
"/vote"
)
mux
.
HandleFunc
(
"/result"
)
mux
.
HandleFunc
(
"/new_ballot"
,
rba
.
doNewBallot
)
mux
.
HandleFunc
(
"/vote"
,
rba
.
doVote
)
mux
.
HandleFunc
(
"/result"
,
rba
.
doResult
)
// création du serveur http
s
:=
&
http
.
Server
{
...
...
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