-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_output.go
68 lines (60 loc) · 1.74 KB
/
udp_output.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
/***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2012
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Rob Miller (rmiller@mozilla.com)
#
# ***** END LICENSE BLOCK *****/
package examples
import (
"fmt"
"github.com/mozilla-services/heka/pipeline"
"net"
)
// This is our plugin struct.
type UdpOutput struct {
conn net.Conn
}
// This is our plugin's config struct
type UdpOutputConfig struct {
Address string
}
// Provides pipeline.HasConfigStruct interface.
func (o *UdpOutput) ConfigStruct() interface{} {
return &UdpOutputConfig{"my.example.com:44444"}
}
// Initialize UDP connection
func (o *UdpOutput) Init(config interface{}) (err error) {
conf := config.(*UdpOutputConfig) // assert we have the right config type
var udpAddr *net.UDPAddr
if udpAddr, err = net.ResolveUDPAddr("udp", conf.Address); err != nil {
return fmt.Errorf("can't resolve %s: %s", conf.Address,
err.Error())
}
if o.conn, err = net.DialUDP("udp", nil, udpAddr); err != nil {
return fmt.Errorf("error dialing %s: %s", conf.Address,
err.Error())
}
return
}
func (o *UdpOutput) Run(runner pipeline.FilterRunner, helper pipeline.PluginHelper) (
err error) {
var outgoing string
for pack := range runner.InChan() {
outgoing = fmt.Sprintf("%s\n", pack.Message.GetPayload())
o.conn.Write([]byte(outgoing))
pack.Recycle()
}
return
}
func init() {
pipeline.RegisterPlugin("UdpOutput", func() interface{} {
return new(UdpOutput)
})
}