forked from 7574-sistemas-distribuidos/tp0-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
175 lines (148 loc) · 3.38 KB
/
client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"context"
"errors"
"io"
"net"
"time"
"github.com/juliangcalderon-fiuba/distribuidos-tp0/common"
"github.com/juliangcalderon-fiuba/distribuidos-tp0/safeio"
"github.com/juliangcalderon-fiuba/distribuidos-tp0/protocol"
)
type clientConfig struct {
id int
batchSize int
serverAddress string
loopPeriod time.Duration
}
type client struct {
config clientConfig
conn *net.TCPConn
connReader *safeio.Reader
connWriter *safeio.Writer
betsReader *safeio.Reader
}
func newClient(config clientConfig, betsReader *safeio.Reader) *client {
client := &client{
config: config,
betsReader: betsReader,
}
return client
}
func (c *client) createClientSocket() error {
raddr, err := net.ResolveTCPAddr("tcp", c.config.serverAddress)
if err != nil {
return err
}
conn, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
return err
}
c.conn = conn
c.connReader = safeio.NewReader(conn)
c.connWriter = safeio.NewWriter(conn)
err = protocol.SendFlush(protocol.HelloMessage{AgencyId: c.config.id}, c.connWriter)
if err != nil {
closeErr := closeSocket(c.conn)
return errors.Join(err, closeErr)
}
return nil
}
func (c *client) run(ctx context.Context) (err error) {
err = c.createClientSocket()
if err != nil {
return err
}
closer := common.SpawnCloser(ctx, c.conn, closeSocket)
defer func() {
closeErr := closer.Close()
err = errors.Join(err, closeErr)
}()
for {
batch, err := c.readBatch()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return err
}
err = c.sendBatch(batch)
if err != nil {
log.Error(common.FmtLog("send_batch", err))
if errors.Is(err, net.ErrClosed) || errors.Is(err, io.EOF) {
return err
}
} else {
log.Info(common.FmtLog("send_batch", nil,
"batchSize", len(batch),
))
}
select {
case <-ctx.Done():
return net.ErrClosed
case <-time.After(c.config.loopPeriod):
}
}
err = protocol.SendFlush(protocol.FinishMessage{}, c.connWriter)
if err != nil {
return err
}
winners, err := protocol.Receive[protocol.WinnersMessage](c.connReader)
if err != nil {
return err
} else {
log.Info(common.FmtLog("consulta_ganadores", nil,
"cant_ganadores", len(winners),
))
}
return nil
}
func (c *client) sendBatch(bets []protocol.BetMessage) error {
err := protocol.SendFlush(protocol.BatchMessage{BatchSize: len(bets)}, c.connWriter)
if err != nil {
return err
}
for _, bet := range bets {
protocol.Send(bet, c.connWriter)
}
err = protocol.Flush(c.connWriter)
if err != nil {
return err
}
_, err = protocol.Receive[protocol.OkMessage](c.connReader)
if err != nil {
return err
}
return nil
}
// Reads batch from agency data, with up to `c.config.batchSize` bets
func (c *client) readBatch() ([]protocol.BetMessage, error) {
batch := make([]protocol.BetMessage, 0, c.config.batchSize)
for i := 0; i < c.config.batchSize; i++ {
betRecord, err := c.betsReader.Read()
if errors.Is(err, io.EOF) {
if len(batch) > 0 {
return batch, nil
} else {
return nil, io.EOF
}
}
if err != nil {
return nil, err
}
bet, err := protocol.Deserialize[protocol.BetMessage](betRecord)
if err != nil {
return nil, err
}
batch = append(batch, bet)
}
return batch, nil
}
func closeSocket(c *net.TCPConn) error {
err := c.Close()
if err != nil {
log.Error(common.FmtLog("close_connection", err))
return err
}
return nil
}