This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
76 lines (65 loc) · 1.99 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package decenarch
/*
The api.go defines the methods that can be called from the outside. Most
of the methods will take a roster so that the service knows which nodes
it should work with.
This part of the service runs on the client or the app.
*/
import (
"time"
"gopkg.in/dedis/onet.v2"
"gopkg.in/dedis/onet.v2/log"
)
// ServiceName is used for registration on the onet.
const ServiceName = "Decenarch"
const StatTimeFormat = "2006/01/02 15:04:05.0000"
// Client is a structure to communicate with the Decenarch
// service
type Client struct {
*onet.Client
}
// NewClient instantiates a new decenarch.Client
func NewClient() *Client {
return &Client{Client: onet.NewClient(Suite, ServiceName)}
}
// Setup will setup everything is needed for DecenArch
func (c *Client) Setup(r *onet.Roster) (*SetupResponse, error) {
dst := r.RandomServerIdentity()
resp := &SetupResponse{}
err := c.SendProtobuf(dst, &SetupRequest{Roster: r}, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// Save will record the website requested in the conodes
func (c *Client) Save(r *onet.Roster, url string) (*SaveResponse, error) {
dst := r.RandomServerIdentity()
log.Lvl4("Sending message to", dst)
resp := &SaveResponse{Times: make([]string, 0)}
resp.Times = append(resp.Times, "genstart;"+time.Now().Format(StatTimeFormat))
err := c.SendProtobuf(dst, &SaveRequest{url, r}, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// Retrieve will send the website requested to the client
func (c *Client) Retrieve(r *onet.Roster, url string, timestamp string) (*RetrieveResponse, error) {
// if no timestamp is given, take 'now as timestamp'
if timestamp == "" {
t := time.Now()
timestamp = t.Format("2006/01/02 15:04")
}
resp := &RetrieveResponse{}
dst := r.RandomServerIdentity()
err := c.SendProtobuf(
dst,
&RetrieveRequest{Roster: r, Url: url, Timestamp: timestamp},
resp)
if err != nil {
return nil, err
}
log.Info("Page", resp.Main.Url, "sucessfully retrieved!")
return resp, nil
}