diff --git a/agt/voteragent/voteragent.go b/agt/voteragent/voteragent.go index f59b497f47bbecf4e4aa782648be3e66e0822c30..3395f68d67f4a5f19833d9dd3e243530d369c282 100644 --- a/agt/voteragent/voteragent.go +++ b/agt/voteragent/voteragent.go @@ -1,60 +1,76 @@ 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 { - id string - url string - operator string - arg1 int - arg2 int + id string + url string + scrutin string + nbAlts int + prefs []int + options []int } -func NewRestClientAgent(id string, url string, op string, arg1 int, arg2 int) *RestClientAgent { - return &RestClientAgent{id, url, op, arg1, arg2} +func NewRestClientAgent(id string, url string, scrutin string, nbAlts int, prefs []int, options []int) *RestClientAgent { + return &RestClientAgent{id, url, scrutin, nbAlts, prefs, options} } -//func (rca *RestClientAgent) treatResponse(r *http.Response) int { -// buf := new(bytes.Buffer) -// buf.ReadFrom(r.Body) -// -// var resp rad.Response -// json.Unmarshal(buf.Bytes(), &resp) -// -// return resp.Result -//} -// -//func (rca *RestClientAgent) doRequest() (res int, err error) { -// req := rad.Request{ -// Operator: rca.operator, -// Args: [2]int{rca.arg1, rca.arg2}, -// } -// -// // sérialisation de la requête -// url := rca.url + "/calculator" -// data, _ := json.Marshal(req) -// -// // envoi de la requête -// resp, err := http.Post(url, "application/json", bytes.NewBuffer(data)) -// -// // traitement de la réponse -// if err != nil { -// return -// } -// if resp.StatusCode != http.StatusOK { -// err = fmt.Errorf("[%d] %s", resp.StatusCode, resp.Status) -// return -// } -// res = rca.treatResponse(resp) -// -// return -//} - -//func (rca *RestClientAgent) Start() { -// log.Printf("démarrage de %s", rca.id) -// res, err := rca.doRequest() -// -// if err != nil { -// log.Fatal(rca.id, "error:", err.Error()) -// } else { -// log.Printf("[%s] %d %s %d = %d\n", rca.id, rca.arg1, rca.operator, rca.arg2, res) -// } -//} +func (rca *RestClientAgent) vote() (err error) { + req := rad.Vote{ + AgentID: rca.id, + BallotID: rca.scrutin, + Prefs: rca.prefs, + Options: rca.options, + } + + // sérialisation de la requête + url := rca.url + "/vote" + data, _ := json.Marshal(req) + + // envoi de la requête + resp, err := http.Post(url, "application/json", bytes.NewBuffer(data)) + + // traitement de la réponse + if err != nil { + return + } + if resp.StatusCode != http.StatusOK { + err = fmt.Errorf("[%d] %s", resp.StatusCode, resp.Status) + return + } + + return +} + +func (rca *RestClientAgent) Start() { + log.Printf("démarrage de %s", rca.id) + + prefs := make([]cs.Alternative, 0) + if len(rca.prefs) == 0 { + prefs = cs.GenerateProfile(1, rca.nbAlts)[0] + rca.prefs = make([]int, len(prefs)) + for i, pref := range prefs { + rca.prefs[i] = int(pref) + } + } + + rca.options = []int{1 + rand.Intn(rca.nbAlts-1)} + + err := rca.vote() + + if err != nil { + log.Fatal(rca.id, "error:", err.Error()) + } else { + log.Printf("[%s] voted in %s\n", rca.id, rca.scrutin) + } +} diff --git a/cmd/launch-vagt/launch-vagt.go b/cmd/launch-vagt/launch-vagt.go index 11556351cfedfe33ed9dd61f642129cae0f99d16..fbf0857c47f9e5157b979edce3e360e1816b651a 100644 --- a/cmd/launch-vagt/launch-vagt.go +++ b/cmd/launch-vagt/launch-vagt.go @@ -7,7 +7,7 @@ import ( ) func main() { - ag := va.NewRestClientAgent("id1", "http://localhost:8080", "+", 11, 1) + ag := va.NewRestClientAgent("ag1", "http://localhost:8080", "scrutin1", 3, make([]int, 0), make([]int, 0)) ag.Start() fmt.Scanln() }