Skip to content

Commit 372bc3c

Browse files
authored
Wrappers (#45)
* add new client interface * tidy go mod * add tests for new client send functions * fix SendRaw check for transport phase * SendRaw share code path with Send * Revert "SendRaw share code path with Send" This reverts commit 54d04cd.
1 parent 904b1e1 commit 372bc3c

File tree

7 files changed

+835
-105
lines changed

7 files changed

+835
-105
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ The `record` object must be a `map` or `struct`. Objects that implement the [`ms
5858
record := map[string]interface{}{
5959
"Hello": "World",
6060
}
61-
msg := protocol.NewMessage("tag", record)
62-
err := c.Send(msg)
61+
err := c.SendMessage("tag", record)
6362
```
6463

6564
### Send a byte-encoded message
6665

6766
```go
68-
raw := protocol.RawMessage(myMessageBytes)
69-
err := c.Send(raw)
67+
err := c.SendRaw(myMessageBytes)
7068
```
7169

7270
### Message confirmation

cmd/forward/main.go

+5-22
Original file line numberDiff line numberDiff line change
@@ -85,48 +85,31 @@ func main() {
8585
},
8686
}
8787

88-
msg := protocol.NewMessage(tagVar, record)
89-
mne := protocol.NewMessageExt(tagVar, record)
90-
fwd := protocol.NewForwardMessage(tagVar, entries)
91-
packedFwd, _ := protocol.NewPackedForwardMessage(tagVar+".packed", entries)
92-
compressed, _ := protocol.NewCompressedPackedForwardMessage(tagVar+".compressed",
93-
fwd.Entries)
94-
95-
err = c.Send(msg)
88+
err = c.SendMessage(tagVar, record)
9689
if err != nil {
9790
fmt.Println(err)
9891
os.Exit(1)
9992
}
10093

101-
err = c.Send(mne)
94+
err = c.SendMessageExt(tagVar, record)
10295
if err != nil {
10396
fmt.Println(err)
10497
os.Exit(1)
10598
}
10699

107-
err = c.Send(fwd)
100+
err = c.SendForward(tagVar, entries)
108101
if err != nil {
109102
fmt.Println(err)
110103
os.Exit(1)
111104
}
112105

113-
err = c.Send(packedFwd)
106+
err = c.SendPacked(tagVar+".packed", entries)
114107
if err != nil {
115108
fmt.Println(err)
116109
os.Exit(1)
117110
}
118111

119-
err = c.Send(compressed)
120-
if err != nil {
121-
fmt.Println(err)
122-
os.Exit(1)
123-
}
124-
125-
_, _ = compressed.Chunk()
126-
b, _ := compressed.MarshalMsg(nil)
127-
rm := protocol.RawMessage(b)
128-
129-
err = c.Send(rm)
112+
err = c.SendCompressed(tagVar+".compressed", entries)
130113
if err != nil {
131114
fmt.Println(err)
132115
os.Exit(1)

fluent/client/client.go

+109-38
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ type MessageClient interface {
5151
Disconnect() (err error)
5252
Reconnect() error
5353
Send(e protocol.ChunkEncoder) error
54+
SendCompressed(tag string, entries protocol.EntryList) error
55+
SendCompressedFromBytes(tag string, entries []byte) error
56+
SendForward(tag string, entries protocol.EntryList) error
57+
SendMessage(tag string, record interface{}) error
58+
SendMessageExt(tag string, record interface{}) error
59+
SendPacked(tag string, entries protocol.EntryList) error
60+
SendPackedFromBytes(tag string, entries []byte) error
5461
SendRaw(raw []byte) error
5562
}
5663

@@ -140,6 +147,62 @@ func (c *Client) connect() error {
140147
return nil
141148
}
142149

150+
// Handshake initiates handshake mode. Users must call this before attempting
151+
// to send any messages when the server is configured with a shared key, otherwise
152+
// the server will reject any message events. Successful completion of the
153+
// handshake puts the connection into message (or forward) mode, at which time
154+
// the client is free to send event messages.
155+
func (c *Client) Handshake() error {
156+
c.sessionLock.RLock()
157+
defer c.sessionLock.RUnlock()
158+
159+
if c.session == nil {
160+
return errors.New("not connected")
161+
}
162+
163+
var helo protocol.Helo
164+
165+
r := msgp.NewReader(c.session.Connection)
166+
err := helo.DecodeMsg(r)
167+
168+
if err != nil {
169+
return err
170+
}
171+
172+
salt := make([]byte, 16)
173+
174+
_, err = rand.Read(salt)
175+
if err != nil {
176+
return err
177+
}
178+
179+
ping, err := protocol.NewPing(c.Hostname, c.AuthInfo.SharedKey, salt, helo.Options.Nonce)
180+
if err != nil {
181+
return err
182+
}
183+
184+
err = msgp.Encode(c.session.Connection, ping)
185+
if err != nil {
186+
return err
187+
}
188+
189+
var pong protocol.Pong
190+
191+
err = pong.DecodeMsg(r)
192+
if err != nil {
193+
return err
194+
}
195+
196+
if err := protocol.ValidatePongDigest(&pong, c.AuthInfo.SharedKey,
197+
helo.Options.Nonce, salt); err != nil {
198+
return err
199+
}
200+
201+
c.session.TransportPhase = true
202+
203+
return nil
204+
}
205+
143206
// Connect initializes the Session and Connection objects by opening
144207
// a client connect to the target configured in the ConnectionFactory
145208
func (c *Client) Connect() error {
@@ -239,61 +302,69 @@ func (c *Client) Send(e protocol.ChunkEncoder) error {
239302
// is not yet in transport phase, an error is returned,
240303
// and no message is sent.
241304
func (c *Client) SendRaw(m []byte) error {
242-
return c.Send(protocol.RawMessage(m))
243-
}
244-
245-
// Handshake initiates handshake mode. Users must call this before attempting
246-
// to send any messages when the server is configured with a shared key, otherwise
247-
// the server will reject any message events. Successful completion of the
248-
// handshake puts the connection into message (or forward) mode, at which time
249-
// the client is free to send event messages.
250-
func (c *Client) Handshake() error {
251305
c.sessionLock.RLock()
252306
defer c.sessionLock.RUnlock()
253307

254308
if c.session == nil {
255-
return errors.New("not connected")
309+
return errors.New("no active session")
256310
}
257311

258-
var helo protocol.Helo
312+
if !c.session.TransportPhase {
313+
return errors.New("session handshake not completed")
314+
}
259315

260-
r := msgp.NewReader(c.session.Connection)
261-
err := helo.DecodeMsg(r)
316+
_, err := c.session.Connection.Write(m)
262317

263-
if err != nil {
264-
return err
318+
return err
319+
}
320+
321+
func (c *Client) SendPacked(tag string, entries protocol.EntryList) error {
322+
msg, err := protocol.NewPackedForwardMessage(tag, entries)
323+
if err == nil {
324+
err = c.Send(msg)
265325
}
266326

267-
salt := make([]byte, 16)
327+
return err
328+
}
268329

269-
_, err = rand.Read(salt)
270-
if err != nil {
271-
return err
272-
}
330+
func (c *Client) SendPackedFromBytes(tag string, entries []byte) error {
331+
msg := protocol.NewPackedForwardMessageFromBytes(tag, entries)
273332

274-
ping, err := protocol.NewPing(c.Hostname, c.AuthInfo.SharedKey, salt, helo.Options.Nonce)
275-
if err != nil {
276-
return err
277-
}
333+
return c.Send(msg)
334+
}
278335

279-
err = msgp.Encode(c.session.Connection, ping)
280-
if err != nil {
281-
return err
282-
}
336+
func (c *Client) SendMessage(tag string, record interface{}) error {
337+
msg := protocol.NewMessage(tag, record)
283338

284-
var pong protocol.Pong
339+
return c.Send(msg)
340+
}
285341

286-
err = pong.DecodeMsg(r)
287-
if err != nil {
288-
return err
289-
}
342+
func (c *Client) SendMessageExt(tag string, record interface{}) error {
343+
msg := protocol.NewMessageExt(tag, record)
290344

291-
if err := protocol.ValidatePongDigest(&pong, c.AuthInfo.SharedKey,
292-
helo.Options.Nonce, salt); err != nil {
293-
return err
345+
return c.Send(msg)
346+
}
347+
348+
func (c *Client) SendForward(tag string, entries protocol.EntryList) error {
349+
msg := protocol.NewForwardMessage(tag, entries)
350+
351+
return c.Send(msg)
352+
}
353+
354+
func (c *Client) SendCompressed(tag string, entries protocol.EntryList) error {
355+
msg, err := protocol.NewCompressedPackedForwardMessage(tag, entries)
356+
if err == nil {
357+
err = c.Send(msg)
294358
}
295359

296-
c.session.TransportPhase = true
360+
return err
361+
}
297362

298-
return nil
363+
func (c *Client) SendCompressedFromBytes(tag string, entries []byte) error {
364+
msg, err := protocol.NewCompressedPackedForwardMessageFromBytes(tag, entries)
365+
if err == nil {
366+
err = c.Send(msg)
367+
}
368+
369+
return err
299370
}

0 commit comments

Comments
 (0)