-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessages.go
139 lines (114 loc) · 3.03 KB
/
messages.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package passtor
import (
"fmt"
"net"
"go.dedis.ch/protobuf"
)
// SendMessage send the given message to the remote peer over udp
// returns the reply message once it has arrived
func (p *Passtor) SendMessage(msg Message, dst net.UDPAddr,
retries int) *Message {
msg.Sender = &p.Addr
if msg.Reply {
bytes, err := protobuf.Encode(&msg)
checkErr(err)
_, err = p.PConn.WriteTo(bytes, &dst)
checkErr(err)
} else {
// this message is a request generate new message id
// register message channel to get the reply
id := p.Messages.GetMessageID()
c := make(chan Message)
// take new message id
p.Messages.Mutex.Lock()
p.Messages.PendingMsg[id] = &c
p.Messages.Mutex.Unlock()
msg.ID = id
bytes, err := protobuf.Encode(&msg)
checkErr(err)
// tries to send a message up to retries times
for i := 0; i < retries; i++ {
// send the message
_, err = p.PConn.WriteTo(bytes, &dst)
checkErr(err)
// define a timeout
timeout := Timeout(TIMEOUT)
select {
case <-*timeout:
// on timeout resend message
continue
case rep := <-c:
// on message reply return it
return &rep
}
}
}
// if this is reached, no reply was received
return nil
}
// HandleMessage handles incoming messages
func (p *Passtor) HandleMessage(protobufed []byte) {
// unprotobuf the message
rep := Message{}
err := protobuf.Decode(protobufed, &rep)
checkErr(err)
// add the sender to bucket if necessary
p.AddPeerToBucket(*rep.Sender)
if rep.Reply {
// if the message is a replyd istribute reply to thread that sent req
p.Messages.Mutex.Lock()
*p.Messages.PendingMsg[rep.ID] <- rep
p.Messages.Mutex.Unlock()
return
}
rep.Reply = true
if rep.Ping != nil {
//ping message
p.SendMessage(rep, rep.Sender.Addr, MINRETRIES)
} else if rep.LookupReq != nil {
// reply to lookup
p.LookupRep(rep)
} else if rep.AllocationReq != nil {
// allocate and reply to allocation request
p.HandleAllocation(rep)
} else if rep.FetchReq != nil {
// searches and reply with corresponding data
p.HandleFetch(rep)
}
}
func (p *Passtor) HandleClientMessage(accounts Accounts, message ClientMessage) *ServerResponse {
// Update or store new account
if message.Push != nil {
p.Printer.Print(fmt.Sprint("Push request ", message.Push.ID), V2)
providers := p.Allocate(message.Push.ID, REPL, *message.Push)
p.Printer.Print(fmt.Sprint("Allocated at:", providers), V2)
if len(providers) == REPL {
return &ServerResponse{
Status: "ok",
}
}
msg := fmt.Sprintln("couldn't allocate data to ", REPL, "peers")
return &ServerResponse{
Status: "warning",
Debug: &msg,
}
}
// Retrieve account
if message.Pull != nil {
p.Printer.Print(fmt.Sprint("Pull request ", *message.Pull), V2)
account := p.FetchData(message.Pull, THRESHOLD)
if account != nil {
accountNetwork := account.ToAccountNetwork()
return &ServerResponse{
Status: "ok",
Data: &accountNetwork,
}
}
msg := "This account does not exist"
return &ServerResponse{
Status: "error",
Debug: &msg,
}
}
return nil
}