This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dummy.go
110 lines (92 loc) · 2.06 KB
/
dummy.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
package main
import (
"encoding/binary"
"fmt"
"net"
)
type DummyMessage struct {
Party PartyID
Value uint64
}
type DummyProtocol struct {
*LocalParty
Chan chan DummyMessage
Peers map[PartyID]*DummyRemote
Input uint64
Output uint64
}
type DummyRemote struct {
*RemoteParty
Chan chan DummyMessage
}
func (lp *LocalParty) NewDummyProtocol(input uint64) *DummyProtocol {
cep := new(DummyProtocol)
cep.LocalParty = lp
cep.Chan = make(chan DummyMessage, 32)
cep.Peers = make(map[PartyID]*DummyRemote, len(lp.Peers))
for i, rp := range lp.Peers {
cep.Peers[i] = &DummyRemote{
RemoteParty: rp,
Chan: make(chan DummyMessage, 32),
}
}
cep.Input = input
cep.Output = input
return cep
}
func (cep *DummyProtocol) BindNetwork(nw *TCPNetworkStruct) {
for partyID, conn := range nw.Conns {
if partyID == cep.ID {
continue
}
rp := cep.Peers[partyID]
// Receiving loop from remote
go func(conn net.Conn, rp *DummyRemote) {
for {
var id, val uint64
var err error
err = binary.Read(conn, binary.BigEndian, &id)
check(err)
err = binary.Read(conn, binary.BigEndian, &val)
check(err)
msg := DummyMessage{
Party: PartyID(id),
Value: val,
}
//fmt.Println(cep, "receiving", msg, "from", rp)
cep.Chan <- msg
}
}(conn, rp)
// Sending loop of remote
go func(conn net.Conn, rp *DummyRemote) {
var m DummyMessage
var open = true
for open {
m, open = <- rp.Chan
//fmt.Println(cep, "sending", m, "to", rp)
check(binary.Write(conn, binary.BigEndian, m.Party))
check(binary.Write(conn, binary.BigEndian, m.Value))
}
}(conn, rp)
}
}
func (cep *DummyProtocol) Run() {
fmt.Println(cep, "is running")
for _, peer := range cep.Peers {
if peer.ID != cep.ID {
peer.Chan <- DummyMessage{cep.ID, cep.Input}
}
}
received := 0
for m := range cep.Chan {
fmt.Println(cep, "received message from", m.Party, ":", m.Value)
cep.Output += m.Value
received++
if received == len(cep.Peers)-1 {
close(cep.Chan)
}
}
if cep.WaitGroup != nil {
cep.WaitGroup.Done()
}
}