Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IA04binôme2A
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
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
Gabrielle Van De Vijver
IA04binôme2A
Commits
042ff11e
Commit
042ff11e
authored
1 year ago
by
Balthazar Wilson
Browse files
Options
Downloads
Patches
Plain Diff
add: voter agent
parent
459dd6d8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
agt/voteragent/voteragent.go
+69
-53
69 additions, 53 deletions
agt/voteragent/voteragent.go
cmd/launch-vagt/launch-vagt.go
+1
-1
1 addition, 1 deletion
cmd/launch-vagt/launch-vagt.go
with
70 additions
and
54 deletions
agt/voteragent/voteragent.go
+
69
−
53
View file @
042ff11e
package
voteragent
package
voteragent
import
(
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
rad
"gitlab.utc.fr/gvandevi/ia04binome2a"
cs
"gitlab.utc.fr/gvandevi/ia04binome2a/comsoc"
)
type
RestClientAgent
struct
{
type
RestClientAgent
struct
{
id
string
id
string
url
string
url
string
operator
string
scrutin
string
arg1
int
nbAlts
int
arg2
int
prefs
[]
int
options
[]
int
}
}
func
NewRestClientAgent
(
id
string
,
url
string
,
op
string
,
arg1
int
,
arg2
int
)
*
RestClientAgent
{
func
NewRestClientAgent
(
id
string
,
url
string
,
scrutin
string
,
nbAlts
int
,
prefs
[]
int
,
options
[]
int
)
*
RestClientAgent
{
return
&
RestClientAgent
{
id
,
url
,
op
,
arg1
,
arg2
}
return
&
RestClientAgent
{
id
,
url
,
scrutin
,
nbAlts
,
prefs
,
options
}
}
}
//func (rca *RestClientAgent) treatResponse(r *http.Response) int {
func
(
rca
*
RestClientAgent
)
vote
()
(
err
error
)
{
// buf := new(bytes.Buffer)
req
:=
rad
.
Vote
{
// buf.ReadFrom(r.Body)
AgentID
:
rca
.
id
,
//
BallotID
:
rca
.
scrutin
,
// var resp rad.Response
Prefs
:
rca
.
prefs
,
// json.Unmarshal(buf.Bytes(), &resp)
Options
:
rca
.
options
,
//
}
// return resp.Result
//}
// sérialisation de la requête
//
url
:=
rca
.
url
+
"/vote"
//func (rca *RestClientAgent) doRequest() (res int, err error) {
data
,
_
:=
json
.
Marshal
(
req
)
// req := rad.Request{
// Operator: rca.operator,
// envoi de la requête
// Args: [2]int{rca.arg1, rca.arg2},
resp
,
err
:=
http
.
Post
(
url
,
"application/json"
,
bytes
.
NewBuffer
(
data
))
// }
//
// traitement de la réponse
// // sérialisation de la requête
if
err
!=
nil
{
// url := rca.url + "/calculator"
return
// data, _ := json.Marshal(req)
}
//
if
resp
.
StatusCode
!=
http
.
StatusOK
{
// // envoi de la requête
err
=
fmt
.
Errorf
(
"[%d] %s"
,
resp
.
StatusCode
,
resp
.
Status
)
// resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
return
//
}
// // traitement de la réponse
// if err != nil {
return
// return
}
// }
// if resp.StatusCode != http.StatusOK {
func
(
rca
*
RestClientAgent
)
Start
()
{
// err = fmt.Errorf("[%d] %s", resp.StatusCode, resp.Status)
log
.
Printf
(
"démarrage de %s"
,
rca
.
id
)
// return
// }
prefs
:=
make
([]
cs
.
Alternative
,
0
)
// res = rca.treatResponse(resp)
if
len
(
rca
.
prefs
)
==
0
{
//
prefs
=
cs
.
GenerateProfile
(
1
,
rca
.
nbAlts
)[
0
]
// return
rca
.
prefs
=
make
([]
int
,
len
(
prefs
))
//}
for
i
,
pref
:=
range
prefs
{
rca
.
prefs
[
i
]
=
int
(
pref
)
//func (rca *RestClientAgent) Start() {
}
// log.Printf("démarrage de %s", rca.id)
}
// res, err := rca.doRequest()
//
rca
.
options
=
[]
int
{
1
+
rand
.
Intn
(
rca
.
nbAlts
-
1
)}
// if err != nil {
// log.Fatal(rca.id, "error:", err.Error())
err
:=
rca
.
vote
()
// } else {
// log.Printf("[%s] %d %s %d = %d\n", rca.id, rca.arg1, rca.operator, rca.arg2, res)
if
err
!=
nil
{
// }
log
.
Fatal
(
rca
.
id
,
"error:"
,
err
.
Error
())
//}
}
else
{
log
.
Printf
(
"[%s] voted in %s
\n
"
,
rca
.
id
,
rca
.
scrutin
)
}
}
This diff is collapsed.
Click to expand it.
cmd/launch-vagt/launch-vagt.go
+
1
−
1
View file @
042ff11e
...
@@ -7,7 +7,7 @@ import (
...
@@ -7,7 +7,7 @@ import (
)
)
func
main
()
{
func
main
()
{
ag
:=
va
.
NewRestClientAgent
(
"
id
1"
,
"http://localhost:8080"
,
"
+"
,
11
,
1
)
ag
:=
va
.
NewRestClientAgent
(
"
ag
1"
,
"http://localhost:8080"
,
"
scrutin1"
,
3
,
make
([]
int
,
0
),
make
([]
int
,
0
)
)
ag
.
Start
()
ag
.
Start
()
fmt
.
Scanln
()
fmt
.
Scanln
()
}
}
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